git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #78886 from Mic92/restic-fixes

Restic fixes: pruning, process substitution (take 2)

commit341241b1c863c6deb361d681b68ff1cbbc71b015 authorJörg Thalheim <Mic92@users.noreply.github.com> dateFri 7 Feb 2020, 14:14 (7 years ago) parent862105d4472071
4 files changed+117−7
nixos/modules/services/backup/restic.nix+50−6 nixos/tests/all-tests.nix+1 nixos/tests/restic.nix+63 pkgs/tools/backup/restic/default.nix+3−1
nixos/modules/services/backup/restic.nix+50−6
@@ -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;
nixos/tests/all-tests.nix+1
@@ -248,6 +248,7 @@ in
248248 radicale = handleTest ./radicale.nix {};
249249 redis = handleTest ./redis.nix {};
250250 redmine = handleTest ./redmine.nix {};
251+ restic = handleTest ./restic.nix {};
251252 roundcube = handleTest ./roundcube.nix {};
252253 rspamd = handleTest ./rspamd.nix {};
253254 rss2email = handleTest ./rss2email.nix {};
nixos/tests/restic.nixnew file+63
@@ -0,0 +1,63 @@
1+import ./make-test-python.nix (
2+ { pkgs, ... }:
3+
4+ let
5+ password = "some_password";
6+ repository = "/tmp/restic-backup";
7+ passwordFile = pkgs.writeText "password" "correcthorsebatterystaple";
8+ in
9+ {
10+ name = "restic";
11+
12+ meta = with pkgs.stdenv.lib.maintainers; {
13+ maintainers = [ bbigras ];
14+ };
15+
16+ nodes = {
17+ server =
18+ { ... }:
19+ {
20+ services.restic.backups = {
21+ remotebackup = {
22+ inherit repository;
23+ passwordFile = "${passwordFile}";
24+ initialize = true;
25+ paths = [ "/opt" ];
26+ pruneOpts = [
27+ "--keep-daily 2"
28+ "--keep-weekly 1"
29+ "--keep-monthly 1"
30+ "--keep-yearly 99"
31+ ];
32+ };
33+ };
34+ };
35+ };
36+
37+ testScript = ''
38+ server.start()
39+ server.wait_for_unit("dbus.socket")
40+ server.fail(
41+ "${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots"
42+ )
43+ server.succeed(
44+ "mkdir -p /opt",
45+ "touch /opt/some_file",
46+ "timedatectl set-time '2016-12-13 13:45'",
47+ "systemctl start restic-backups-remotebackup.service",
48+ '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^1 snapshot"',
49+ "timedatectl set-time '2017-12-13 13:45'",
50+ "systemctl start restic-backups-remotebackup.service",
51+ "timedatectl set-time '2018-12-13 13:45'",
52+ "systemctl start restic-backups-remotebackup.service",
53+ "timedatectl set-time '2018-12-14 13:45'",
54+ "systemctl start restic-backups-remotebackup.service",
55+ "timedatectl set-time '2018-12-15 13:45'",
56+ "systemctl start restic-backups-remotebackup.service",
57+ "timedatectl set-time '2018-12-16 13:45'",
58+ "systemctl start restic-backups-remotebackup.service",
59+ '${pkgs.restic}/bin/restic -r ${repository} -p ${passwordFile} snapshots -c | grep -e "^4 snapshot"',
60+ )
61+ '';
62+ }
63+)
pkgs/tools/backup/restic/default.nix+3−1
@@ -1,4 +1,4 @@
1−{ lib, buildGoPackage, fetchFromGitHub }:
1+{ lib, buildGoPackage, fetchFromGitHub, nixosTests }:
22
33buildGoPackage rec {
44 pname = "restic";
@@ -18,6 +18,8 @@ buildGoPackage rec {
1818 go run build.go
1919 '';
2020
21+ passthru.tests.restic = nixosTests.restic;
22+
2123 installPhase = ''
2224 mkdir -p \
2325 $bin/bin \