From baef643232f7f9f916e89ac11ff1b2008e534941 Mon Sep 17 00:00:00 2001 From: Markus Kowalewski Date: Sun, 17 Jun 2018 19:48:51 +0200 Subject: [PATCH 1/3] nixos/pgbackup: Fix the postgres backup modules --- .../services/backup/postgresql-backup.nix | 60 ++++++++++++++----- 1 file changed, 44 insertions(+), 16 deletions(-) diff --git a/nixos/modules/services/backup/postgresql-backup.nix b/nixos/modules/services/backup/postgresql-backup.nix index 4a5ebebc682..93dcd2f9647 100644 --- a/nixos/modules/services/backup/postgresql-backup.nix +++ b/nixos/modules/services/backup/postgresql-backup.nix @@ -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.period; + }; + +in { options = { @@ -28,9 +51,9 @@ in }; period = mkOption { - default = "15 01 * * *"; + default = "*-*-* 01:15:00"; description = '' - This option defines (in the format used by cron) when the + This option defines (in the format used by systemd.time) when the databases should be dumped. 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. ''; }; + + 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); }; } From aee0f49fe6725f1a9551b546a757f523bd560681 Mon Sep 17 00:00:00 2001 From: Markus Kowalewski Date: Sun, 17 Jun 2018 19:49:25 +0200 Subject: [PATCH 2/3] nixos/pgbackup: add postgres backup to the postgres test --- nixos/tests/postgresql.nix | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/nixos/tests/postgresql.nix b/nixos/tests/postgresql.nix index 0ce37b55bb7..2381939552e 100644 --- a/nixos/tests/postgresql.nix +++ b/nixos/tests/postgresql.nix @@ -26,6 +26,9 @@ let { services.postgresql.package=postgresql-package; services.postgresql.enable = true; + + services.postgresqlBackup.enable = true; + services.postgresqlBackup.databases = [ "postgres" ]; }; testScript = '' @@ -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 'ok'"); $machine->shutdown; ''; From 6dc06fdd282dcfd1ca46160b196dc2c2676aa86d Mon Sep 17 00:00:00 2001 From: Markus Kowalewski Date: Tue, 19 Jun 2018 18:22:46 +0200 Subject: [PATCH 3/3] nixos/pgbackup: rename option period -> startAt --- nixos/modules/rename.nix | 6 ++++++ nixos/modules/services/backup/postgresql-backup.nix | 6 +++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/nixos/modules/rename.nix b/nixos/modules/rename.nix index b15dd84999a..26b20f5eb0d 100644 --- a/nixos/modules/rename.nix +++ b/nixos/modules/rename.nix @@ -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 services.postgresqlBackup.startAt. + '') + # Profile splitting (mkRenamedOptionModule [ "virtualization" "growPartition" ] [ "boot" "growPartition" ]) diff --git a/nixos/modules/services/backup/postgresql-backup.nix b/nixos/modules/services/backup/postgresql-backup.nix index 93dcd2f9647..2ec78ce6f2c 100644 --- a/nixos/modules/services/backup/postgresql-backup.nix +++ b/nixos/modules/services/backup/postgresql-backup.nix @@ -34,7 +34,7 @@ let User = "postgres"; }; - startAt = cfg.period; + startAt = cfg.startAt; }; in { @@ -50,10 +50,10 @@ in { ''; }; - period = mkOption { + startAt = mkOption { default = "*-*-* 01:15:00"; description = '' - This option defines (in the format used by systemd.time) when the + This option defines (see systemd.time for format) when the databases should be dumped. The default is to update at 01:15 (at night) every day. '';