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