at 18.09-beta 2.1 kB view raw
1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 7 cfg = config.services.postgresqlBackup; 8 9 postgresqlBackupService = db : 10 { 11 enable = true; 12 13 description = "Backup of database ${db}"; 14 15 requires = [ "postgresql.service" ]; 16 17 preStart = '' 18 mkdir -m 0700 -p ${cfg.location} 19 chown postgres ${cfg.location} 20 ''; 21 22 script = '' 23 if [ -e ${cfg.location}/${db}.sql.gz ]; then 24 ${pkgs.coreutils}/bin/mv ${cfg.location}/${db}.sql.gz ${cfg.location}/${db}.prev.sql.gz 25 fi 26 27 ${config.services.postgresql.package}/bin/pg_dump ${cfg.pgdumpOptions} ${db} | \ 28 ${pkgs.gzip}/bin/gzip -c > ${cfg.location}/${db}.sql.gz 29 ''; 30 31 serviceConfig = { 32 Type = "oneshot"; 33 PermissionsStartOnly = "true"; 34 User = "postgres"; 35 }; 36 37 startAt = cfg.startAt; 38 }; 39 40in { 41 42 options = { 43 44 services.postgresqlBackup = { 45 46 enable = mkOption { 47 default = false; 48 description = '' 49 Whether to enable PostgreSQL dumps. 50 ''; 51 }; 52 53 startAt = mkOption { 54 default = "*-*-* 01:15:00"; 55 description = '' 56 This option defines (see <literal>systemd.time</literal> for format) when the 57 databases should be dumped. 58 The default is to update at 01:15 (at night) every day. 59 ''; 60 }; 61 62 databases = mkOption { 63 default = []; 64 description = '' 65 List of database names to dump. 66 ''; 67 }; 68 69 location = mkOption { 70 default = "/var/backup/postgresql"; 71 description = '' 72 Location to put the gzipped PostgreSQL database dumps. 73 ''; 74 }; 75 76 pgdumpOptions = mkOption { 77 type = types.string; 78 default = "-Cbo"; 79 description = '' 80 Command line options for pg_dump. 81 ''; 82 }; 83 }; 84 85 }; 86 87 config = mkIf config.services.postgresqlBackup.enable { 88 89 systemd.services = listToAttrs (map (db : { 90 name = "postgresqlBackup-${db}"; 91 value = postgresqlBackupService db; } ) cfg.databases); 92 }; 93 94}