mirror of
https://git.pvv.ntnu.no/Drift/pvv-nixos-config.git
synced 2026-07-21 20:41:25 +02:00
bicep/postgres: split backups into one file per db
This commit is contained in:
@@ -41,25 +41,42 @@ in
|
|||||||
|
|
||||||
path = with pkgs; [
|
path = with pkgs; [
|
||||||
coreutils
|
coreutils
|
||||||
|
diffutils
|
||||||
zstd
|
zstd
|
||||||
cfg.package
|
cfg.package
|
||||||
];
|
];
|
||||||
|
|
||||||
script = let
|
script = ''
|
||||||
rotations = 2;
|
|
||||||
in ''
|
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
OUT_FILE="$STATE_DIRECTORY/postgresql-dump-$(date --iso-8601).sql.zst"
|
dump() {
|
||||||
|
local name="$1" out tmp
|
||||||
|
out="$STATE_DIRECTORY/$name.sql.zst"
|
||||||
|
tmp="$out.tmp"
|
||||||
|
shift
|
||||||
|
"$@" | zstd -9 --rsyncable -f -o "$tmp"
|
||||||
|
if cmp -s "$tmp" "$out" 2>/dev/null; then
|
||||||
|
rm -f "$tmp"
|
||||||
|
else
|
||||||
|
mv -f "$tmp" "$out"
|
||||||
|
fi
|
||||||
|
}
|
||||||
|
|
||||||
pg_dumpall -U postgres | zstd --compress -9 --rsyncable -o "$OUT_FILE"
|
declare -A keep
|
||||||
|
dump globals pg_dumpall -U postgres --globals-only --restrict-key=backup
|
||||||
|
keep[globals.sql.zst]=1
|
||||||
|
|
||||||
# NOTE: this needs to be a hardlink for rrsync to allow sending it
|
while IFS= read -r db; do
|
||||||
rm "$STATE_DIRECTORY/postgresql-dump-latest.sql.zst" ||:
|
[ -n "$db" ] || continue
|
||||||
ln -T "$OUT_FILE" "$STATE_DIRECTORY/postgresql-dump-latest.sql.zst"
|
dump "$db" pg_dump -U postgres -C -d "$db" --restrict-key=backup
|
||||||
|
keep["$db.sql.zst"]=1
|
||||||
|
done < <(psql -U postgres -tAc "SELECT datname FROM pg_database WHERE datallowconn ORDER BY datname")
|
||||||
|
|
||||||
while [ "$(find "$STATE_DIRECTORY" -type f -printf '.' | wc -c)" -gt '${toString (rotations + 1)}' ]; do
|
# drop dumps of databases that no longer exist
|
||||||
rm "$(find "$STATE_DIRECTORY" -type f -printf '%T+ %p\n' | sort | head -n 1 | cut -d' ' -f2)"
|
for f in "$STATE_DIRECTORY"/*.sql.zst; do
|
||||||
|
[ -e "$f" ] || continue
|
||||||
|
base="$(basename "$f")"
|
||||||
|
[ -n "''${keep[$base]:-}" ] || rm -f "$f"
|
||||||
done
|
done
|
||||||
'';
|
'';
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user