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:
|
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" ];
|
|
|
|
|
|
|
|
script = ''
|
2018-11-06 20:59:29 +00:00
|
|
|
umask 0077 # ensure backup is only readable by postgres user
|
|
|
|
|
2018-06-17 18:48:51 +01: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 10:33:20 +00:00
|
|
|
${dumpCmd} | \
|
2018-06-17 18:48:51 +01:00
|
|
|
${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz
|
|
|
|
'';
|
|
|
|
|
|
|
|
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-01-31 11:00:31 +00:00
|
|
|
type = types.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 == [];
|
|
|
|
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 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 = ''
|
|
|
|
Location to put the gzipped PostgreSQL database dumps.
|
|
|
|
'';
|
|
|
|
};
|
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
|
|
|
'';
|
|
|
|
};
|
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 =
|
|
|
|
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 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
|
|
|
}
|