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
|
|
|
|
2018-06-17 18:48:51 +01:00
|
|
|
postgresqlBackupService = db :
|
|
|
|
{
|
|
|
|
enable = true;
|
2009-10-07 12:55:36 +01:00
|
|
|
|
2018-06-17 18:48:51 +01:00
|
|
|
description = "Backup of database ${db}";
|
2009-10-07 12:55:36 +01:00
|
|
|
|
2018-06-17 18:48:51 +01:00
|
|
|
requires = [ "postgresql.service" ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0700 -p ${cfg.location}
|
|
|
|
chown postgres ${cfg.location}
|
|
|
|
'';
|
|
|
|
|
|
|
|
script = ''
|
|
|
|
if [ -e ${cfg.location}/${db}.sql.gz ]; then
|
|
|
|
${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz
|
|
|
|
fi
|
|
|
|
|
|
|
|
${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db} | \
|
|
|
|
${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz
|
|
|
|
'';
|
|
|
|
|
|
|
|
serviceConfig = {
|
|
|
|
Type = "oneshot";
|
|
|
|
PermissionsStartOnly = "true";
|
|
|
|
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
|
|
|
|
|
|
|
options = {
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
services.postgresqlBackup = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable PostgreSQL dumps.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-06-19 17:22:46 +01:00
|
|
|
startAt = mkOption {
|
2018-06-17 18:48:51 +01:00
|
|
|
default = "*-*-* 01:15:00";
|
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.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
databases = mkOption {
|
|
|
|
default = [];
|
|
|
|
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";
|
|
|
|
description = ''
|
|
|
|
Location to put the gzipped PostgreSQL database dumps.
|
|
|
|
'';
|
|
|
|
};
|
2018-06-17 18:48:51 +01:00
|
|
|
|
|
|
|
pgdumpOptions = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "-Cbo";
|
|
|
|
description = ''
|
|
|
|
Command line options for pg_dump.
|
|
|
|
'';
|
|
|
|
};
|
2009-10-07 12:55:36 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.services.postgresqlBackup.enable {
|
|
|
|
|
2018-06-17 18:48:51 +01:00
|
|
|
systemd.services = listToAttrs (map (db : {
|
|
|
|
name = "postgresqlBackup-${db}";
|
|
|
|
value = postgresqlBackupService db; } ) cfg.databases);
|
2009-10-07 12:55:36 +01:00
|
|
|
};
|
2011-09-14 19:20:50 +01:00
|
|
|
|
2009-10-07 12:55:36 +01:00
|
|
|
}
|