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"; }; };