Przeglądaj źródła

New module: phoebe.backup.postgresql

pjones/monitoring
Peter J. Jones 4 miesięcy temu
rodzic
commit
7cc5c27782
Podpisane przez: Peter Jones <pjones@devalot.com> ID klucza GPG: 9DAFAA8D01941E49

+ 4
- 0
README.md Wyświetl plik

@@ -35,6 +35,10 @@ Module List
35 35
 
36 36
      HTTPS to HTTP private tunnels for web developers.
37 37
 
38
+  * `phoebe.backup.postgresql`:
39
+
40
+     Simple backups for PostgreSQL via `pg_dump`.
41
+
38 42
 
39 43
 [nixos]: https://nixos.org/
40 44
 [nixpkgs]: https://nixos.org/nixpkgs/

+ 1
- 1
default.nix Wyświetl plik

@@ -15,7 +15,7 @@ pkgs.stdenvNoCC.mkDerivation rec {
15 15
 
16 16
   installPhase = ''
17 17
     mkdir -p $out
18
-    cp -rp bin modules lib $out/
18
+    cp -rp bin modules lib pkgs $out/
19 19
     chmod 0555 $out/bin/*
20 20
   '';
21 21
 }

+ 7
- 0
modules/backup/default.nix Wyświetl plik

@@ -0,0 +1,7 @@
1
+{ config, lib, pkgs, ...}:
2
+
3
+{
4
+  imports = [
5
+    ./postgresql.nix
6
+  ];
7
+}

+ 87
- 0
modules/backup/postgresql.nix Wyświetl plik

@@ -0,0 +1,87 @@
1
+# Simple backups for PostgreSQL.
2
+{ config, lib, pkgs, ...}:
3
+
4
+with lib;
5
+
6
+let
7
+  cfg = config.phoebe.backup.postgresql;
8
+  scripts = (import ../../pkgs/default.nix { inherit pkgs; }).backup-scripts;
9
+  pguser = "postgres";
10
+
11
+  # systemd service:
12
+  service = database: {
13
+    "backup-postgresql-${database}" = {
14
+      description = "Backup PostgreSQL Database ${database}";
15
+      after = [ "postgresql.service" ];
16
+      path  = [ pkgs.coreutils config.services.postgresql.package scripts ];
17
+
18
+      serviceConfig = {
19
+        Type = "simple";
20
+        PermissionsStartOnly = "true";
21
+        User = pguser;
22
+      };
23
+
24
+      preStart = ''
25
+        mkdir -p "${cfg.directory}"
26
+        chown ${pguser}:${pguser} "${cfg.directory}"
27
+        chmod 0750 "${cfg.directory}"
28
+      '';
29
+
30
+      script = ''
31
+        export BACKUP_DIRECTORY="${cfg.directory}"
32
+        export BACKUP_LOG_DIR=stdout
33
+        backup-postgresql-dump.sh "${database}"
34
+        backup-purge.sh -k ${toString cfg.keep} "${cfg.directory}/${database}"
35
+      '';
36
+    };
37
+  };
38
+
39
+  # systemd timer:
40
+  timer = database: {
41
+    "backup-postgresql-${database}" = {
42
+      description = "Scheduled Backup of PostgreSQL ${database}";
43
+      wantedBy = [ "timers.target" ];
44
+      timerConfig.OnCalendar = cfg.schedule;
45
+      timerConfig.RandomizedDelaySec = "5m";
46
+      timerConfig.Unit = "backup-postgresql-${database}.service";
47
+    };
48
+  };
49
+
50
+in
51
+{
52
+  #### Interface
53
+  options.phoebe.backup.postgresql = {
54
+    enable = mkEnableOption "Backup PostgreSQL Databases.";
55
+
56
+    databases = mkOption {
57
+      type = types.nonEmptyListOf types.str;
58
+      example = [ "store" ];
59
+      description = "Database names to backup.";
60
+    };
61
+
62
+    directory = mkOption {
63
+      type = types.path;
64
+      default = "/var/backup/postgresql";
65
+      description = "Base directory where dumps are stored.";
66
+    };
67
+
68
+    schedule = mkOption {
69
+      type = types.str;
70
+      default = "*-*-* 00/2:00:00";
71
+      description = "A systemd OnCalendar formatted frequency specification.";
72
+    };
73
+
74
+    keep = mkOption {
75
+      type = types.ints.positive;
76
+      default = 12;
77
+      description = "Number of backups to keep when deleting older backups.";
78
+    };
79
+  };
80
+
81
+  #### Implementation
82
+  config = mkIf cfg.enable {
83
+    # Configure systemd services and timers:
84
+    systemd.services = foldr (a: b: service a // b) {} cfg.databases;
85
+    systemd.timers   = foldr (a: b: timer   a // b) {} cfg.databases;
86
+  };
87
+}

+ 1
- 0
modules/default.nix Wyświetl plik

@@ -14,6 +14,7 @@ let
14 14
 in
15 15
 {
16 16
   imports = [
17
+    ./backup
17 18
     ./security
18 19
     ./services
19 20
   ];

+ 7
- 0
pkgs/backup-scripts.json Wyświetl plik

@@ -0,0 +1,7 @@
1
+{
2
+  "url": "git://git.devalot.com/backup-scripts.git",
3
+  "rev": "9cadcbeb970405ec99fe99304fcf2ece84927cf9",
4
+  "date": "2019-03-06T14:20:57-07:00",
5
+  "sha256": "0y8qba4v1ybvw8m98rwd32i3c4maf8clmv39lwn0f965wa4cr0l9",
6
+  "fetchSubmodules": false
7
+}

+ 18
- 0
pkgs/default.nix Wyświetl plik

@@ -0,0 +1,18 @@
1
+{ pkgs ? import <nixpkgs> {}
2
+}:
3
+
4
+with pkgs.lib;
5
+
6
+let
7
+  callPackage = f:
8
+    let json = removeAttrs (importJSON f) ["date"];
9
+    in callPackageWith attrs "${pkgs.fetchgit json}/default.nix";
10
+
11
+  attrs = {
12
+    inherit pkgs;
13
+
14
+    # Useful backup scripts.
15
+    backup-scripts = callPackage ./backup-scripts.json { };
16
+  };
17
+
18
+in attrs

Ładowanie…
Anuluj
Zapisz