Guest User

Untitled

a guest
Jul 19th, 2010
475
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.91 KB | None | 0 0
  1. #!/usr/bin/bash -p
  2. # ZFS snapshot replication script
  3. # Sections borrowed from/inspired by:
  4. # http://blogs.sun.com/constantin/entry/zfs_replicator_script_new_edition
  5. #
  6. # Modified and changed to meet my own needs
  7. # Brent Jones
  8. # http://www.brentrjones.com
  9. #
  10.  
  11. # Grab commandline argument variables
  12. sourcefs=$1
  13. destfsroot=$2
  14. RHOST=$3
  15. SHOST=$4
  16. PORT=$5
  17.  
  18. # Create a dest fs variable derived from source fs
  19. destfs=$destfsroot/`echo $sourcefs | cut -d/ -f2,3-`
  20.  
  21. # Establish a ZFS user property to store replication settings/locking
  22. repllock="replication:locked"
  23. replconfirmed="replication:confirmed"
  24. repldepend="replication:depend"
  25.  
  26. # Contact e-mail address
  27.  
  28. # Get a good date format from 'date' YYYYMMDD-HH:MM:SS
  29. snap=`date +%Y%m%d-%H:%M:%S`
  30.  
  31. # Define local and remote ZFS commands and SSH param
  32. LZFS="/sbin/zfs"
  33. RZFS="ssh $RHOST /sbin/zfs"
  34.  
  35. # Usage info
  36. help() {
  37. cat <<EOT
  38. usage: replicate.ksh [local filesystem] [remote target pool] [remote host address] [port (mbuffer)]
  39. eg. replicate.ksh tank remotetank 192.168.1.1 2020
  40. EOT
  41. }
  42.  
  43. if [ $# -lt 3 ]; then
  44. help
  45. exit 1
  46. fi
  47.  
  48. # Check the local and remote filesystems for locks from other jobs so we don't run multiples
  49. localfslocked=`$LZFS get -H $repllock $sourcefs | cut -f3`
  50. remotefslocked=`$RZFS get -H $repllock $destfs | cut -f3`
  51.  
  52. if [[ $localfslocked = "true" || $localfslocked = "true" ]];then
  53. echo "\nFilesystem locked, quitting: $sourcefs"
  54. echo "\nFilesystem locked" \
  55. "\n$sourcefs is locked: $localfslocked" \
  56. "\n$destfs is locked: $remotefslocked" \
  57. | mailx -s "Failed snapshot: $sourcefs" $contact;
  58. exit 1;
  59. else
  60. $LZFS set $repllock=true $sourcefs
  61. fi
  62.  
  63. # Get the most recent snapshot from localhost
  64. localprevsnap=`$LZFS list -Hr -o name -s creation -t snapshot $sourcefs | tail -1`
  65.  
  66. # Get the same variable and strip the root poolname out so we can work with it to compare later
  67. remoteprevsnap=`$LZFS list -Hr -o name -s creation -t snapshot $sourcefs | tail -1 | cut -d/ -f2,3-`
  68.  
  69. # Variable to give new snapshot name eg. tank@20090211-12:30:10 and give it a remote prefix
  70. newsnap="$sourcefs@$snap"
  71. remotenewsnap=$destfsroot/`echo $newsnap | cut -d/ -f2,3-`
  72.  
  73.  
  74.  
  75. # If the remote most recent snapshot doesnt match ours, give up
  76. # However if it does, attempt to send an incremental snapshot
  77. # from the last recent local snapshot, with the one just taken above
  78.  
  79. if [ -z "`$RZFS list -Ho name -s creation -t snapshot | grep $destfsroot/$remoteprevsnap`" ]; then
  80. echo "Snapshot continuity verification failed, manual intervention required"\
  81. "\nSource: $localprevsnap $newsnap"\
  82. "\nDestination: $RHOST - $destfsroot" \
  83. | mailx -s "Failed snapshot: $newsnap" $contact;
  84. else
  85. echo "Starting mbuffer on $RHOST on port $PORT\n"
  86. # Start mbuffer on remote side with flags to receive snapshot
  87. ssh -f -o HostKeyAlias=10.6.0.44 $RHOST "/usr/local/bin/mbuffer -q -I $SHOST:$PORT -m 1G -s 131072 | /sbin/zfs receive -vFd $destfsroot"
  88. echo "Snapshot continuity verification passed. Starting Replication Chain."
  89. echo "Taking snapshot: $LZFS snapshot $newsnap"
  90. # Take the snapshot now
  91. $LZFS snapshot $newsnap
  92. $LZFS send -pi $localprevsnap $newsnap | /usr/local/bin/mbuffer -q -O $RHOST:$PORT -m 1G -s 131072 > /dev/null 2>&1 || \
  93. {
  94. echo "Error when zfs send/receiving. Destroying $newsnap";
  95. $LZFS destroy $newsnap;
  96. echo "Failed snapshot replication" \
  97. "\nSource: $localprevsnap $newsnap" \
  98. "\nDestination: $RHOST - $destfsroot" \
  99. "\nDestroyed new snapshot: $newsnap" \
  100. | mailx -s "Failed snapshot: $newsnap" $contact;
  101. $LZFS set $repllock=false $sourcefs
  102. exit 1;
  103. }
  104. $LZFS set $repllock=false $sourcefs
  105. $LZFS set $replconfirmed=true $newsnap
  106. $LZFS set $repldepend=false $localprevsnap
  107. $LZFS set $repldepend=true $newsnap
  108. fi
Advertisement
Add Comment
Please, Sign In to add comment