Guest User

Untitled

a guest
Jan 27th, 2013
79
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/bin/bash
  2. #
  3. # Author: Ryan Kernan
  4. # Date: September 23, 2011
  5. # Version: 1.1
  6. # Credits: Mike La Spina for the original concept and script http://blog.laspina.ca/
  7. #  
  8. # Mike La Spina - 1.1 added vars to support full paths on remote ssh executions
  9. # Mike La Spina - 1.2 added var symbol to last_snap_shost input parse.
  10. #
  11. # Function: Provides snapshot and send process which replicates ZFS file systems from a source to target server.
  12. # Maintains a runing snapshot archive for X days (X being the value of keep_snaps).
  13. #
  14. # The input format should conform to the following:
  15. # source zfspath, source host, dest host
  16. #
  17. # eg.
  18. #
  19. # pool/zfsstore,host1,host2
  20. #
  21. # Input parms:1
  22. # rep_list - a file name var of which the file contains a list of comma delimed zfs paths, source and target host names.
  23. # keep_snaps - Number of days to keep snaps for.
  24. # snap_list - temp file to list current snapshots.
  25. #
  26. #
  27. #
  28.  
  29. ZFS=/sbin/zfs
  30. DATE=/bin/date
  31. SSH=/usr/bin/ssh
  32.  
  33. PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/games:/usr/local/sbin:/usr/local/bin:/root/bin
  34.  
  35. rep_list=$1
  36. keep_snaps=7
  37. snap_list=snaplist.lst
  38.  
  39. if [ -e /tmp/zfs-rep.lock ];
  40. then
  41.     exit
  42. else
  43.     touch /tmp/zfs-rep.lock
  44. fi
  45.  
  46. #######################################################################################
  47. ####################################Function###########################################
  48. #######################################################################################
  49. #
  50. # Function parse the comma delimited input file and assign the field to the respective
  51. # variables for file system, pool and host names.
  52. #
  53. # Global output parms
  54. # zfspool
  55. # zfspath
  56. # shost
  57. # dhost
  58.  
  59. parse_rep_list() {
  60.  
  61.     rep_list_line=$1
  62.         zfspool="$(echo $rep_list_line | cut -d/ -f1)"
  63.         zfspath="$(echo $rep_list_line | cut -d, -f1)"
  64.         shost="$(echo $rep_list_line | cut -d, -f2)"
  65.         dhost="$(echo $rep_list_line | cut -d, -f3)"
  66.  
  67. }
  68.  
  69. #######################################################################################
  70. ####################################Function###########################################
  71. #######################################################################################
  72. #
  73. # Function issue zfs list commands and assign the variables the last snapshot names for
  74. # both the source and destination hosts.
  75. #
  76. # Global input parms
  77. # zfspath
  78. # shost
  79. # dhost
  80.  
  81. check_last_snap() {
  82.  
  83.     last_snap_dhost=""
  84.     last_snap_shost=""
  85.     last_snap_shost=$( $ZFS list -o name -t snapshot | grep $zfspath | tail -1 )
  86.     last_snap_dhost=$( $SSH -n $dhost $ZFS list -H -o name -r -t snapshot | grep $zfspath | tail -1 )
  87.     true
  88. }
  89.  
  90.  
  91. #######################################################################################
  92. ####################################Function###########################################
  93. #######################################################################################
  94. #
  95. # Function check if the destination $ZFS path exists and assign the result to the
  96. # variable dhost_fs_name.
  97. #
  98. # Global input parms
  99. # $zfspath
  100. # dhost
  101.  
  102. dhost_fs_exists() {
  103.  
  104.     dhost_fs_name=""
  105.     dhost_fs_name=$($SSH -n $dhost $ZFS list -o name -H $zfspath | tail -1)
  106.  
  107.     if [ "$dhost_fs_name" = "" ]
  108.     then
  109.         echo $($DATE) "->" $zfspath file system does not exist on target host $dhost. >> replicate.log
  110.     fi
  111.  
  112. }
  113.  
  114. #######################################################################################
  115. ####################################Function###########################################
  116. #######################################################################################
  117. #
  118. # Function create a zfs path on the destination to allow the receive command
  119. # funtionallity then issue zfs snap and send to transfer the zfs object to the
  120. # destination host
  121. #
  122. # Global input parms
  123. # zfspath
  124. # dhost
  125.  
  126. dhost_create_fs() {
  127.  
  128.     $SSH -n $dhost $ZFS create -p $zfspath
  129.     $SSH -n $dhost $ZFS set mountpoint=none $zfspath
  130.     last_snap_shost=$( $ZFS list -o name -t snapshot -H | grep "$zfspath\@" | tail -1 )
  131.     echo $($DATE) "->" $last_snap_shost Initial replication start. >> replicate.log
  132.     $ZFS send -v -R $last_snap_shost | $SSH $dhost $ZFS recv -v -F -d $zfspool
  133.     echo $($DATE) "->" $last_snap_shost Initial replication end. >> replicate.log
  134.  
  135. }
  136.  
  137. #######################################################################################
  138. ####################################Function###########################################
  139. #######################################################################################
  140. #
  141. # Function Issue a snapshot for the source zfs path
  142. #
  143. # Global input parms
  144. # zfspath
  145.  
  146. create_fs_snap() {
  147.  
  148.     snap_date="$($DATE +%m-%d-%y-%H:%M)"
  149.     echo $($DATE) "->" $zfspath@$snap_date Snapshot creation start. >> replicate.log
  150.     $ZFS snapshot $zfspath@$snap_date
  151.     echo $($DATE) "->" $zfspath@$snap_date Snapshot creation end. >> replicate.log
  152.  
  153. }
  154.  
  155. #######################################################################################
  156. ####################################Function###########################################
  157. #######################################################################################
  158. #
  159. # Function create a zfs send/recv command set based on a the zfs path source
  160. # and target hosts for an established replication state. (aka incremental replication)
  161. #
  162. # Global input parms
  163. # zfspath
  164. # dhost
  165.  
  166. incr_repl_fs() {
  167.  
  168.     echo $($DATE) "->" $last_snap_dhost $last_snap_shost Incremental send start. >> replicate.log
  169.     $ZFS send -I $last_snap_dhost $last_snap_shost | $SSH $dhost $ZFS recv -d -F $zfspool >> replicate.log
  170.     echo $($DATE) "->" $last_snap_dhost $last_snap_shost Incremental send end. >> replicate.log
  171.  
  172. }
  173.  
  174. #######################################################################################
  175. ####################################Function###########################################
  176. #######################################################################################
  177. #
  178. # Function to clean up snapshots that are older than X days old X being the
  179. # value set by "keep_snaps" on both the source and destination hosts.
  180.  
  181. clean_snaps() {
  182.  
  183.     PreviousSnapDate=""
  184.     PreviousSnap=""
  185.     CurrentYear="$( $DATE +%y )"
  186.     CurrentMonth="$( $DATE +%m )"
  187.     CurrentDay="$( $DATE +%d )"
  188.  
  189.     if [ "$zfspath" != "" ]
  190.     then
  191.             $ZFS list -o name -t snapshot | grep $zfspath\@ > $snap_list
  192.  
  193.         while read snaps
  194.         do
  195.  
  196.             SnapDate=`echo $snaps | cut -d @ -f 2`
  197.         SnapDay="$(echo $SnapDate | cut -d- -f2)"
  198.         SnapMonth="$(echo $SnapDate | cut -d- -f1)"  
  199.         SnapYear="$(echo $SnapDate | cut -d- -f3)"
  200.  
  201.         SnapDate="$SnapMonth-$SnapDay-$SnapYear"
  202.                
  203.             if [ "$($DATE -v-${keep_snaps}d +%m-%d-%y)" = "$SnapDate" ]
  204.         then
  205.             echo "Destroying $SnapDate snapshot $snaps on $shost" >> replicate.log
  206.                     $ZFS destroy $snaps
  207.             echo "Destroying $SnapDate snapshot $snaps on $dhost" >> replicate.log
  208.                     $SSH -n $dhost $ZFS destroy $snaps
  209.         fi
  210.  
  211.         done < $snap_list
  212.         rm $snap_list
  213.     fi
  214. }
  215.  
  216. #######################################################################################
  217. #####################################Main Entery#######################################
  218. #######################################################################################
  219. #
  220. #
  221. # Init Global Parms
  222.  
  223. zfspath=""
  224. shost=""
  225. dhost=""
  226.  
  227. # Create the snapshots all at the same time
  228.  
  229. while read line
  230. do
  231.     parse_rep_list $line
  232.  
  233.     if [ "$zfspath" != "" ]
  234.     then
  235.         create_fs_snap >> replicate.log     # Create a new snapshot of the path spec.
  236.     fi
  237.  
  238. done < $rep_list
  239.  
  240. # Send the snapshots to the remote and create the fs if required
  241.  
  242. while read line
  243. do
  244.  
  245.     parse_rep_list $line
  246.  
  247.     if [ "$zfspath" != "" ]
  248.     then
  249.         dhost_fs_exists                          # Test for the existence of our listed
  250.                                         # zfs file system path on the target host.
  251.  
  252.         if [ "$dhost_fs_name" ]
  253.         then
  254.             check_last_snap >> replicate.log           # Get the start and stop snaps.
  255.             incr_repl_fs >> replicate.log              # Initiate a dif replication.
  256.             clean_snaps                    # Clean up any snapshots that are X days old.
  257.         else
  258.             dhost_create_fs >> replicate.log            # Create a first time replication.
  259.             fi
  260.       fi
  261.  
  262. done < $rep_list
  263.  
  264. rm /tmp/zfs-rep.lock
  265.  
  266. exit 0
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×