2014-04-14 16:26:48 +02:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 17:41:38 +02:00
|
|
|
|
2014-04-14 16:26:48 +02:00
|
|
|
with lib;
|
2009-10-07 13:55:36 +02:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2018-06-17 19:48:51 +02:00
|
|
|
cfg = config.services.postgresqlBackup;
|
2009-10-07 13:55:36 +02:00
|
|
|
|
2019-01-19 11:33:20 +01:00
|
|
|
postgresqlBackupService = db: dumpCmd:
|
2021-08-03 23:04:48 +02:00
|
|
|
let
|
|
|
|
compressSuffixes = {
|
|
|
|
"none" = "";
|
|
|
|
"gzip" = ".gz";
|
|
|
|
"zstd" = ".zstd";
|
|
|
|
};
|
|
|
|
compressSuffix = getAttr cfg.compression compressSuffixes;
|
|
|
|
|
|
|
|
compressCmd = getAttr cfg.compression {
|
|
|
|
"none" = "cat";
|
|
|
|
"gzip" = "${pkgs.gzip}/bin/gzip -c";
|
|
|
|
"zstd" = "${pkgs.zstd}/bin/zstd -c";
|
|
|
|
};
|
|
|
|
|
|
|
|
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 {
|
2018-06-17 19:48:51 +02:00
|
|
|
enable = true;
|
2009-10-07 13:55:36 +02:00
|
|
|
|
2019-01-19 11:33:20 +01:00
|
|
|
description = "Backup of ${db} database(s)";
|
2009-10-07 13:55:36 +02:00
|
|
|
|
2018-06-17 19:48:51 +02:00
|
|
|
requires = [ "postgresql.service" ];
|
|
|
|
|
2021-08-03 23:04:48 +02:00
|
|
|
path = [ pkgs.coreutils config.services.postgresql.package ];
|
2021-06-04 17:28:20 +02:00
|
|
|
|
2018-06-17 19:48:51 +02:00
|
|
|
script = ''
|
2021-06-04 17:34:26 +02:00
|
|
|
set -e -o pipefail
|
|
|
|
|
2018-11-06 21:59:29 +01:00
|
|
|
umask 0077 # ensure backup is only readable by postgres user
|
|
|
|
|
2021-08-03 23:04:48 +02:00
|
|
|
if [ -e ${curFile} ]; then
|
|
|
|
rm -f ${toString prevFiles}
|
|
|
|
mv ${curFile} ${prevFile}
|
2018-06-17 19:48:51 +02:00
|
|
|
fi
|
|
|
|
|
2021-08-03 23:04:48 +02:00
|
|
|
${dumpCmd} \
|
|
|
|
| ${compressCmd} \
|
|
|
|
> ${inProgressFile}
|
2021-06-04 17:34:26 +02:00
|
|
|
|
2021-08-03 23:04:48 +02:00
|
|
|
mv ${inProgressFile} ${curFile}
|
2018-06-17 19:48:51 +02:00
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
User = "postgres";
|
|
|
|
};
|
|
|
|
|
2018-06-19 18:22:46 +02:00
|
|
|
startAt = cfg.startAt;
|
2018-06-17 19:48:51 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
2009-10-07 13:55:36 +02:00
|
|
|
|
2019-12-10 02:51:19 +01:00
|
|
|
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>.
|
|
|
|
'')
|
|
|
|
];
|
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
options = {
|
|
|
|
services.postgresqlBackup = {
|
2020-04-20 20:05:26 +02:00
|
|
|
enable = mkEnableOption "PostgreSQL dumps";
|
2009-10-07 13:55:36 +02:00
|
|
|
|
2018-06-19 18:22:46 +02:00
|
|
|
startAt = mkOption {
|
2018-06-17 19:48:51 +02:00
|
|
|
default = "*-*-* 01:15:00";
|
2021-05-14 18:14:03 +02:00
|
|
|
type = with types; either (listOf str) str;
|
2009-10-07 13:55:36 +02:00
|
|
|
description = ''
|
2018-06-19 18:22:46 +02:00
|
|
|
This option defines (see <literal>systemd.time</literal> for format) when the
|
2009-10-07 13:55:36 +02:00
|
|
|
databases should be dumped.
|
|
|
|
The default is to update at 01:15 (at night) every day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2019-01-19 11:33:20 +01:00
|
|
|
backupAll = mkOption {
|
|
|
|
default = cfg.databases == [];
|
2021-10-03 18:06:03 +02:00
|
|
|
defaultText = literalExpression "services.postgresqlBackup.databases == []";
|
2019-01-19 11:33:20 +01:00
|
|
|
type = lib.types.bool;
|
|
|
|
description = ''
|
|
|
|
Backup all databases using pg_dumpall.
|
|
|
|
This option is mutual exclusive to
|
|
|
|
<literal>services.postgresqlBackup.databases</literal>.
|
|
|
|
The resulting backup dump will have the name all.sql.gz.
|
|
|
|
This option is the default if no databases are specified.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
databases = mkOption {
|
|
|
|
default = [];
|
2021-01-31 12:00:31 +01:00
|
|
|
type = types.listOf types.str;
|
2009-10-07 13:55:36 +02:00
|
|
|
description = ''
|
2011-09-14 20:20:50 +02:00
|
|
|
List of database names to dump.
|
2009-10-07 13:55:36 +02:00
|
|
|
'';
|
|
|
|
};
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
location = mkOption {
|
|
|
|
default = "/var/backup/postgresql";
|
2021-01-31 12:00:31 +01:00
|
|
|
type = types.path;
|
2009-10-07 13:55:36 +02:00
|
|
|
description = ''
|
2021-08-03 23:04:48 +02:00
|
|
|
Path of directory where the PostgreSQL database dumps will be placed.
|
2009-10-07 13:55:36 +02:00
|
|
|
'';
|
|
|
|
};
|
2018-06-17 19:48:51 +02:00
|
|
|
|
|
|
|
pgdumpOptions = mkOption {
|
2019-08-08 22:48:27 +02:00
|
|
|
type = types.separatedString " ";
|
2019-12-15 13:14:21 +01:00
|
|
|
default = "-C";
|
2018-06-17 19:48:51 +02:00
|
|
|
description = ''
|
2019-01-19 11:33:20 +01:00
|
|
|
Command line options for pg_dump. This options is not used
|
|
|
|
if <literal>config.services.postgresqlBackup.backupAll</literal> is enabled.
|
|
|
|
Note that config.services.postgresqlBackup.backupAll is also active,
|
|
|
|
when no databases where specified.
|
2018-06-17 19:48:51 +02:00
|
|
|
'';
|
|
|
|
};
|
2021-08-03 23:04:48 +02:00
|
|
|
|
|
|
|
compression = mkOption {
|
|
|
|
type = types.enum ["none" "gzip" "zstd"];
|
|
|
|
default = "gzip";
|
|
|
|
description = ''
|
|
|
|
The type of compression to use on the generated database dump.
|
|
|
|
'';
|
|
|
|
};
|
2009-10-07 13:55:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
2019-01-19 11:33:20 +01:00
|
|
|
config = mkMerge [
|
|
|
|
{
|
|
|
|
assertions = [{
|
|
|
|
assertion = cfg.backupAll -> cfg.databases == [];
|
|
|
|
message = "config.services.postgresqlBackup.backupAll cannot be used together with config.services.postgresqlBackup.databases";
|
|
|
|
}];
|
|
|
|
}
|
2019-02-24 15:01:07 +01:00
|
|
|
(mkIf cfg.enable {
|
|
|
|
systemd.tmpfiles.rules = [
|
|
|
|
"d '${cfg.location}' 0700 postgres - - -"
|
|
|
|
];
|
|
|
|
})
|
2019-01-19 11:33:20 +01:00
|
|
|
(mkIf (cfg.enable && cfg.backupAll) {
|
|
|
|
systemd.services.postgresqlBackup =
|
2021-06-04 17:28:20 +02:00
|
|
|
postgresqlBackupService "all" "pg_dumpall";
|
2019-01-19 11:33:20 +01:00
|
|
|
})
|
|
|
|
(mkIf (cfg.enable && !cfg.backupAll) {
|
|
|
|
systemd.services = listToAttrs (map (db:
|
|
|
|
let
|
2021-06-04 17:28:20 +02:00
|
|
|
cmd = "pg_dump ${cfg.pgdumpOptions} ${db}";
|
2019-01-19 11:33:20 +01:00
|
|
|
in {
|
2018-06-17 19:48:51 +02:00
|
|
|
name = "postgresqlBackup-${db}";
|
2019-01-19 11:33:20 +01:00
|
|
|
value = postgresqlBackupService db cmd;
|
|
|
|
}) cfg.databases);
|
|
|
|
})
|
|
|
|
];
|
2011-09-14 20:20:50 +02:00
|
|
|
|
2009-10-07 13:55:36 +02:00
|
|
|
}
|