Advertisement
Guest User

Untitled

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