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