summary refs log tree commit diff
path: root/nixos/modules/services/backup/postgresql-backup.nix
diff options
context:
space:
mode:
authorMarkus Kowalewski <markus.kowalewski@gmail.com>2018-06-17 19:48:51 +0200
committerMarkus Kowalewski <markus.kowalewski@gmail.com>2018-06-17 19:48:51 +0200
commitbaef643232f7f9f916e89ac11ff1b2008e534941 (patch)
tree1e54db88bcd5958aceefa3dab6d66130f87cc62a /nixos/modules/services/backup/postgresql-backup.nix
parentf34b498bd2d998e6d5c1b707076f1f4875b3583e (diff)
downloadnixlib-baef643232f7f9f916e89ac11ff1b2008e534941.tar
nixlib-baef643232f7f9f916e89ac11ff1b2008e534941.tar.gz
nixlib-baef643232f7f9f916e89ac11ff1b2008e534941.tar.bz2
nixlib-baef643232f7f9f916e89ac11ff1b2008e534941.tar.lz
nixlib-baef643232f7f9f916e89ac11ff1b2008e534941.tar.xz
nixlib-baef643232f7f9f916e89ac11ff1b2008e534941.tar.zst
nixlib-baef643232f7f9f916e89ac11ff1b2008e534941.zip
nixos/pgbackup: Fix the postgres backup modules
Diffstat (limited to 'nixos/modules/services/backup/postgresql-backup.nix')
-rw-r--r--nixos/modules/services/backup/postgresql-backup.nix60
1 files changed, 44 insertions, 16 deletions
diff --git a/nixos/modules/services/backup/postgresql-backup.nix b/nixos/modules/services/backup/postgresql-backup.nix
index 4a5ebebc682e..93dcd2f96479 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 <literal>systemd.time</literal>) 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);
   };
 
 }