nixos/pgbackup: Fix the postgres backup modules
This commit is contained in:
parent
f34b498bd2
commit
baef643232
@ -3,18 +3,41 @@
|
|||||||
with lib;
|
with lib;
|
||||||
|
|
||||||
let
|
let
|
||||||
inherit (pkgs) gzip;
|
|
||||||
|
|
||||||
location = config.services.postgresqlBackup.location;
|
cfg = config.services.postgresqlBackup;
|
||||||
|
|
||||||
postgresqlBackupCron = db:
|
postgresqlBackupService = db :
|
||||||
''
|
{
|
||||||
${config.services.postgresqlBackup.period} root ${config.services.postgresql.package}/bin/pg_dump ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz
|
enable = true;
|
||||||
'';
|
|
||||||
|
|
||||||
in
|
description = "Backup of database ${db}";
|
||||||
|
|
||||||
{
|
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";
|
||||||
|
};
|
||||||
|
|
||||||
|
startAt = cfg.period;
|
||||||
|
};
|
||||||
|
|
||||||
|
in {
|
||||||
|
|
||||||
options = {
|
options = {
|
||||||
|
|
||||||
@ -28,9 +51,9 @@ in
|
|||||||
};
|
};
|
||||||
|
|
||||||
period = mkOption {
|
period = mkOption {
|
||||||
default = "15 01 * * *";
|
default = "*-*-* 01:15:00";
|
||||||
description = ''
|
description = ''
|
||||||
This option defines (in the format used by cron) when the
|
This option defines (in the format used by <literal>systemd.time</literal>) when the
|
||||||
databases should be dumped.
|
databases should be dumped.
|
||||||
The default is to update at 01:15 (at night) every day.
|
The default is to update at 01:15 (at night) every day.
|
||||||
'';
|
'';
|
||||||
@ -49,18 +72,23 @@ in
|
|||||||
Location to put the gzipped PostgreSQL database dumps.
|
Location to put the gzipped PostgreSQL database dumps.
|
||||||
'';
|
'';
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pgdumpOptions = mkOption {
|
||||||
|
type = types.string;
|
||||||
|
default = "-Cbo";
|
||||||
|
description = ''
|
||||||
|
Command line options for pg_dump.
|
||||||
|
'';
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
};
|
};
|
||||||
|
|
||||||
config = mkIf config.services.postgresqlBackup.enable {
|
config = mkIf config.services.postgresqlBackup.enable {
|
||||||
services.cron.systemCronJobs = map postgresqlBackupCron config.services.postgresqlBackup.databases;
|
|
||||||
|
|
||||||
system.activationScripts.postgresqlBackup = stringAfter [ "stdio" "users" ]
|
systemd.services = listToAttrs (map (db : {
|
||||||
''
|
name = "postgresqlBackup-${db}";
|
||||||
mkdir -m 0700 -p ${config.services.postgresqlBackup.location}
|
value = postgresqlBackupService db; } ) cfg.databases);
|
||||||
chown root ${config.services.postgresqlBackup.location}
|
|
||||||
'';
|
|
||||||
};
|
};
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user