@@ -103,6 +103,34 @@ in
103103 Create the repository if it doesn't exist.
104104 '';
105105 };
106+
107+ pruneOpts = mkOption {
108+ type = types.listOf types.str;
109+ default = [];
110+ description = ''
111+ A list of options (--keep-* et al.) for 'restic forget
112+ --prune', to automatically prune old snapshots. The
113+ 'forget' command is run *after* the 'backup' command, so
114+ keep that in mind when constructing the --keep-* options.
115+ '';
116+ example = [
117+ "--keep-daily 7"
118+ "--keep-weekly 5"
119+ "--keep-monthly 12"
120+ "--keep-yearly 75"
121+ ];
122+ };
123+
124+ dynamicFilesFrom = mkOption {
125+ type = with types; nullOr str;
126+ default = null;
127+ description = ''
128+ A script that produces a list of files to back up. The
129+ results of this command are given to the '--files-from'
130+ option.
131+ '';
132+ example = "find /home/matt/git -type d -name .git";
133+ };
106134 };
107135 }));
108136 default = {};
@@ -134,25 +162,41 @@ in
134162 let
135163 extraOptions = concatMapStrings (arg: " -o ${arg}") backup.extraOptions;
136164 resticCmd = "${pkgs.restic}/bin/restic${extraOptions}";
165+ filesFromTmpFile = "/run/restic-backups-${name}/includes";
166+ backupPaths = if (backup.dynamicFilesFrom == null)
167+ then concatStringsSep " " backup.paths
168+ else "--files-from ${filesFromTmpFile}";
169+ pruneCmd = optionals (builtins.length backup.pruneOpts > 0) [
170+ ( resticCmd + " forget --prune " + (concatStringsSep " " backup.pruneOpts) )
171+ ( resticCmd + " check" )
172+ ];
137173 in nameValuePair "restic-backups-${name}" ({
138174 environment = {
139175 RESTIC_PASSWORD_FILE = backup.passwordFile;
140176 RESTIC_REPOSITORY = backup.repository;
141177 };
142− path = with pkgs; [
143− openssh
144− ];
178+ path = [ pkgs.openssh ];
145179 restartIfChanged = false;
146180 serviceConfig = {
147181 Type = "oneshot";
148− ExecStart = "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${concatStringsSep " " backup.paths}";
182+ ExecStart = [ "${resticCmd} backup ${concatStringsSep " " backup.extraBackupArgs} ${backupPaths}" ] ++ pruneCmd;
149183 User = backup.user;
184+ RuntimeDirectory = "restic-backups-${name}";
150185 } // optionalAttrs (backup.s3CredentialsFile != null) {
151186 EnvironmentFile = backup.s3CredentialsFile;
152187 };
153− } // optionalAttrs backup.initialize {
188+ } // optionalAttrs (backup.initialize || backup.dynamicFilesFrom != null) {
154189 preStart = ''
155− ${resticCmd} snapshots || ${resticCmd} init
190+ ${optionalString (backup.initialize) ''
191+ ${resticCmd} snapshots || ${resticCmd} init
192+ ''}
193+ ${optionalString (backup.dynamicFilesFrom != null) ''
194+ ${pkgs.writeScript "dynamicFilesFromScript" backup.dynamicFilesFrom} > ${filesFromTmpFile}
195+ ''}
196+ '';
197+ } // optionalAttrs (backup.dynamicFilesFrom != null) {
198+ postStart = ''
199+ rm ${filesFromTmpFile}
156200 '';
157201 })
158202 ) config.services.restic.backups;