ctucx.git: nixfiles

ctucx' nixfiles

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
79 
80 
81 
82 
83 
84 
85 
86 
87 
88 
89 
90 
91 
92 
93 
94 
95 
96 
97 
98 
99 
100 
101 
102 
103 
104 
105 
106 
107 
108 
109 
110 
111 
112 
113 
114 
115 
116 
117 
118 
119 
120 
121 
122 
123 
124 
125 
126 
127 
128 
129 
130 
131 
132 
133 
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 
145 
146 
147 
148 
149 
150 
151 
152 
153 
154 
155 
156 
157 
158 
159 
160 
161 
162 
163 
164 
165 
166 
167 
168 
169 
170 
{ options, config, pkgs, lib, ... }:

with lib;

let
  backups = config.restic-backups;

  backupOpts = { ... }: {
    options = {

      user = mkOption {
        type    = types.str;
        default = "root";
      };

      passwordFile = mkOption {
        type = types.str;
      };

      runBeforeBackup = mkOption {
        type = types.str;
        default = "";
      };

      paths = mkOption {
        type    = with types; listOf str;
        default = [];
      };

      postgresDatabases = mkOption {
        type    = with types; listOf str;
        default = [];
      };

      sqliteDatabases = mkOption {
        type    = with types; listOf str;
        default = [];
      };

      influxBuckets = mkOption {
        type    = with types; listOf str;
        default = [];
      };

      targets = mkOption {
        type    = with types; listOf str;
        default = [ "wanderduene.ctu.cx" "briefkasten.ctu.cx" ];
      };

      timerConfig = mkOption {
        type    = types.attrs;
        default = {
          OnCalendar         = "daily";
          RandomizedDelaySec = 1200;
        };
      };

    };
  };

in {

  options.restic-backups = mkOption {
    type    = with types; attrsOf (submodule backupOpts);
    default = {};
  };

  config = mkIf (backups != {}) {

    systemd.services = mapAttrs' (
      name: backup: nameValuePair "restic-backup-${name}" {
        restartIfChanged = false;
        requires         = [ "network.target" "local-fs.target" ];
        onFailure        = [ "email-notify@%i.service" ];

        path = [
          pkgs.restic
        ] ++ optionals (backup.postgresDatabases != []) [
          config.services.postgresql.package
          pkgs.zstd
        ];

        serviceConfig    = {
          Type               = "oneshot";
          User               = backup.user;
          RuntimeDirectory   = "restic-backup-${name}";
          CacheDirectory     = "restic-backup-${name}";
          CacheDirectoryMode = "0700";
#          ReadWritePaths     = backup.paths;
          PrivateTmp         = true;
          ProtectHome        = true;
          ProtectSystem      = "strict";
          Environment        = "RESTIC_PASSWORD_FILE=/tmp/passwordFile";
          ExecStartPre = [
            (
              "!" + (pkgs.writeScript "privileged-pre-start" (''
                #!${pkgs.runtimeShell}
                set -eu pipefail

                cp ${backup.passwordFile} /tmp/passwordFile;

                ${if builtins.elem "briefkasten.ctu.cx" backup.targets then ''
                  cp /run/agenix/restic-server-briefkasten /tmp/briefkasten.ctu.cx;
                '' else "" }

                ${if builtins.elem "wanderduene.ctu.cx" backup.targets then ''
                  cp /run/agenix/restic-server-wanderduene /tmp/wanderduene.ctu.cx;
                '' else "" }

                chown -R ${backup.user} /tmp
              ''))
            )
            (
              pkgs.writeScript "pre-start" (''
                #!${pkgs.runtimeShell}
                set -eu pipefail

                ${backup.runBeforeBackup}

                '' + concatMapStringsSep "\n" (db: ''
                echo "Dumping Postgres-database: ${db}"
                mkdir -p /tmp/postgresDatabases
                pg_dump ${db} | zstd --rsyncable > /tmp/postgresDatabases/${db}.sql.zst
                [ $(du -b /tmp/postgresDatabases/${db}.sql.zst | cut -f1) -gt "50" ] || exit 1
              '') backup.postgresDatabases + concatMapStringsSep "\n" (db: ''
                echo "Dumping sqlite-database: ${db}"
                mkdir -p /tmp/sqliteDatabases
                ${pkgs.sqlite}/bin/sqlite3 ${db} ".backup '/tmp/sqliteDatabases/${builtins.baseNameOf db}.sqlite-backup'"
                [ $(du -b /tmp/sqliteDatabases/${builtins.baseNameOf db}.sqlite-backup | cut -f1) -gt "50" ] || exit 1
              '') backup.sqliteDatabases + concatMapStringsSep "\n" (db: ''
                echo "Dumping influx-bucket: ${db}"
                mkdir -p /tmp/influxBuckets
                ${pkgs.influxdb2}/bin/influx backup --compression=none --bucket=${db} /tmp/influxBuckets/${db}
                [ $(du -b /tmp/influxBuckets/${db} | cut -f1) -gt "50" ] || exit 1
              '') backup.influxBuckets)
            )
          ];
        };

        script = ''
          set -eu pipefail
          export XDG_CACHE_HOME=/var/cache/restic-backup-${name}

        '' + concatMapStringsSep "\n\n" (server: ''
          echo "Backing up to: ${server}"

          export RESTIC_REPOSITORY="rest:https://restic:$(cat /tmp/${server})@restic.${server}/${config.networking.hostName}-${name}"

          #create repo if it not exists
          restic snapshots || restic init

          #backup files
          restic backup ${escapeShellArgs (((backup.paths ++ optional (backup.postgresDatabases != []) "/tmp/postgresDatabases") ++ optional (backup.sqliteDatabases != []) "/tmp/sqliteDatabases") ++ optional (backup.influxBuckets != []) "/tmp/influxBuckets") }

          restic check
        '') backup.targets;

      }
    ) backups;

    systemd.timers = mapAttrs' (
      name: backup: nameValuePair "restic-backup-${name}" {
        wantedBy    = [ "timers.target" ];
        timerConfig = backup.timerConfig;
      }
    ) backups;

  };

}