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