Added code to fallback to lists of snapshots to find common point
[zfs-nexenta/.git] / zfs-replicate
1 #/bin/bash
2
3 # Usage: replicate <hostname> <zfs filesystem>
4 remote=$1
5 remote_fs=$2
6 remote_pool=${2%%/*}
7 remote_lockdir="/tmp/zfs-admin-lock"
8
9 # Set the name of the local pool used to store the backup of the remote
10 local_pool=backups
11
12 # Set the email address to send notification to
13 mailto=alan@pippins.net
14
15 # The ssh connection doesn't find zfs without this.
16 zfs=/usr/sbin/zfs
17
18 # Make sure we have valid arguments
19 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
20   echo "Usage: $0 <hostname> <zfs filesystem>"
21   exit 1
22 fi
23
24 # Make sure the local pool and local receiving filesystem exist, or print some errors
25 zpool list -H "$local_pool" >/dev/null 2>&1
26 if [ $? != 0 ]; then
27   echo >&2 "-E- The local pool, '$local_pool' doesn't seem to exist."
28   exit 1
29 fi
30 zfs list "$local_pool/$remote_pool" >/dev/null 2>&1
31 if [ $? != 0 ]; then
32   echo >&2 "-I- The local filesystem for the remote pool, '$local_pool/$remote_pool' doesn't seem to exist."
33   echo >&2 "    Creating the local filesystem to receive the remote pool into: $local_pool/$remote_pool"
34   $zfs create $local_pool/$remote_pool
35   if [ $? != 0 ]; then
36     echo "-E- remote $zfs create command failed"
37     exit 1
38   fi
39 fi
40
41 # Obtain the zpool guid for the local pool
42 local_pool_guid=`zpool get guid $local_pool 2>&1 | grep $local_pool | awk '{ print $3 }'`
43 zpool get guid $local_pool > /dev/null 2>&1
44 if [ $? != 0 ]; then
45   echo >&2 "-E- Unable to extract the guid for the local pool: $local_pool"
46   exit 1
47 fi
48
49 # Turn on shell verbosity
50 set -x
51
52 # Create the remote lockdir before continuing with the replicate
53 # Spinlock on creating the lock
54 maxsleeptime=60
55 maxattempts=100
56 attempts=0
57 while true; do
58   ssh $remote mkdir "$remote_lockdir" >/dev/null 2>&1
59   if [ $? != 0 ]; then
60     # Another zfs admin tool is running.
61     # Wait a random amount of time and try again
62     ransleep=$(($RANDOM % $maxsleeptime))
63     sleep $ransleep
64     ((attempts=attempts+1))
65   else 
66     # No other zfs admin tool is running, we can now.
67     break
68   fi
69   if [[ $attempts -gt $maxattempts ]]; then
70     # We've exceeded our maximum while loop count
71     echo "-W- The zfs filesystem has been locked down. Skipping replicate operation."
72     ssh $remote ls -ld $remote_lockdir | /usr/bin/mailx -s "zfs-replicate-all unable to obtain zfs admin lock" $mailto
73     exit 1
74   fi
75 done
76
77 # Declare a cleanup() method to remove the remote lockdir
78 cleanup() { ssh $remote rm -rf "$remote_lockdir"; }
79 trap cleanup EXIT
80
81 # Setup our backup marker names
82 current_backup_marker=${remote_fs}@current-backup-${local_pool_guid}
83 previous_backup_marker=${remote_fs}@previous-backup-${local_pool_guid}
84
85 # List the snapshots on the remote machine.
86 remote_list=$(mktemp /tmp/replicate.XXXXXX)
87 ssh $remote \
88     $zfs list -H -t snapshot |
89     grep ^${remote_fs}@ |
90     awk '{print$1}' > $remote_list
91 if [ $? != 0 ]; then
92   echo "-E- remote $zfs list command failed"
93   exit 1
94 fi
95
96 # List the snapshots on the local machine.
97 local_list=$(mktemp /tmp/replicate.XXXXXX)
98 $zfs list -H -t snapshot |
99     grep ^${local_pool}/${remote_fs}@ |
100     awk "{gsub(/^$local_pool./,\"\",\$1); print\$1}" > $local_list
101 if [ $? != 0 ]; then
102   echo "-E- local $zfs list command failed"
103   exit 1
104 fi
105
106 # Destroy the current backup marker snapshot on the remote system if it exists
107 grep -q ${current_backup_marker} $remote_list
108 if [ $? == 0 ]; then
109   ssh $remote $zfs destroy ${current_backup_marker} 
110   if [ $? != 0 ]; then
111     echo "-E- remote $zfs destroy command failed"
112     exit 1
113   fi
114 fi
115
116 # Create the current backup marker snapshot on the remote system
117 ssh $remote $zfs snapshot ${current_backup_marker}
118 if [ $? != 0 ]; then
119   echo "-E- remote $zfs snapshot command failed"
120   exit 1
121 fi
122
123 # Check to see if the previous backup marker exists in the remote snapshot list.
124 # Check to see if the previous backup marker exists in the local snapshot list.
125 # If the previous backup markers exists, perform an incremental replicate. Else:
126 # 1) check to see if a common snapshot exists, and perform an incremental replicate.
127 # 2) if no common snapshot exists, destroy the local filesystem, and perform a full replicate.
128 grep -q ${previous_backup_marker} $remote_list
129 no_markers=$?
130 grep -q ${previous_backup_marker} $local_list
131 no_markers=$(($no_markers || $?))
132
133 if [ $no_markers == 0 ]; then
134   # We found backup markers, incrementally send the new snaps
135
136   # First, rollback the local pool to the previous backup marker in case the previous
137   # backup was interrupted for some reason. If we don't do this, the zfs send -R command
138   # below may complain about snaps already existing as it tries to resend from the 
139   # previous backup marker again from a previously interrupted replicate.
140   $zfs rollback -r ${local_pool}/${previous_backup_marker} 
141   if [ $? != 0 ]; then
142     echo "-E- remote incremental $zfs rollback command failed"
143     exit 1
144   fi
145   # Now it should be safe to send the snaps
146   ssh $remote $zfs send -R -I${previous_backup_marker} ${current_backup_marker} | 
147       $zfs receive -vF -d ${local_pool}/${remote_pool}
148   if [ $? != 0 ]; then
149     echo "-E- remote incremental $zfs send command failed"
150     exit 1
151   fi
152 else
153   # We didn't find any backup markers, next check to see if we have a common snapshot.
154
155   # See what the most recent snapshot on the remote end is.
156   latest=$(tail -n 1 $remote_list)
157
158   # I did this to make sure that diff would always display the most recent common
159   # Since we're keying off the context of the diff, we need to ensure we will get context
160   # by injecting a known difference in case no others exist in the lists.
161   echo bogus.remote >> $remote_list
162   echo bogus.local  >> $local_list
163   common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
164
165   if [[ -n "$common" ]]; then
166     # We found a common snapshot, incrementally send the new snaps
167     ssh $remote $zfs send -R -I${common/*@/@} ${current_backup_marker} |
168         $zfs receive -vF -d ${local_pool}/${remote_pool}
169     if [ $? != 0 ]; then
170       echo "-E- remote incremental $zfs send command failed"
171       exit 1
172     fi
173   else
174     # We did not find any markers or a common snapshot
175     # At this point, we'll have to send the entire filesystem
176     # Destroy the local filesystem if it exists before receving the full replicate
177     zfs list ${local_pool}/${remote_fs} > /dev/null 2>&1
178     if [ $? == 0 ]; then
179       zfs destroy -r ${local_pool}/${remote_fs}
180       if [ $? != 0 ]; then
181         echo "-E- remote full $zfs destroy command failed"
182         exit 1
183       fi
184     fi
185     # Send the full filesystem
186     ssh $remote $zfs send -R ${current_backup_marker} |
187         $zfs receive -vF -d ${local_pool}/${remote_pool}
188     if [ $? != 0 ]; then
189       echo "-E- remote full $zfs send command failed"
190       exit 1
191     fi
192   fi
193 fi
194  
195 # destroy the previous backup markers now that we've replicated past them
196 # don't check the return codes here because these may not exist, and that is ok
197 $zfs destroy ${local_pool}/${previous_backup_marker} > /dev/null 2>&1
198 ssh $remote $zfs destroy ${previous_backup_marker} > /dev/null 2>&1
199
200 # Rename the current backup marker to be the previous backup marker
201 $zfs rename ${local_pool}/${current_backup_marker} ${local_pool}/${previous_backup_marker}
202 if [ $? != 0 ]; then
203   echo "-E- local $zfs rename command failed"
204   exit 1
205 fi
206 ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
207 if [ $? != 0 ]; then
208   echo "-E- remote $zfs rename command failed"
209   exit 1
210 fi
211    
212 # Remove tmp files
213 rm -f $local_list $remote_list
214