02f9924ad6c8a7a2f48ded18ab10f60cf62da2df
[zfs-ubuntu/.git] / zfs-replicate
1 #!/bin/bash
2
3 # Author: Carl Baldwin & Alan Pippin
4 # Description: This script replicates a remote zfs filesystem to a local zfs pool.
5 #              This script will keep all snapshots in sync, removing the ones
6 #              that have been deleted since the last replicate was performed.
7 #              This script will only send the new, or missing, snapshots since
8 #              the last replicate was performed.
9 # Usage: replicate <hostname> <zfs filesystem>
10
11 # source our configuration
12 config="${0%/*}/zfs-scripts.conf"
13 [ -e "${config}.dist" ] && . ${config}.dist
14 [ -e "${config}" ] && . ${config}
15
16 # command line arg parsing
17 remote=$1
18 remote_fs=$2
19 remote_pool=${2%%/*}
20
21 # get the backup pool from the command line or the config file if not specified
22 if [[ -n $3 ]]; then
23     backup_pool=$3
24 else
25     backup_pool=${backup_pool%% *} # use the first backup pool if none specified
26 fi
27
28 # Setup our cleanup and exit trap
29 cleanup() {
30   if [[ -e "$local_list" ]]; then
31     rm -f $local_list
32   fi
33   if [[ -e "$remote_list" ]]; then
34     rm -f $remote_list
35   fi
36   $ssh $remote ls -d "$lockdir" > /dev/null 2>&1
37   if [[ $? == 0 ]]; then
38     $ssh $remote rm -rf "$lockdir"
39   fi
40 }
41 fatal_and_exit() {
42   echo -e 2>&1 "$1"
43   # Destroy the current backup markers from the local backup_pool and remote_pool if they exist
44   if [[ -n "$current_backup_marker" ]]; then
45     # Local backup pool current backup marker
46     $zfs list -t snapshot ${backup_pool}/${current_backup_marker} > /dev/null 2>&1
47     if [ $? == 0 ]; then
48       $zfs destroy ${backup_pool}/${current_backup_marker}
49     fi
50     # Remote pool current backup marker
51     $ssh $remote zfs list -t snapshot ${current_backup_marker} > /dev/null 2>&1
52     if [ $? == 0 ]; then
53       $ssh $remote $zfs destroy ${current_backup_marker}
54     fi
55   fi
56   # send email notification
57   if [[ -n "$2" ]]; then
58     echo -e "$1" | $mailx -s "zfs replicate on $hostname failed" "$2"
59   fi
60   # exit
61   exit 1
62 }
63 trap fatal_and_exit INT
64 trap cleanup EXIT
65
66 # Declare a function to handle the replicate operation
67 replicate() {
68   zfs_send="$1"
69   zfs_recv="$zfs receive -vF -d ${backup_pool}/${remote_pool}"
70   glue="$throttle $throttle_opt"
71   if [[ $throttle_enable == 1 && -e $throttle ]]; then
72     # handle using the glue in the local and remote host case properly
73     if [[ -z "$ssh" ]]; then
74       # local host glue case
75       $zfs_send | $glue | $zfs_recv 
76     else
77       # remote host glue case
78       $ssh $remote "$zfs_send | $glue" | $zfs_recv
79     fi
80   else 
81     # no glue case - works for both the local and remote host cases
82     $ssh $remote $zfs_send | $zfs_recv
83   fi
84   # The return code of the zfs_send | zfs_recv operation will be returned to the caller
85 }
86
87 # Make sure we have valid arguments
88 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
89   fatal_and_exit "Usage: $0 <hostname> <zfs filesystem>"
90 fi
91
92 # check for localhost
93 if [[ $remote = "localhost" ]]; then
94   remote=""
95   ssh=""
96 fi
97
98 # Make sure the local backup pool and local receiving filesystem exist, or print some errors
99 zpool list -H "$backup_pool" >/dev/null 2>&1
100 if [ $? != 0 ]; then
101   fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." $mailto
102 fi
103 zfs list "$backup_pool/$remote_pool" >/dev/null 2>&1
104 if [ $? != 0 ]; then
105   echo >&2 "-I- The local filesystem for the remote pool, '$backup_pool/$remote_pool' doesn't seem to exist."
106   echo >&2 "    Creating the local filesystem to receive the remote pool into: $backup_pool/$remote_pool"
107   $zfs create $backup_pool/$remote_pool
108   if [ $? != 0 ]; then
109     fatal_and_exit "-E- remote $zfs on $hostname create command failed" $mailto
110   fi
111 fi
112
113 # Obtain the zpool guid for the local backup pool
114 backup_pool_guid=`zpool get guid $backup_pool 2>&1 | grep $backup_pool | awk '{ print $3 }'`
115 zpool get guid $backup_pool > /dev/null 2>&1
116 if [ $? != 0 ]; then
117   fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" $mailto
118 fi
119
120 # Turn on shell verbosity
121 set -x
122
123 # Create the remote lockdir before continuing with the replicate
124 # Spinlock on creating the lock
125 maxsleeptime=60
126 maxattempts=500
127 attempts=0
128 while true; do
129   $ssh $remote mkdir "$lockdir" >/dev/null 2>&1
130   if [ $? != 0 ]; then
131     # Another zfs admin tool is running.
132     # Wait a random amount of time and try again
133     ransleep=$(($RANDOM % $maxsleeptime))
134     sleep $ransleep
135     ((attempts=attempts+1))
136   else 
137     # No other zfs admin tool is running, we can now.
138     break
139   fi
140   if [[ $attempts -gt $maxattempts ]]; then
141     # We've exceeded our maximum while loop count
142     echo "-E- The zfs filesystem has been locked down. Skipping replicate operation."
143     fail_msg=`$ssh $remote ls -ld $lockdir 2>&1`
144     fatal_and_exit "zfs-replicate-all on $hostname unable to obtain zfs admin lock:\n$fail_msg" $mailto
145   fi
146 done
147
148 # Setup our backup marker names
149 current_backup_marker=${remote_fs}@current-backup-${backup_pool_guid}
150 previous_backup_marker=${remote_fs}@previous-backup-${backup_pool_guid}
151
152 # List the snapshots on the remote machine.
153 remote_list=$(mktemp /tmp/replicate.XXXXXX)
154 $ssh $remote \
155     $zfs list -H -t snapshot |
156     grep ^${remote_fs}@ |
157     awk '{print$1}' > $remote_list
158 if [ $? != 0 ]; then
159   fatal_and_exit "-E- remote $zfs list on $hostname command failed" $mailto
160 fi
161
162 # List the snapshots on the local machine.
163 # Don't list the current backup marker if it exists on the local side.
164 # If you do, it can mess up the common finding algorithm below.
165 local_list=$(mktemp /tmp/replicate.XXXXXX)
166 $zfs list -H -t snapshot |
167     grep ^${backup_pool}/${remote_fs}@ |
168     grep -v ^${backup_pool}/${current_backup_marker} | 
169     awk "{gsub(/^$backup_pool./,\"\",\$1); print\$1}" > $local_list
170 if [ $? != 0 ]; then
171   fatal_and_exit "-E- local $zfs list on $hostname command failed" $mailto
172 fi
173
174 # Destroy the current backup marker snapshot on the remote system if it exists
175 grep -q ${current_backup_marker} $remote_list
176 if [ $? == 0 ]; then
177   $ssh $remote $zfs destroy ${current_backup_marker} 
178   if [ $? != 0 ]; then
179     fatal_and_exit "-E- remote $zfs destroy on $hostname command failed" $mailto
180   fi
181 fi
182
183 # Create the current backup marker snapshot on the remote system
184 $ssh $remote $zfs snapshot ${current_backup_marker}
185 if [ $? != 0 ]; then
186   fatal_and_exit "-E- remote $zfs snapshot on $hostname command failed" $mailto
187 fi
188
189 # Check to see if the previous backup marker exists in the remote snapshot list.
190 # Check to see if the previous backup marker exists in the local snapshot list.
191 # If the previous backup markers exists, perform an incremental replicate. Else:
192 # 1) check to see if a common snapshot exists, and perform an incremental replicate.
193 # 2) if no common snapshot exists, destroy the local filesystem, and perform a full replicate.
194 grep -q ${previous_backup_marker} $remote_list
195 no_markers=$?
196 grep -q ${previous_backup_marker} $local_list
197 no_markers=$(($no_markers || $?))
198
199 if [ $no_markers == 0 ]; then
200   # We found backup markers, incrementally send the new snaps
201
202   # First, rollback the local backup pool to the previous backup marker in case the previous
203   # backup was interrupted for some reason. If we don't do this, the zfs send -R command
204   # below may complain about snaps already existing as it tries to resend from the 
205   # previous backup marker again from a previously interrupted replicate.
206   $zfs rollback -rf ${backup_pool}/${previous_backup_marker} 
207   if [ $? != 0 ]; then
208     sleep 120
209     $zfs rollback -rf ${backup_pool}/${previous_backup_marker}
210     if [ $? != 0 ]; then
211       fatal_and_exit "-E- remote incremental $zfs rollback command failed on $hostname" $mailto
212     fi
213   fi
214   # Now it should be safe to send the snaps
215   replicate "$zfs send -Rc -I${previous_backup_marker} ${current_backup_marker}"
216   if [ $? != 0 ]; then
217     fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
218   fi
219 else
220   # We didn't find any backup markers, next check to see if we have a common snapshot.
221
222   # See what the most recent snapshot on the remote end is.
223   latest=$(tail -n 1 $remote_list)
224
225   # I did this to make sure that diff would always display the most recent common
226   # Since we're keying off the context of the diff, we need to ensure we will get context
227   # by injecting a known difference in case no others exist in the lists.
228   echo bogus.remote >> $remote_list
229   echo bogus.local  >> $local_list
230   common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
231
232   if [[ -n "$common" ]]; then
233     # We found a common snapshot, incrementally send the new snaps
234     replicate "$zfs send -Rc -I${common/*@/@} ${current_backup_marker}"
235     if [ $? != 0 ]; then
236       fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
237     fi
238   else
239     # We did not find any markers or a common snapshot
240     # At this point, we'll have to send the entire filesystem
241     # Destroy the local filesystem if it exists before receving the full replicate
242     zfs list ${backup_pool}/${remote_fs} > /dev/null 2>&1
243     if [ $? == 0 ]; then
244       if [[ $destroy_local_filesystem_on_full_replicate == 1 ]]; then
245         $zfs destroy -r ${backup_pool}/${remote_fs}
246         if [ $? != 0 ]; then
247           fatal_and_exit "-E- remote full $zfs destroy command failed on $hostname" $mailto
248         fi
249       else
250         echo "-W- We need to destroy a local filesystem before receiving a full stream."
251         echo "    However, since the option is set to prevent this, skipping replicate operation."
252         fatal_and_exit "unable to destroy local filesystem:\n$zfs destroy -r ${backup_pool}/${remote_fs} not able to run on $hostname" $mailto
253       fi
254     fi
255     # Send the full filesystem
256     replicate "$zfs send -Rc ${current_backup_marker}"
257     if [ $? != 0 ]; then
258       fatal_and_exit "-E- remote full $zfs send command failed on $hostname" $mailto
259     fi
260   fi
261 fi
262  
263 # destroy the previous backup markers now that we've replicated past them
264 # don't check the return codes here because these may not exist, and that is ok
265 $zfs destroy ${backup_pool}/${previous_backup_marker} > /dev/null 2>&1
266 $ssh $remote $zfs destroy ${previous_backup_marker} > /dev/null 2>&1
267
268 # Rename the current backup marker to be the previous backup marker
269 $zfs rename ${backup_pool}/${current_backup_marker} ${backup_pool}/${previous_backup_marker}
270 if [ $? != 0 ]; then
271   fatal_and_exit "-E- local $zfs rename command failed on $hostname" $mailto
272 fi
273 $ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
274 if [ $? != 0 ]; then
275   fatal_and_exit "-E- remote $zfs rename command failed on $hostname" $mailto
276 fi