From 529656f3173863e12286e6b8911c9c16177afe5c Mon Sep 17 00:00:00 2001 From: mc-fucker Date: Wed, 12 Jul 2023 17:18:18 +0200 Subject: [PATCH] added retention to podman-postgresql backup --- modules/podman-postgresql.nix | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/modules/podman-postgresql.nix b/modules/podman-postgresql.nix index ef7f878..ef0038d 100644 --- a/modules/podman-postgresql.nix +++ b/modules/podman-postgresql.nix @@ -61,6 +61,13 @@ in example = "daily"; }; + backupRetention = mkOption { + description = "The amount of backups to keep."; + type = types.int; + default = 28; + example = "2"; + }; + }; })); }; @@ -101,7 +108,11 @@ in description = "Backup of ${cfg.database} database"; requisite = [ "podman-postgresql-${cfg.database}.service" ]; serviceConfig = { - ExecStart = "${pkgs.bash}/bin/bash -c '${pkgs.podman}/bin/podman exec postgresql-${cfg.database} pg_dumpall -c -U ${cfg.user} | ${pkgs.zstd}/bin/zstd -o ${cfg.backupPath}/${cfg.database}/\$(${pkgs.coreutils}/bin/date +%%F_%%R).sql.zst'" ; + ExecStart = + let + retention = (toString cfg.backupRetention); + in + "${pkgs.bash}/bin/bash -c '${pkgs.podman}/bin/podman exec postgresql-${cfg.database} pg_dumpall -c -U ${cfg.user} | ${pkgs.zstd}/bin/zstd -o ${cfg.backupPath}/${cfg.database}/\$(${pkgs.coreutils}/bin/date +%%F_%%R).sql.zst && find ${cfg.backupPath}/${cfg.database} -type f | sort | head -n -${retention} | xargs rm -v'" ; Type = "oneshot"; }; };