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:
|
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" ];
|
|
|
|
|
|
|
|
script = ''
|
2018-11-06 21:59:29 +01:00
|
|
|
umask 0077 # ensure backup is only readable by postgres user
|
|
|
|
|
2018-06-17 19:48:51 +02:00
|
|
|
if [ -e ${cfg.location}/${db}.sql.gz ]; then
|
|
|
|
${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz
|
|
|
|
fi
|
|
|
|
|
2019-01-19 11:33:20 +01:00
|
|
|
${dumpCmd} | \
|
2018-06-17 19:48:51 +02:00
|
|
|
${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz
|
|
|
|
'';
|
|
|
|
|
|
|
|
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 = {
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable PostgreSQL dumps.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-06-19 18:22:46 +02:00
|
|
|
startAt = mkOption {
|
2018-06-17 19:48:51 +02:00
|
|
|
default = "*-*-* 01:15:00";
|
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 == [];
|
|
|
|
defaultText = "services.postgresqlBackup.databases == []";
|
|
|
|
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 = [];
|
|
|
|
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";
|
|
|
|
description = ''
|
|
|
|
Location to put the gzipped PostgreSQL database dumps.
|
|
|
|
'';
|
|
|
|
};
|
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
|
|
|
'';
|
|
|
|
};
|
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 =
|
|
|
|
postgresqlBackupService "all" "${config.services.postgresql.package}/bin/pg_dumpall";
|
|
|
|
})
|
|
|
|
(mkIf (cfg.enable && !cfg.backupAll) {
|
|
|
|
systemd.services = listToAttrs (map (db:
|
|
|
|
let
|
|
|
|
cmd = "${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db}";
|
|
|
|
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
|
|
|
}
|