Guest User

Untitled

a guest
Feb 23rd, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. #/usr/bin/env bash
  2.  
  3. share_volume="<zfs base-dataset>" # ex. /trunk
  4. target_host="<user>@<target_host>" # ex. root@fileserver
  5.  
  6. keep_snaps=$((24*4))
  7.  
  8. for path in $(zfs list -o name -t filesystem -r "$share_volume" | tail -n +2)
  9. do
  10. echo "=> Volume: $path";
  11. snapname=$(zfs list -t snapshot -o name,creation -s creation -d 1 "$path" | tail -n +2 | grep 'frequent' | tail -n 1 | awk '{print $1}' 2>&1)
  12. dataset=$(echo $snapname | cut -d "@" -f 1)
  13. echo "Snapshot: $snapname"
  14.  
  15. last_snapshot=$(ssh "$target_host" "(zfs list -o name -t filesystem -d 1 \"$path\" | tail -n +2) 2>&1")
  16. echo "Remote Filesystem: $last_snapshot"
  17. echo
  18.  
  19. if [[ "$last_snapshot" =~ "dataset does not exist" ]]; then
  20. echo "- Dataset $dataset not found ... creating"
  21. zfs send $snapname | gzip | ssh "$target_host" "gzip -d | zfs recv \"$dataset\""
  22. else
  23. lastremote=$(ssh "$target_host" "(zfs list -t snapshot -o name,creation -s creation -d 1 $path | tail -n +2 | grep 'frequent' | tail -n 1 | awk '{print \$1}' | cut -d '@' -f 2) 2>&1")
  24. echo "Last Remote: $lastremote"
  25.  
  26. localref=$(zfs list "$dataset@$lastremote" | tail -n +2 | awk '{print $1}')
  27.  
  28. if [ "$dataset@$lastremote" == "$snapname" ]; then
  29. echo "Replication already up-to-date ... skipping"
  30. echo
  31. continue
  32. fi
  33.  
  34. if [[ "$lastremote" =~ "no datasets available" ]]; then
  35. echo "No Snapshot for reference found ... doing full replication"
  36. zfs send "$snapname" | gzip | ssh "$target_host" "gzip -d | zfs recv -F \"$dataset\""
  37. else
  38. echo "Doing incremental replication ..."
  39.  
  40. if zfs send -i "$lastremote" "$snapname" | gzip | ssh "$target_host" "gzip -d | zfs recv -F \"$dataset\""
  41. then
  42. echo "Replicated successfully"
  43. else
  44. echo "!! Failed! ... continuing"
  45. continue
  46. fi
  47. fi
  48. fi
  49.  
  50. echo "Cleanup remote snapshots (keep last $keep_snaps) ..."
  51. ssh "$target_host" "zfs list -t snapshot -d 1 \"$path\" | grep frequent | sort -r | tail -n \"+$keep_snaps\" | sort -r | awk '{print \$1}'| xargs -I {} bash -c 'echo \"Destroying \$1\"; zfs destroy \"\$1\";' _ {}"
  52.  
  53. echo
  54. done
  55.  
  56. echo "DONE"
Add Comment
Please, Sign In to add comment