2020-04-24 23:36:52 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
|
|
|
|
|
|
|
with lib;
|
|
|
|
|
|
|
|
let
|
|
|
|
|
|
|
|
cfg = config.services.postgresqlBackup;
|
|
|
|
|
|
|
|
postgresqlBackupService = db: dumpCmd:
|
2021-08-05 21:33:18 +00:00
|
|
|
let
|
|
|
|
compressSuffixes = {
|
|
|
|
"none" = "";
|
|
|
|
"gzip" = ".gz";
|
|
|
|
"zstd" = ".zstd";
|
|
|
|
};
|
|
|
|
compressSuffix = getAttr cfg.compression compressSuffixes;
|
|
|
|
|
|
|
|
compressCmd = getAttr cfg.compression {
|
|
|
|
"none" = "cat";
|
2022-07-14 12:49:19 +00:00
|
|
|
"gzip" = "${pkgs.gzip}/bin/gzip -c -${toString cfg.compressionLevel}";
|
|
|
|
"zstd" = "${pkgs.zstd}/bin/zstd -c -${toString cfg.compressionLevel}";
|
2021-08-05 21:33:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
mkSqlPath = prefix: suffix: "${cfg.location}/${db}${prefix}.sql${suffix}";
|
|
|
|
curFile = mkSqlPath "" compressSuffix;
|
|
|
|
prevFile = mkSqlPath ".prev" compressSuffix;
|
|
|
|
prevFiles = map (mkSqlPath ".prev") (attrValues compressSuffixes);
|
|
|
|
inProgressFile = mkSqlPath ".in-progress" compressSuffix;
|
|
|
|
in {
|
2020-04-24 23:36:52 +00:00
|
|
|
enable = true;
|
|
|
|
|
|
|
|
description = "Backup of ${db} database(s)";
|
|
|
|
|
|
|
|
requires = [ "postgresql.service" ];
|
|
|
|
|
2021-08-05 21:33:18 +00:00
|
|
|
path = [ pkgs.coreutils config.services.postgresql.package ];
|
2021-07-14 22:03:04 +00:00
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
script = ''
|
2021-07-14 22:03:04 +00:00
|
|
|
set -e -o pipefail
|
|
|
|
|
2020-04-24 23:36:52 +00:00
|
|
|
umask 0077 # ensure backup is only readable by postgres user
|
|
|
|
|
2021-08-05 21:33:18 +00:00
|
|
|
if [ -e ${curFile} ]; then
|
|
|
|
rm -f ${toString prevFiles}
|
|
|
|
mv ${curFile} ${prevFile}
|
2020-04-24 23:36:52 +00:00
|
|
|
fi
|
|
|
|
|
2021-08-05 21:33:18 +00:00
|
|
|
${dumpCmd} \
|
|
|
|
| ${compressCmd} \
|
|
|
|
> ${inProgressFile}
|
2021-07-14 22:03:04 +00:00
|
|
|
|
2021-08-05 21:33:18 +00:00
|
|
|
mv ${inProgressFile} ${curFile}
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "postgres";
|
|
|
|
};
|
|
|
|
|
|
|
|
startAt = cfg.startAt;
|
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
|
|
|
|
|
|
|
imports = [
|
|
|
|
(mkRemovedOptionModule [ "services" "postgresqlBackup" "period" ] ''
|
|
|
|
A systemd timer is now used instead of cron.
|
|
|
|
The starting time can be configured via <literal>services.postgresqlBackup.startAt</literal>.
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
|
|
|
options = {
|
|
|
|
services.postgresqlBackup = {
|
|
|
|
enable = mkEnableOption "PostgreSQL dumps";
|
|
|
|
|
|
|
|
startAt = mkOption {
|
|
|
|
default = "*-*-* 01:15:00";
|
2021-05-20 23:08:51 +00:00
|
|
|
type = with types; either (listOf str) str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
|
|
|
This option defines (see `systemd.time` for format) when the
|
2020-04-24 23:36:52 +00:00
|
|
|
databases should be dumped.
|
|
|
|
The default is to update at 01:15 (at night) every day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
backupAll = mkOption {
|
|
|
|
default = cfg.databases == [];
|
2021-10-06 13:57:05 +00:00
|
|
|
defaultText = literalExpression "services.postgresqlBackup.databases == []";
|
2020-04-24 23:36:52 +00:00
|
|
|
type = lib.types.bool;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Backup all databases using pg_dumpall.
|
|
|
|
This option is mutual exclusive to
|
2022-08-12 12:06:08 +00:00
|
|
|
`services.postgresqlBackup.databases`.
|
2020-04-24 23:36:52 +00:00
|
|
|
The resulting backup dump will have the name all.sql.gz.
|
|
|
|
This option is the default if no databases are specified.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
databases = mkOption {
|
|
|
|
default = [];
|
2021-02-05 17:12:51 +00:00
|
|
|
type = types.listOf types.str;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
List of database names to dump.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
location = mkOption {
|
|
|
|
default = "/var/backup/postgresql";
|
2021-02-05 17:12:51 +00:00
|
|
|
type = types.path;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-08-05 21:33:18 +00:00
|
|
|
Path of directory where the PostgreSQL database dumps will be placed.
|
2020-04-24 23:36:52 +00:00
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
pgdumpOptions = mkOption {
|
|
|
|
type = types.separatedString " ";
|
|
|
|
default = "-C";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2020-04-24 23:36:52 +00:00
|
|
|
Command line options for pg_dump. This options is not used
|
2022-08-12 12:06:08 +00:00
|
|
|
if `config.services.postgresqlBackup.backupAll` is enabled.
|
2020-04-24 23:36:52 +00:00
|
|
|
Note that config.services.postgresqlBackup.backupAll is also active,
|
|
|
|
when no databases where specified.
|
|
|
|
'';
|
|
|
|
};
|
2021-08-05 21:33:18 +00:00
|
|
|
|
|
|
|
compression = mkOption {
|
|
|
|
type = types.enum ["none" "gzip" "zstd"];
|
|
|
|
default = "gzip";
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2021-08-05 21:33:18 +00:00
|
|
|
The type of compression to use on the generated database dump.
|
|
|
|
'';
|
|
|
|
};
|
2022-07-14 12:49:19 +00:00
|
|
|
|
|
|
|
compressionLevel = mkOption {
|
|
|
|
type = types.ints.between 1 19;
|
|
|
|
default = 6;
|
2022-08-12 12:06:08 +00:00
|
|
|
description = lib.mdDoc ''
|
2022-07-14 12:49:19 +00:00
|
|
|
The compression level used when compression is enabled.
|
|
|
|
gzip accepts levels 1 to 9. zstd accepts levels 1 to 19.
|
|
|
|
'';
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkMerge [
|
|
|
|
{
|
2022-07-14 12:49:19 +00:00
|
|
|
assertions = [
|
|
|
|
{
|
|
|
|
assertion = cfg.backupAll -> cfg.databases == [];
|
|
|
|
message = "config.services.postgresqlBackup.backupAll cannot be used together with config.services.postgresqlBackup.databases";
|
|
|
|
}
|
|
|
|
{
|
|
|
|
assertion = cfg.compression == "none" ||
|
|
|
|
(cfg.compression == "gzip" && cfg.compressionLevel >= 1 && cfg.compressionLevel <= 9) ||
|
|
|
|
(cfg.compression == "zstd" && cfg.compressionLevel >= 1 && cfg.compressionLevel <= 19);
|
|
|
|
message = "config.services.postgresqlBackup.compressionLevel must be set between 1 and 9 for gzip and 1 and 19 for zstd";
|
|
|
|
}
|
|
|
|
];
|
2020-04-24 23:36:52 +00:00
|
|
|
}
|
|
|
|
(mkIf cfg.enable {
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.location}' 0700 postgres - - -"
|
|
|
|
];
|
|
|
|
})
|
|
|
|
(mkIf (cfg.enable && cfg.backupAll) {
|
|
|
|
systemd.services.postgresqlBackup =
|
2021-07-14 22:03:04 +00:00
|
|
|
postgresqlBackupService "all" "pg_dumpall";
|
2020-04-24 23:36:52 +00:00
|
|
|
})
|
|
|
|
(mkIf (cfg.enable && !cfg.backupAll) {
|
|
|
|
systemd.services = listToAttrs (map (db:
|
|
|
|
let
|
2021-07-14 22:03:04 +00:00
|
|
|
cmd = "pg_dump ${cfg.pgdumpOptions} ${db}";
|
2020-04-24 23:36:52 +00:00
|
|
|
in {
|
|
|
|
name = "postgresqlBackup-${db}";
|
|
|
|
value = postgresqlBackupService db cmd;
|
|
|
|
}) cfg.databases);
|
|
|
|
})
|
|
|
|
];
|
|
|
|
|
|
|
|
}
|