86 lines
2.5 KiB
Nix
86 lines
2.5 KiB
Nix
{
|
|
config,
|
|
lib,
|
|
pkgs,
|
|
...
|
|
}:
|
|
with lib;
|
|
let
|
|
cfg = config.vps.remnawave;
|
|
script = pkgs.writeShellScript "remnawave-backup" ''
|
|
set -eu
|
|
stamp=$(date -u +%Y%m%dT%H%M%SZ)
|
|
dir=/var/lib/remnawave-backup
|
|
out="$dir/remnawave-$stamp.sql.gz"
|
|
install -d -m 0700 "$dir"
|
|
${pkgs.docker}/bin/docker exec -e PGPASSWORD="$POSTGRES_PASSWORD" remnawave-db \
|
|
pg_dump -U postgres postgres | ${pkgs.gzip}/bin/gzip -c > "$out"
|
|
chmod 0600 "$out"
|
|
|
|
if [ -n "''${TELEGRAM_BOT_TOKEN:-}" ] && [ -n "''${TELEGRAM_CHAT_ID:-}" ]; then
|
|
${pkgs.curl}/bin/curl -fsS \
|
|
-F "chat_id=$TELEGRAM_CHAT_ID" \
|
|
-F "document=@$out" \
|
|
"https://api.telegram.org/bot$TELEGRAM_BOT_TOKEN/sendDocument" \
|
|
|| echo "telegram backup upload failed" >&2
|
|
fi
|
|
|
|
if [ -n "''${BACKUP_SSH_TARGET:-}" ]; then
|
|
${pkgs.openssh}/bin/scp \
|
|
-i ${config.sops.secrets."panel/backup-ssh-key".path} \
|
|
-o StrictHostKeyChecking=accept-new \
|
|
"$out" "$BACKUP_SSH_TARGET" \
|
|
|| echo "netbird backup copy failed" >&2
|
|
fi
|
|
|
|
ls -1t "$dir"/remnawave-*.sql.gz | tail -n +8 | xargs -r rm -f
|
|
'';
|
|
in
|
|
{
|
|
config = mkIf (cfg.enable && cfg.type == "panel") {
|
|
sops.secrets = {
|
|
"panel/postgres-password" = { };
|
|
"panel/telegram-bot-token" = { };
|
|
"panel/telegram-chat-id" = { };
|
|
"panel/backup-ssh-target" = { };
|
|
"panel/backup-ssh-key" = { };
|
|
};
|
|
|
|
sops.templates."remnawave-backup.env" = {
|
|
mode = "0400";
|
|
content = ''
|
|
POSTGRES_PASSWORD=${config.sops.placeholder."panel/postgres-password"}
|
|
TELEGRAM_BOT_TOKEN=${config.sops.placeholder."panel/telegram-bot-token"}
|
|
TELEGRAM_CHAT_ID=${config.sops.placeholder."panel/telegram-chat-id"}
|
|
BACKUP_SSH_TARGET=${config.sops.placeholder."panel/backup-ssh-target"}
|
|
'';
|
|
};
|
|
|
|
systemd.services.remnawave-backup = {
|
|
description = "Dump the panel database and send it out";
|
|
after = [
|
|
"docker-remnawave-db.service"
|
|
"network-online.target"
|
|
];
|
|
wants = [ "docker-remnawave-db.service" ];
|
|
path = [ pkgs.coreutils ];
|
|
serviceConfig = {
|
|
Type = "oneshot";
|
|
EnvironmentFile = config.sops.templates."remnawave-backup.env".path;
|
|
ExecStart = script;
|
|
};
|
|
};
|
|
|
|
systemd.timers.remnawave-backup = {
|
|
wantedBy = [ "timers.target" ];
|
|
timerConfig = {
|
|
OnCalendar = "daily";
|
|
Persistent = true;
|
|
RandomizedDelaySec = "30m";
|
|
};
|
|
};
|
|
|
|
systemd.tmpfiles.rules = [ "d /var/lib/remnawave-backup 0700 root root -" ];
|
|
};
|
|
}
|