added retention to podman-postgresql backup

This commit is contained in:
mc-fucker 2023-07-12 17:18:18 +02:00
parent 7593d9efc8
commit 529656f317

View file

@ -61,6 +61,13 @@ in
example = "daily"; 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"; description = "Backup of ${cfg.database} database";
requisite = [ "podman-postgresql-${cfg.database}.service" ]; requisite = [ "podman-postgresql-${cfg.database}.service" ];
serviceConfig = { 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"; Type = "oneshot";
}; };
}; };