Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions nixos/modules/rename.nix
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,12 @@ with lib;
(mkRenamedOptionModule [ "fonts" "fontconfig" "ultimate" "forceAutohint" ] [ "fonts" "fontconfig" "forceAutohint" ])
(mkRenamedOptionModule [ "fonts" "fontconfig" "ultimate" "renderMonoTTFAsBitmap" ] [ "fonts" "fontconfig" "renderMonoTTFAsBitmap" ])

# postgresqlBackup
(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>.
'')

# Profile splitting
(mkRenamedOptionModule [ "virtualization" "growPartition" ] [ "boot" "growPartition" ])

Expand Down
62 changes: 45 additions & 17 deletions nixos/modules/services/backup/postgresql-backup.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,41 @@
with lib;

let
inherit (pkgs) gzip;

location = config.services.postgresqlBackup.location;
cfg = config.services.postgresqlBackup;

postgresqlBackupCron = db:
''
${config.services.postgresqlBackup.period} root ${config.services.postgresql.package}/bin/pg_dump ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz
'';
postgresqlBackupService = db :
{
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.startAt;
};

in {

options = {

Expand All @@ -27,10 +50,10 @@ in
'';
};

period = mkOption {
default = "15 01 * * *";
startAt = mkOption {
default = "*-*-* 01:15:00";
description = ''
This option defines (in the format used by cron) when the
This option defines (see <literal>systemd.time</literal> for format) when the
databases should be dumped.
The default is to update at 01:15 (at night) every day.
'';
Expand All @@ -49,18 +72,23 @@ in
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 {
services.cron.systemCronJobs = map postgresqlBackupCron config.services.postgresqlBackup.databases;

system.activationScripts.postgresqlBackup = stringAfter [ "stdio" "users" ]
''
mkdir -m 0700 -p ${config.services.postgresqlBackup.location}
chown root ${config.services.postgresqlBackup.location}
'';
systemd.services = listToAttrs (map (db : {
name = "postgresqlBackup-${db}";
value = postgresqlBackupService db; } ) cfg.databases);
};

}
7 changes: 7 additions & 0 deletions nixos/tests/postgresql.nix
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ let
{
services.postgresql.package=postgresql-package;
services.postgresql.enable = true;

services.postgresqlBackup.enable = true;
services.postgresqlBackup.databases = [ "postgres" ];
};

testScript = ''
Expand All @@ -46,6 +49,10 @@ let
$machine->succeed(check_count("SELECT * FROM sth;", 5));
$machine->fail(check_count("SELECT * FROM sth;", 4));
$machine->succeed(check_count("SELECT xpath(\'/test/text()\', doc) FROM xmltest;", 1));

# Check backup service
$machine->succeed("systemctl start postgresqlBackup-postgres.service");
$machine->succeed("zcat /var/backup/postgresql/postgres.sql.gz | grep '<test>ok</test>'");
$machine->shutdown;
'';

Expand Down