restic.nix (1807B)
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 source "$HOME/vault/docs/secure/restic.env" 9 10 case "''${1:-backup}" in 11 prune) 12 echo "Running restic retention policy..." 13 ${pkgs.restic}/bin/restic forget \ 14 --group-by host,paths \ 15 --keep-daily 7 \ 16 --keep-weekly 4 \ 17 --keep-monthly 6 \ 18 --keep-yearly 2 \ 19 --prune 20 ;; 21 backup) 22 exclude_args=() 23 for ext in "''${excludes[@]}"; do 24 exclude_args+=("--exclude=$ext") 25 done 26 for dir in "''${directories[@]}"; do 27 if [[ -d "$dir" ]]; then 28 echo "Directory exists: $dir" 29 ${pkgs.restic}/bin/restic backup "$dir" "''${exclude_args[@]}" 30 else 31 echo "Directory does not exist: $dir" 32 fi 33 done 34 ;; 35 *) 36 echo "Usage: backup-cloud [backup|prune]" 37 exit 1 38 ;; 39 esac 40 ''; 41 in { 42 environment.systemPackages = [ backup-cloud ]; 43 44 systemd.services.backup-cloud = { 45 description = "Restic cloud backup"; 46 serviceConfig = { 47 Type = "oneshot"; 48 User = vars.user.username; 49 ExecStart = "${backup-cloud}/bin/backup-cloud"; 50 Environment = "PATH=/run/current-system/sw/bin:/run/wrappers/bin"; 51 }; 52 }; 53 54 systemd.timers.backup-cloud = { 55 description = "Run backup-cloud every 12 hours at a random offset"; 56 wantedBy = [ "timers.target" ]; 57 timerConfig = { 58 OnCalendar = "*-*-* 00/12:00:00"; 59 RandomizedDelaySec = "6h"; 60 Persistent = true; 61 }; 62 }; 63 }