git.iko.soy/typeable/nixpkgs
on master help sign in
Merge pull request #37897 from mkaito/tarsnap-symlinks-and-restore

Tarsnap: add symlink options and a restore service

commite61d69bfd3ce32bab30b3a35903153b3b9e2a3a4 authorYegor Timoshenko <yegortimoshenko@riseup.net> dateTue 27 Mar 2018, 16:21 (9 years ago) parent707a1cec515f70
1 file changed+60−7
nixos/modules/services/backup/tarsnap.nix+60−7
nixos/modules/services/backup/tarsnap.nix+60−7
@@ -238,6 +238,20 @@ in
238238 Whether to produce verbose logging output.
239239 '';
240240 };
241+ explicitSymlinks = mkOption {
242+ type = types.bool;
243+ default = false;
244+ description = ''
245+ Whether to follow symlinks specified as archives.
246+ '';
247+ };
248+ followSymlinks = mkOption {
249+ type = types.bool;
250+ default = false;
251+ description = ''
252+ Whether to follow all symlinks in archive trees.
253+ '';
254+ };
241255 };
242256 }
243257 ));
@@ -285,12 +299,12 @@ in
285299 }) gcfg.archives);
286300
287301 systemd.services =
288− mapAttrs' (name: cfg: nameValuePair "tarsnap-${name}" {
302+ (mapAttrs' (name: cfg: nameValuePair "tarsnap-${name}" {
289303 description = "Tarsnap archive '${name}'";
290304 requires = [ "network-online.target" ];
291305 after = [ "network-online.target" ];
292306
293− path = [ pkgs.iputils pkgs.tarsnap pkgs.utillinux ];
307+ path = with pkgs; [ iputils tarsnap utillinux ];
294308
295309 # In order for the persistent tarsnap timer to work reliably, we have to
296310 # make sure that the tarsnap server is reachable after systemd starts up
@@ -300,10 +314,12 @@ in
300314 while ! ping -q -c 1 v1-0-0-server.tarsnap.com &> /dev/null; do sleep 3; done
301315 '';
302316
303− script =
304− let run = ''tarsnap --configfile "/etc/tarsnap/${name}.conf" \
305− -c -f "${name}-$(date +"%Y%m%d%H%M%S")" \
317+ script = let
318+ tarsnap = ''tarsnap --configfile "/etc/tarsnap/${name}.conf"'';
319+ run = ''${tarsnap} -c -f "${name}-$(date +"%Y%m%d%H%M%S")" \
306320 ${optionalString cfg.verbose "-v"} \
321+ ${optionalString cfg.explicitSymlinks "-H"} \
322+ ${optionalString cfg.followSymlinks "-L"} \
307323 ${concatStringsSep " " cfg.directories}'';
308324 in if (cfg.cachedir != null) then ''
309325 mkdir -p ${cfg.cachedir}
@@ -313,7 +329,7 @@ in
313329 if [ ! -e ${cfg.cachedir}/firstrun ]; then
314330 ( flock 10
315331 flock -u 9
316− tarsnap --configfile "/etc/tarsnap/${name}.conf" --fsck
332+ ${tarsnap} --fsck
317333 flock 9
318334 ) 10>${cfg.cachedir}/firstrun
319335 fi
@@ -329,7 +345,44 @@ in
329345 CapabilityBoundingSet = [ "CAP_DAC_READ_SEARCH" ];
330346 PermissionsStartOnly = "true";
331347 };
332− }) gcfg.archives;
348+ }) gcfg.archives) //
349+
350+ (mapAttrs' (name: cfg: nameValuePair "tarsnap-restore-${name}"{
351+ description = "Tarsnap restore '${name}'";
352+ requires = [ "network-online.target" ];
353+
354+ path = with pkgs; [ iputils tarsnap utillinux ];
355+
356+ script = let
357+ tarsnap = ''tarsnap --configfile "/etc/tarsnap/${name}.conf"'';
358+ lastArchive = ''$(${tarsnap} --list-archives | sort | tail -1)'';
359+ run = ''${tarsnap} -x -f "${lastArchive}" ${optionalString cfg.verbose "-v"}'';
360+
361+ in if (cfg.cachedir != null) then ''
362+ mkdir -p ${cfg.cachedir}
363+ chmod 0700 ${cfg.cachedir}
364+
365+ ( flock 9
366+ if [ ! -e ${cfg.cachedir}/firstrun ]; then
367+ ( flock 10
368+ flock -u 9
369+ ${tarsnap} --fsck
370+ flock 9
371+ ) 10>${cfg.cachedir}/firstrun
372+ fi
373+ ) 9>${cfg.cachedir}/lockf
374+
375+ exec flock ${cfg.cachedir}/firstrun ${run}
376+ '' else "exec ${run}";
377+
378+ serviceConfig = {
379+ Type = "oneshot";
380+ IOSchedulingClass = "idle";
381+ NoNewPrivileges = "true";
382+ CapabilityBoundingSet = [ "CAP_DAC_READ_SEARCH" ];
383+ PermissionsStartOnly = "true";
384+ };
385+ }) gcfg.archives);
333386
334387 # Note: the timer must be Persistent=true, so that systemd will start it even
335388 # if e.g. your laptop was asleep while the latest interval occurred.