2024-09-30 12:01:57 +05:30
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
pkgs,
|
|
|
|
...
|
|
|
|
}: {
|
|
|
|
options.snowflake.services.postgresql = {
|
|
|
|
enable = lib.mkEnableOption "Enable postgresql service";
|
|
|
|
|
|
|
|
package = lib.mkOption {
|
|
|
|
type = lib.types.package;
|
2024-10-06 00:00:36 +05:30
|
|
|
default = pkgs.postgresql_16;
|
|
|
|
description = "Package to use for the PostgreSQL service";
|
2024-09-30 12:01:57 +05:30
|
|
|
};
|
|
|
|
|
|
|
|
backup.enable = lib.mkEnableOption "Enable backup service for postgresql databases";
|
|
|
|
};
|
|
|
|
|
|
|
|
config = let
|
|
|
|
cfg = config.snowflake.services.postgresql;
|
|
|
|
in
|
|
|
|
lib.mkIf cfg.enable {
|
|
|
|
services.postgresql = {
|
|
|
|
enable = true;
|
|
|
|
package = cfg.package;
|
|
|
|
};
|
|
|
|
|
2024-09-30 12:24:35 +05:30
|
|
|
snowflake.services.backups.config.postgresql = let
|
2024-09-30 12:01:57 +05:30
|
|
|
compressSuffix = ".zstd";
|
|
|
|
compressCmd = "${pkgs.zstd}/bin/zstd -c";
|
|
|
|
|
|
|
|
baseDir = "/tmp/postgres-backup";
|
|
|
|
|
|
|
|
mkSqlPath = prefix: suffix: "/${baseDir}/all${prefix}.sql${suffix}";
|
|
|
|
curFile = mkSqlPath "" compressSuffix;
|
|
|
|
prevFile = mkSqlPath ".prev" compressSuffix;
|
|
|
|
inProgressFile = mkSqlPath ".in-progress" compressSuffix;
|
|
|
|
in
|
|
|
|
lib.mkIf cfg.backup.enable {
|
|
|
|
dynamicFilesFrom = ''
|
|
|
|
set -e -o pipefail
|
|
|
|
|
|
|
|
mkdir -p ${baseDir}
|
|
|
|
|
|
|
|
# Ensure that the backup folder is only readable by the postgres user
|
|
|
|
umask 0077
|
|
|
|
|
|
|
|
if [ -e ${curFile} ]; then
|
|
|
|
rm -f ${prevFile}
|
|
|
|
mv ${curFile} ${prevFile}
|
|
|
|
fi
|
|
|
|
|
|
|
|
${config.security.sudo.package}/bin/sudo -u postgres ${config.services.postgresql.package}/bin/pg_dumpall \
|
|
|
|
| ${compressCmd} \
|
|
|
|
> ${inProgressFile}
|
|
|
|
|
|
|
|
mv ${inProgressFile} ${curFile}
|
|
|
|
|
|
|
|
echo ${curFile}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|