From 1d46fd1ec64cad5a082f0f1374d9faeda70a3d1d Mon Sep 17 00:00:00 2001 From: h7x4 Date: Thu, 29 Jan 2026 14:12:14 +0900 Subject: [PATCH] bicep/{postgres,mysql}: keep multiple backups, point at latest with symlink --- hosts/bicep/services/mysql/backup.nix | 22 ++++++++++++---------- hosts/bicep/services/postgresql/backup.nix | 20 ++++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/hosts/bicep/services/mysql/backup.nix b/hosts/bicep/services/mysql/backup.nix index 91faa84..1fee265 100644 --- a/hosts/bicep/services/mysql/backup.nix +++ b/hosts/bicep/services/mysql/backup.nix @@ -44,20 +44,22 @@ in ]; script = let - rotations = 1; + rotations = 2; in '' - set -eo pipefail + set -euo pipefail - mysqldump --all-databases | zstd --compress -9 --rsyncable -o "/var/lib/mysql-backups/mysql-dump.sql.zst" + OUT_FILE="$STATE_DIRECTORY/mysql-dump-$(date --iso-8601).sql.zst" + + mysqldump --all-databases | zstd --compress -9 --rsyncable -o "$OUT_FILE" + + rm "$STATE_DIRECTORY/mysql-dump-latest.sql.zst" ||: + ln -s -T "$OUT_FILE" "$STATE_DIRECTORY/mysql-dump-latest.sql.zst" + + while [ $(find -type f "$STATE_DIRECTORY" -printf '.' | wc -c) -gt ${toString rotations} ]; do + rm $(find "$STATE_DIRECTORY" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2) + done ''; - # NOTE: keep multiple backups and symlink latest one once we have more disk again - # mysqldump --all-databases | zstd --compress -9 --rsyncable -o "${backupDir}/$(date --iso-8601)-dump.sql.zst" - - # while [ $(ls -1 "${backupDir}" | wc -l) -gt ${toString rotations} ]; do - # rm $(find "${backupDir}" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2) - # done - serviceConfig = { Type = "oneshot"; User = "mysql"; diff --git a/hosts/bicep/services/postgresql/backup.nix b/hosts/bicep/services/postgresql/backup.nix index 151fa52..5ef7f9f 100644 --- a/hosts/bicep/services/postgresql/backup.nix +++ b/hosts/bicep/services/postgresql/backup.nix @@ -45,18 +45,22 @@ in ]; script = let - rotations = 1; + rotations = 2; in '' - set -eo pipefail + set -euo pipefail - pg_dumpall -U postgres | zstd --compress -9 --rsyncable -o "/var/lib//postgresql-backups/postgresql-dump.sql.zstd" + OUT_FILE="$STATE_DIRECTORY/postgresql-dump-$(date --iso-8601).sql.zst" + + pg_dumpall -U postgres | zstd --compress -9 --rsyncable -o "$OUT_FILE" + + rm "$STATE_DIRECTORY/postgresql-dump-latest.sql.zst" ||: + ln -s -T "$OUT_FILE" "$STATE_DIRECTORY/postgresql-dump-latest.sql.zst" + + while [ $(find -type f "$STATE_DIRECTORY" -printf '.' | wc -c) -gt ${toString rotations} ]; do + rm $(find "$STATE_DIRECTORY" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2) + done ''; - # pg_dumpall -U postgres | zstd --compress -9 --rsyncable -o "${backupDir}/$(date --iso-8601)-dump.sql.zst" - # while [ $(ls -1 "${backupDir}" | wc -l) -gt ${toString rotations} ]; do - # rm $(find "${backupDir}" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2) - # done - serviceConfig = { Type = "oneshot"; User = "postgres";