1{ config, lib, pkgs, ... }:
2
3with lib;
4
5let
6 inherit (pkgs) gzip;
7
8 location = config.services.postgresqlBackup.location;
9
10 postgresqlBackupCron = db:
11 ''
12 ${config.services.postgresqlBackup.period} root ${config.services.postgresql.package}/bin/pg_dump ${db} | ${gzip}/bin/gzip -c > ${location}/${db}.gz
13 '';
14
15in
16
17{
18
19 options = {
20
21 services.postgresqlBackup = {
22
23 enable = mkOption {
24 default = false;
25 description = ''
26 Whether to enable PostgreSQL dumps.
27 '';
28 };
29
30 period = mkOption {
31 default = "15 01 * * *";
32 description = ''
33 This option defines (in the format used by cron) when the
34 databases should be dumped.
35 The default is to update at 01:15 (at night) every day.
36 '';
37 };
38
39 databases = mkOption {
40 default = [];
41 description = ''
42 List of database names to dump.
43 '';
44 };
45
46 location = mkOption {
47 default = "/var/backup/postgresql";
48 description = ''
49 Location to put the gzipped PostgreSQL database dumps.
50 '';
51 };
52 };
53
54 };
55
56 config = mkIf config.services.postgresqlBackup.enable {
57 services.cron.systemCronJobs = map postgresqlBackupCron config.services.postgresqlBackup.databases;
58
59 system.activationScripts.postgresqlBackup = stringAfter [ "stdio" "users" ]
60 ''
61 mkdir -m 0700 -p ${config.services.postgresqlBackup.location}
62 chown root ${config.services.postgresqlBackup.location}
63 '';
64 };
65
66}