nix-configs

Personal NixOS and home-manager configuration files
Log | Files | Refs

restic.nix (1332B)


      1 { pkgs, vars, ... }:
      2 
      3 let
      4   backup-cloud = pkgs.writeShellScriptBin "backup-cloud" ''
      5       # variables
      6       directories=( "$HOME/docker/" "$HOME/vault/" )
      7       excludes=( "*.flac" "*.jpg" "*.jpeg" "*.mp4" "*.webm" "*.mkv")
      8       exclude_args=()
      9       for ext in "''${excludes[@]}"; do
     10         exclude_args+=("--exclude=$ext")
     11       done
     12       # process
     13       source "$HOME/vault/docs/secure/restic.env"
     14       # Directory loop
     15       for dir in "''${directories[@]}"; do
     16         if [[ -d "$dir" ]]; then
     17           echo "Directory exists: $dir"
     18           ${pkgs.restic}/bin/restic backup "$dir" "''${exclude_args[@]}"
     19         else
     20           echo "Directory does not exist: $dir"
     21         fi
     22       done
     23   '';
     24 in {
     25   environment.systemPackages = [ backup-cloud ];
     26 
     27   systemd.services.backup-cloud = {
     28     description = "Restic cloud backup";
     29     serviceConfig = {
     30       Type = "oneshot";
     31       User = vars.user.username;
     32       ExecStart = "${backup-cloud}/bin/backup-cloud";
     33       Environment = "PATH=/run/current-system/sw/bin:/run/wrappers/bin";
     34     };
     35   };
     36 
     37   systemd.timers.backup-cloud = {
     38     description = "Run backup-cloud every 12 hours at a random offset";
     39     wantedBy = [ "timers.target" ];
     40     timerConfig = {
     41       OnCalendar = "*-*-* 00/12:00:00";
     42       RandomizedDelaySec = "6h";
     43       Persistent = true;
     44     };
     45   };
     46 }