From f2740e87a0bfc0c35c24ac75fbb5ea7c6a7f6b6c Mon Sep 17 00:00:00 2001 From: oqyude Date: Sat, 4 Jul 2026 00:53:14 +0300 Subject: [PATCH] node backup added --- modules/containers/3x-ui.nix | 3 +- modules/users.nix | 2 +- modules/vds/default.nix | 6 +++ modules/vds/systemd.nix | 94 ++++++++++++++++++++++++++++++++++++ 4 files changed, 103 insertions(+), 2 deletions(-) create mode 100644 modules/vds/systemd.nix diff --git a/modules/containers/3x-ui.nix b/modules/containers/3x-ui.nix index 289ab39..6bc8bec 100644 --- a/modules/containers/3x-ui.nix +++ b/modules/containers/3x-ui.nix @@ -98,11 +98,12 @@ # Folders tmpfiles.rules = [ "d /mnt 0755 root root -" - "d /mnt/containers 0755 root root -" + "d /mnt/services 0755 root root -" "d /mnt/services/containers 0755 root root -" "d /mnt/services/containers/3x-ui 0755 root root -" "d /mnt/services/containers/3x-ui/cert 0755 root root -" "d /mnt/services/containers/3x-ui/db 0755 root root -" + "Z /mnt/services/containers/3x-ui 0755 root root -" ]; }; diff --git a/modules/users.nix b/modules/users.nix index 8b7af16..8633ff4 100644 --- a/modules/users.nix +++ b/modules/users.nix @@ -92,7 +92,7 @@ ssh_key_public_root = { format = "yaml"; key = "ssh_key_public"; - path = "/root/.ssh/id_ed25519"; + path = "/root/.ssh/id_ed25519.pub"; owner = "root"; group = "root"; mode = "0655"; diff --git a/modules/vds/default.nix b/modules/vds/default.nix index fbb4dac..7b76310 100644 --- a/modules/vds/default.nix +++ b/modules/vds/default.nix @@ -7,8 +7,14 @@ ../containers/3x-ui.nix ./nginx.nix ./samba.nix + ./systemd.nix # ./glances.nix # ./netbird.nix # ./xray.nix ]; + systemd.tmpfiles.rules = [ + "d /mnt 0755 root root -" + "d /mnt/services 0755 root root -" + "d /mnt/services/containers 0755 root root -" + ]; } diff --git a/modules/vds/systemd.nix b/modules/vds/systemd.nix new file mode 100644 index 0000000..c830c49 --- /dev/null +++ b/modules/vds/systemd.nix @@ -0,0 +1,94 @@ +{ + config, + lib, + pkgs, + xlib, + ... +}: +let + serviceName = "rsync-services-sync"; + serverAddress = "oqyude@100.64.0.0"; + serverDir = "/mnt/services/nodes/vds"; + nodeDir = "/mnt/services"; +in +{ + systemd = { + services = { + "${serviceName}" = { + description = "Bidirectional rsync"; + unitConfig.RequiresMountsFor = [ ]; + after = [ "network-online.target" ]; + wants = [ "network-online.target" ]; + wantedBy = [ "multi-user.target" ]; + script = '' + set -euo pipefail + + RSYNC="${pkgs.rsync}/bin/rsync" + SSH="${pkgs.openssh}/bin/ssh" + + SSH_CMD="$SSH \ + -o BatchMode=yes \ + -o ConnectTimeout=5 \ + -o StrictHostKeyChecking=no" + + echo "Waiting for server..." + + for i in $(seq 1 60); do + echo "Attempt $i" + + if $SSH_CMD ${serverAddress} true >/dev/null 2>&1; then + echo "Server available" + break + fi + + sleep 5 + done + + # final check + $SSH_CMD ${serverAddress} true >/dev/null 2>&1 + + mkdir -p "${nodeDir}" + + if [ ! -d "${nodeDir}" ] || [ -z "$(ls -A "${nodeDir}")" ]; then + echo "Pull <- ${serverAddress}" + + mkdir -p "${nodeDir}" + + $RSYNC \ + -e "$SSH_CMD" \ + -a \ + "${serverAddress}:${serverDir}/" \ + "${nodeDir}/" + else + echo "Push -> ${serverAddress}" + + $RSYNC \ + -e "$SSH_CMD" \ + -a \ + --delete \ + "${nodeDir}/" \ + "${serverAddress}:${serverDir}/" + fi + ''; + serviceConfig = { + Type = "oneshot"; + User = "root"; + Group = "root"; + Nice = 10; + CPUQuota = "5%"; + IOSchedulingClass = "idle"; + }; + }; + }; + timers = { + "${serviceName}" = { + wantedBy = [ "timers.target" ]; + timerConfig = { + OnCalendar = "daily"; + Persistent = true; + Unit = "${serviceName}.service"; + }; + }; + }; + }; +}