diff --git a/modules/borg/client.nix b/modules/borg/client.nix index 454a780..45ad5c6 100644 --- a/modules/borg/client.nix +++ b/modules/borg/client.nix @@ -13,6 +13,7 @@ let "**/.stversions/" # Syncthing versioning folders ]; excludeArgs = concatMapStrings (pattern: " --exclude '${pattern}'") excludePatterns; + ntfyUrl = "ntfy.sh/yanlincs-homelab"; in { @@ -116,7 +117,7 @@ in "--keep-yearly ${toString keepYearly}" ]; in '' - trap 'echo "ERROR: Critical backup failure with exit code $? at line $LINENO" >&2; exit 1' ERR + trap 'curl -s -d "Borg backup FAILED on ${config.networking.hostName} (exit $? at line $LINENO)" "${ntfyUrl}" || true; exit 1' ERR set -e export BORG_RSH="${sshCommand}" @@ -126,93 +127,31 @@ in fi if [[ "${cfg.repositoryUrl}" == ssh://* ]]; then - mkdir -p /root/.ssh - chmod 700 /root/.ssh - - if [ -f /home/yanlin/.ssh/config ]; then - cp /home/yanlin/.ssh/config /root/.ssh/config - chmod 600 /root/.ssh/config - fi - - if [ -d /home/yanlin/Credentials/ssh_keys ]; then - mkdir -p /root/Credentials - cp -r /home/yanlin/Credentials/ssh_keys /root/Credentials/ - chmod -R 600 /root/Credentials/ssh_keys - fi - - if [ -f /home/yanlin/.ssh/known_hosts ]; then - cp /home/yanlin/.ssh/known_hosts /root/.ssh/known_hosts - chmod 600 /root/.ssh/known_hosts - fi + mkdir -p /root/.ssh && chmod 700 /root/.ssh + [ -f /home/yanlin/.ssh/config ] && cp /home/yanlin/.ssh/config /root/.ssh/config && chmod 600 /root/.ssh/config + [ -d /home/yanlin/Credentials/ssh_keys ] && mkdir -p /root/Credentials && cp -r /home/yanlin/Credentials/ssh_keys /root/Credentials/ && chmod -R 600 /root/Credentials/ssh_keys + [ -f /home/yanlin/.ssh/known_hosts ] && cp /home/yanlin/.ssh/known_hosts /root/.ssh/known_hosts && chmod 600 /root/.ssh/known_hosts fi if ! borg info > /dev/null 2>&1; then - echo "Initializing Borg repository at ${cfg.repositoryUrl}" borg init --encryption=repokey-blake2 fi - ARCHIVE_NAME="backup-$(date +%Y-%m-%d_%H-%M-%S)" - echo "Creating backup archive: $ARCHIVE_NAME" - BACKUP_START=$(date +%s) borg create \ - --verbose \ - --stats \ - --progress \ + --verbose --stats \ --compression lz4,6 \ --exclude-caches \ ${excludeArgs} \ - "::$ARCHIVE_NAME" \ - ${backupPathsStr} 2>&1 | tee /tmp/borg-create-output.log - BACKUP_END=$(date +%s) - BACKUP_DURATION=$((BACKUP_END - BACKUP_START)) + "::backup-$(date +%Y-%m-%d_%H-%M-%S)" \ + ${backupPathsStr} + BACKUP_DURATION=$(( $(date +%s) - BACKUP_START )) set +e + borg prune --list --prefix 'backup-' --show-rc ${retentionArgs} || true + borg compact || true - echo "Pruning old backups..." - if ! borg prune \ - --list \ - --prefix 'backup-' \ - --show-rc \ - ${retentionArgs}; then - echo "WARNING: Pruning failed, but backup archive was created successfully" >&2 - fi - - echo "Compacting repository..." - if ! borg compact; then - echo "WARNING: Compacting failed, but backup archive was created successfully" >&2 - fi - - { - echo "Extracting backup statistics..." - BACKUP_STATS="Duration: ''${BACKUP_DURATION}s" - - if [ -f /tmp/borg-create-output.log ]; then - ARCHIVE_SIZE=$(grep -E "This archive:" /tmp/borg-create-output.log | awk '{print $3, $4}' 2>/dev/null || echo "") - if [ -n "''$ARCHIVE_SIZE" ]; then - BACKUP_STATS="''$BACKUP_STATS, Archive: ''$ARCHIVE_SIZE" - fi - - DEDUPE_SIZE=$(grep -E "This archive:.*Deduplicated size" /tmp/borg-create-output.log | awk '{for(i=1;i<=NF;i++) if($i~/[0-9]/) print $(i) " " $(i+1); break}' 2>/dev/null || echo "") - if [ -z "''$DEDUPE_SIZE" ]; then - DEDUPE_SIZE=$(grep -A 1 -E "This archive:" /tmp/borg-create-output.log | tail -1 | awk '{print ''$NF-1, ''$NF}' 2>/dev/null || echo "") - fi - if [ -n "''$DEDUPE_SIZE" ]; then - BACKUP_STATS="''$BACKUP_STATS, Deduplicated: ''$DEDUPE_SIZE" - fi - - rm -f /tmp/borg-create-output.log 2>/dev/null || true - fi - - BACKUP_STATS="Backup completed successfully. ''$BACKUP_STATS" - echo "Backup statistics: ''$BACKUP_STATS" - - } || { - echo "WARNING: Statistics extraction failed, but backup succeeded" >&2 - echo "Backup completed successfully for ${config.networking.hostName}" - } - - echo "Backup process completed successfully" + curl -s -d "Backup OK on ${config.networking.hostName} (''${BACKUP_DURATION}s)" "${ntfyUrl}" || true ''; }; @@ -229,20 +168,6 @@ in systemd.targets.multi-user.wants = [ "borg-backup.timer" ]; - environment.etc."borg-backup-manual" = { - text = '' - echo "Starting manual Borg backup..." - systemctl start borg-backup.service - - echo "Checking backup status..." - systemctl status borg-backup.service - - echo "Recent backup logs:" - journalctl -u borg-backup.service -n 20 - ''; - mode = "0755"; - }; - environment.shellAliases = { borg-init = "BORG_REPO='${cfg.repositoryUrl}' BORG_RSH='${sshCommand}' borg init --encryption=repokey-blake2"; borg-status = "systemctl status borg-backup.service borg-backup.timer";