backup-local.nix (948B)
1 { 2 pkgs, 3 ... 4 }: let 5 backup-local = pkgs.writeShellScriptBin "backup-local" '' 6 # variables 7 mount_points=("/tank/" "/usb/") 8 report_file=("''${mount_points[1]}reports/$(date +"%Y-%m-%d_%H-%M").log") 9 # checks 10 for dir in "''${mount_points[@]}"; do 11 if ! findmnt -r "$dir" > /dev/null; then 12 echo "Error: $dir is not mounted. Exiting." >&2 13 exit 1 14 fi 15 done 16 if [ ! -w "''${mount_points[1]}" ]; then 17 echo "Error: No write permissions for destination. Exiting." >&2 18 exit 1 19 fi 20 printf "Mirroring from %s to %s.\n" "''${mount_points[0]}" "''${mount_points[1]}" 21 read -n1 -r -p "Press any key to begin..." key 22 # process 23 mkdir -p "$(dirname "$report_file")" 24 ${pkgs.rsync}/bin/rsync -avhP --delete --exclude=lost+found/ --exclude=reports/ "''${mount_points[0]}" "''${mount_points[1]}" 2>&1 | tee "$report_file" 25 ''; 26 in { 27 environment.systemPackages = [backup-local]; 28 }