2014-04-14 14:26:48 +00:00
|
|
|
{ config, lib, pkgs, ... }:
|
2010-09-13 15:41:38 +00:00
|
|
|
|
2014-04-14 14:26:48 +00:00
|
|
|
with lib;
|
2009-10-07 11:55:36 +00:00
|
|
|
|
|
|
|
let
|
|
|
|
|
2018-06-17 17:48:51 +00:00
|
|
|
cfg = config.services.postgresqlBackup;
|
2009-10-07 11:55:36 +00:00
|
|
|
|
2018-06-17 17:48:51 +00:00
|
|
|
postgresqlBackupService = db :
|
|
|
|
{
|
|
|
|
enable = true;
|
2009-10-07 11:55:36 +00:00
|
|
|
|
2018-06-17 17:48:51 +00:00
|
|
|
description = "Backup of database ${db}";
|
2009-10-07 11:55:36 +00:00
|
|
|
|
2018-06-17 17:48:51 +00:00
|
|
|
requires = [ "postgresql.service" ];
|
|
|
|
|
|
|
|
preStart = ''
|
|
|
|
mkdir -m 0700 -p ${cfg.location}
|
|
|
|
chown postgres ${cfg.location}
|
|
|
|
'';
|
|
|
|
|
|
|
|
script = ''
|
2018-11-06 20:59:29 +00:00
|
|
|
umask 0077 # ensure backup is only readable by postgres user
|
|
|
|
|
2018-06-17 17:48:51 +00: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
|
|
|
|
|
|
|
|
${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 16:22:46 +00:00
|
|
|
startAt = cfg.startAt;
|
2018-06-17 17:48:51 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
in {
|
2009-10-07 11:55:36 +00:00
|
|
|
|
|
|
|
options = {
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-07 11:55:36 +00:00
|
|
|
services.postgresqlBackup = {
|
|
|
|
|
|
|
|
enable = mkOption {
|
|
|
|
default = false;
|
|
|
|
description = ''
|
|
|
|
Whether to enable PostgreSQL dumps.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
2018-06-19 16:22:46 +00:00
|
|
|
startAt = mkOption {
|
2018-06-17 17:48:51 +00:00
|
|
|
default = "*-*-* 01:15:00";
|
2009-10-07 11:55:36 +00:00
|
|
|
description = ''
|
2018-06-19 16:22:46 +00:00
|
|
|
This option defines (see <literal>systemd.time</literal> for format) when the
|
2009-10-07 11:55:36 +00:00
|
|
|
databases should be dumped.
|
|
|
|
The default is to update at 01:15 (at night) every day.
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
databases = mkOption {
|
|
|
|
default = [];
|
|
|
|
description = ''
|
2011-09-14 18:20:50 +00:00
|
|
|
List of database names to dump.
|
2009-10-07 11:55:36 +00:00
|
|
|
'';
|
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-07 11:55:36 +00:00
|
|
|
location = mkOption {
|
|
|
|
default = "/var/backup/postgresql";
|
|
|
|
description = ''
|
|
|
|
Location to put the gzipped PostgreSQL database dumps.
|
|
|
|
'';
|
|
|
|
};
|
2018-06-17 17:48:51 +00:00
|
|
|
|
|
|
|
pgdumpOptions = mkOption {
|
|
|
|
type = types.string;
|
|
|
|
default = "-Cbo";
|
|
|
|
description = ''
|
|
|
|
Command line options for pg_dump.
|
|
|
|
'';
|
|
|
|
};
|
2009-10-07 11:55:36 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
config = mkIf config.services.postgresqlBackup.enable {
|
|
|
|
|
2018-06-17 17:48:51 +00:00
|
|
|
systemd.services = listToAttrs (map (db : {
|
|
|
|
name = "postgresqlBackup-${db}";
|
|
|
|
value = postgresqlBackupService db; } ) cfg.databases);
|
2009-10-07 11:55:36 +00:00
|
|
|
};
|
2011-09-14 18:20:50 +00:00
|
|
|
|
2009-10-07 11:55:36 +00:00
|
|
|
}
|