1{ config, lib, pkgs, ... }: 2 3with lib; 4 5let 6 cfg = config.services.tarsnap; 7 8 configFile = name: cfg: '' 9 cachedir ${config.services.tarsnap.cachedir}/${name} 10 keyfile ${cfg.keyfile} 11 ${optionalString cfg.nodump "nodump"} 12 ${optionalString cfg.printStats "print-stats"} 13 ${optionalString cfg.printStats "humanize-numbers"} 14 ${optionalString (cfg.checkpointBytes != null) ("checkpoint-bytes "+cfg.checkpointBytes)} 15 ${optionalString cfg.aggressiveNetworking "aggressive-networking"} 16 ${concatStringsSep "\n" (map (v: "exclude "+v) cfg.excludes)} 17 ${concatStringsSep "\n" (map (v: "include "+v) cfg.includes)} 18 ${optionalString cfg.lowmem "lowmem"} 19 ${optionalString cfg.verylowmem "verylowmem"} 20 ${optionalString (cfg.maxbw != null) ("maxbw "+toString cfg.maxbw)} 21 ${optionalString (cfg.maxbwRateUp != null) ("maxbw-rate-up "+toString cfg.maxbwRateUp)} 22 ${optionalString (cfg.maxbwRateDown != null) ("maxbw-rate-down "+toString cfg.maxbwRateDown)} 23 ''; 24in 25{ 26 options = { 27 services.tarsnap = { 28 enable = mkOption { 29 type = types.bool; 30 default = false; 31 description = '' 32 Enable periodic tarsnap backups. 33 ''; 34 }; 35 36 keyfile = mkOption { 37 type = types.str; 38 default = "/root/tarsnap.key"; 39 description = '' 40 The keyfile which associates this machine with your tarsnap 41 account. 42 Create the keyfile with <command>tarsnap-keygen</command>. 43 44 Note that each individual archive (specified below) may also have its 45 own individual keyfile specified. Tarsnap does not allow multiple 46 concurrent backups with the same cache directory and key (starting a 47 new backup will cause another one to fail). If you have multiple 48 archives specified, you should either spread out your backups to be 49 far apart, or specify a separate key for each archive. By default 50 every archive defaults to using 51 <literal>"/root/tarsnap.key"</literal>. 52 53 It's recommended for backups that you generate a key for every archive 54 using <literal>tarsnap-keygen(1)</literal>, and then generate a 55 write-only tarsnap key using <literal>tarsnap-keymgmt(1)</literal>, 56 and keep your master key(s) for a particular machine off-site. 57 58 The keyfile name should be given as a string and not a path, to 59 avoid the key being copied into the Nix store. 60 ''; 61 }; 62 63 cachedir = mkOption { 64 type = types.nullOr types.path; 65 default = "/var/cache/tarsnap"; 66 description = '' 67 The cache allows tarsnap to identify previously stored data 68 blocks, reducing archival time and bandwidth usage. 69 70 Should the cache become desynchronized or corrupted, tarsnap 71 will refuse to run until you manually rebuild the cache with 72 <command>tarsnap --fsck</command>. 73 74 Note that each individual archive (specified below) has its own cache 75 directory specified under <literal>cachedir</literal>; this is because 76 tarsnap locks the cache during backups, meaning multiple services 77 archives cannot be backed up concurrently or overlap with a shared 78 cache. 79 80 Set to <literal>null</literal> to disable caching. 81 ''; 82 }; 83 84 archives = mkOption { 85 type = types.attrsOf (types.submodule ( 86 { 87 options = { 88 keyfile = mkOption { 89 type = types.str; 90 default = config.services.tarsnap.keyfile; 91 description = '' 92 Set a specific keyfile for this archive. This defaults to 93 <literal>"/root/tarsnap.key"</literal> if left unspecified. 94 95 Use this option if you want to run multiple backups 96 concurrently - each archive must have a unique key. You can 97 generate a write-only key derived from your master key (which 98 is recommended) using <literal>tarsnap-keymgmt(1)</literal>. 99 100 Note: every archive must have an individual master key. You 101 must generate multiple keys with 102 <literal>tarsnap-keygen(1)</literal>, and then generate write 103 only keys from those. 104 105 The keyfile name should be given as a string and not a path, to 106 avoid the key being copied into the Nix store. 107 ''; 108 }; 109 110 nodump = mkOption { 111 type = types.bool; 112 default = true; 113 description = '' 114 Exclude files with the <literal>nodump</literal> flag. 115 ''; 116 }; 117 118 printStats = mkOption { 119 type = types.bool; 120 default = true; 121 description = '' 122 Print global archive statistics upon completion. 123 The output is available via 124 <command>systemctl status tarsnap@archive-name</command>. 125 ''; 126 }; 127 128 checkpointBytes = mkOption { 129 type = types.nullOr types.str; 130 default = "1GB"; 131 description = '' 132 Create a checkpoint every <literal>checkpointBytes</literal> 133 of uploaded data (optionally specified using an SI prefix). 134 135 1GB is the minimum value. A higher value is recommended, 136 as checkpointing is expensive. 137 138 Set to <literal>null</literal> to disable checkpointing. 139 ''; 140 }; 141 142 period = mkOption { 143 type = types.str; 144 default = "01:15"; 145 example = "hourly"; 146 description = '' 147 Create archive at this interval. 148 149 The format is described in 150 <citerefentry><refentrytitle>systemd.time</refentrytitle> 151 <manvolnum>7</manvolnum></citerefentry>. 152 ''; 153 }; 154 155 aggressiveNetworking = mkOption { 156 type = types.bool; 157 default = false; 158 description = '' 159 Upload data over multiple TCP connections, potentially 160 increasing tarsnap's bandwidth utilisation at the cost 161 of slowing down all other network traffic. Not 162 recommended unless TCP congestion is the dominant 163 limiting factor. 164 ''; 165 }; 166 167 directories = mkOption { 168 type = types.listOf types.path; 169 default = []; 170 description = "List of filesystem paths to archive."; 171 }; 172 173 excludes = mkOption { 174 type = types.listOf types.str; 175 default = []; 176 description = '' 177 Exclude files and directories matching these patterns. 178 ''; 179 }; 180 181 includes = mkOption { 182 type = types.listOf types.str; 183 default = []; 184 description = '' 185 Include only files and directories matching these 186 patterns (the empty list includes everything). 187 188 Exclusions have precedence over inclusions. 189 ''; 190 }; 191 192 lowmem = mkOption { 193 type = types.bool; 194 default = false; 195 description = '' 196 Reduce memory consumption by not caching small files. 197 Possibly beneficial if the average file size is smaller 198 than 1 MB and the number of files is lower than the 199 total amount of RAM in KB. 200 ''; 201 }; 202 203 verylowmem = mkOption { 204 type = types.bool; 205 default = false; 206 description = '' 207 Reduce memory consumption by a factor of 2 beyond what 208 <literal>lowmem</literal> does, at the cost of significantly 209 slowing down the archiving process. 210 ''; 211 }; 212 213 maxbw = mkOption { 214 type = types.nullOr types.int; 215 default = null; 216 description = '' 217 Abort archival if upstream bandwidth usage in bytes 218 exceeds this threshold. 219 ''; 220 }; 221 222 maxbwRateUp = mkOption { 223 type = types.nullOr types.int; 224 default = null; 225 example = literalExample "25 * 1000"; 226 description = '' 227 Upload bandwidth rate limit in bytes. 228 ''; 229 }; 230 231 maxbwRateDown = mkOption { 232 type = types.nullOr types.int; 233 default = null; 234 example = literalExample "50 * 1000"; 235 description = '' 236 Download bandwidth rate limit in bytes. 237 ''; 238 }; 239 }; 240 } 241 )); 242 243 default = {}; 244 245 example = literalExample '' 246 { 247 nixos = 248 { directories = [ "/home" "/root/ssl" ]; 249 }; 250 251 gamedata = 252 { directories = [ "/var/lib/minecraft "]; 253 period = "*:30"; 254 }; 255 } 256 ''; 257 258 description = '' 259 Tarsnap archive configurations. Each attribute names an archive 260 to be created at a given time interval, according to the options 261 associated with it. When uploading to the tarsnap server, 262 archive names are suffixed by a 1 second resolution timestamp. 263 264 For each member of the set is created a timer which triggers the 265 instanced <literal>tarsnap@</literal> service unit. You may use 266 <command>systemctl start tarsnap@archive-name</command> to 267 manually trigger creation of <literal>archive-name</literal> at 268 any time. 269 ''; 270 }; 271 }; 272 }; 273 274 config = mkIf cfg.enable { 275 assertions = 276 (mapAttrsToList (name: cfg: 277 { assertion = cfg.directories != []; 278 message = "Must specify paths for tarsnap to back up"; 279 }) cfg.archives) ++ 280 (mapAttrsToList (name: cfg: 281 { assertion = !(cfg.lowmem && cfg.verylowmem); 282 message = "You cannot set both lowmem and verylowmem"; 283 }) cfg.archives); 284 285 systemd.services."tarsnap@" = { 286 description = "Tarsnap archive '%i'"; 287 requires = [ "network-online.target" ]; 288 after = [ "network-online.target" ]; 289 290 path = [ pkgs.iputils pkgs.tarsnap pkgs.coreutils ]; 291 292 # In order for the persistent tarsnap timer to work reliably, we have to 293 # make sure that the tarsnap server is reachable after systemd starts up 294 # the service - therefore we sleep in a loop until we can ping the 295 # endpoint. 296 preStart = "while ! ping -q -c 1 betatest-server.tarsnap.com &> /dev/null; do sleep 3; done"; 297 scriptArgs = "%i"; 298 script = '' 299 mkdir -p -m 0755 ${dirOf cfg.cachedir} 300 mkdir -p -m 0700 ${cfg.cachedir} 301 chown root:root ${cfg.cachedir} 302 chmod 0700 ${cfg.cachedir} 303 mkdir -p -m 0700 ${cfg.cachedir}/$1 304 DIRS=`cat /etc/tarsnap/$1.dirs` 305 exec tarsnap --configfile /etc/tarsnap/$1.conf -c -f $1-$(date +"%Y%m%d%H%M%S") $DIRS 306 ''; 307 308 serviceConfig = { 309 IOSchedulingClass = "idle"; 310 NoNewPrivileges = "true"; 311 CapabilityBoundingSet = "CAP_DAC_READ_SEARCH"; 312 PermissionsStartOnly = "true"; 313 }; 314 }; 315 316 # Note: the timer must be Persistent=true, so that systemd will start it even 317 # if e.g. your laptop was asleep while the latest interval occurred. 318 systemd.timers = mapAttrs' (name: cfg: nameValuePair "tarsnap@${name}" 319 { timerConfig.OnCalendar = cfg.period; 320 timerConfig.Persistent = "true"; 321 wantedBy = [ "timers.target" ]; 322 }) cfg.archives; 323 324 environment.etc = 325 (mapAttrs' (name: cfg: nameValuePair "tarsnap/${name}.conf" 326 { text = configFile name cfg; 327 }) cfg.archives) // 328 (mapAttrs' (name: cfg: nameValuePair "tarsnap/${name}.dirs" 329 { text = concatStringsSep " " cfg.directories; 330 }) cfg.archives); 331 332 environment.systemPackages = [ pkgs.tarsnap ]; 333 }; 334}