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>
11 # source our configuration
12 config="${0%/*}/zfs-scripts.conf"
13 [ -e "${config}.dist" ] && . ${config}.dist
14 [ -e "${config}" ] && . ${config}
16 # command line arg parsing
21 # get the backup pool from the command line or the config file if not specified
25 backup_pool=${backup_pool%% *} # use the first backup pool if none specified
28 # Setup our cleanup and exit trap
30 if [[ -e "$local_list" ]]; then
33 if [[ -e "$remote_list" ]]; then
36 $ssh $remote ls -d "$lockdir" > /dev/null 2>&1
37 if [[ $? == 0 ]]; then
38 $ssh $remote rm -rf "$lockdir"
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
48 $zfs destroy ${backup_pool}/${current_backup_marker}
50 # Remote pool current backup marker
51 $ssh $remote zfs list -t snapshot ${current_backup_marker} > /dev/null 2>&1
53 $ssh $remote $zfs destroy ${current_backup_marker}
56 # send email notification
57 if [[ -n "$2" ]]; then
58 echo -e "$1" | $mailx -s "zfs replicate on $hostname failed" "$2"
63 trap fatal_and_exit INT
66 # Declare a function to handle the replicate operation
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
77 # remote host glue case
78 $ssh $remote "$zfs_send | $glue" | $zfs_recv
81 # no glue case - works for both the local and remote host cases
82 $ssh $remote $zfs_send | $zfs_recv
84 # The return code of the zfs_send | zfs_recv operation will be returned to the caller
87 # Make sure we have valid arguments
88 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
89 fatal_and_exit "Usage: $0 <hostname> <zfs filesystem>"
93 if [[ $remote = "localhost" ]]; then
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
101 fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." $mailto
103 zfs list "$backup_pool/$remote_pool" >/dev/null 2>&1
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
109 fatal_and_exit "-E- remote $zfs on $hostname create command failed" $mailto
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
117 fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" $mailto
120 # Turn on shell verbosity
123 # Create the remote lockdir before continuing with the replicate
124 # Spinlock on creating the lock
129 $ssh $remote mkdir "$lockdir" >/dev/null 2>&1
131 # Another zfs admin tool is running.
132 # Wait a random amount of time and try again
133 ransleep=$(($RANDOM % $maxsleeptime))
135 ((attempts=attempts+1))
137 # No other zfs admin tool is running, we can now.
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
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}
152 # List the snapshots on the remote machine.
153 remote_list=$(mktemp /tmp/replicate.XXXXXX)
155 $zfs list -H -t snapshot |
156 grep ^${remote_fs}@ |
157 awk '{print$1}' > $remote_list
159 fatal_and_exit "-E- remote $zfs list on $hostname command failed" $mailto
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
171 fatal_and_exit "-E- local $zfs list on $hostname command failed" $mailto
174 # Destroy the current backup marker snapshot on the remote system if it exists
175 grep -q ${current_backup_marker} $remote_list
177 $ssh $remote $zfs destroy ${current_backup_marker}
179 fatal_and_exit "-E- remote $zfs destroy on $hostname command failed" $mailto
183 # Create the current backup marker snapshot on the remote system
184 $ssh $remote $zfs snapshot ${current_backup_marker}
186 fatal_and_exit "-E- remote $zfs snapshot on $hostname command failed" $mailto
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
196 grep -q ${previous_backup_marker} $local_list
197 no_markers=$(($no_markers || $?))
199 if [ $no_markers == 0 ]; then
200 # We found backup markers, incrementally send the new snaps
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 -r ${backup_pool}/${previous_backup_marker}
208 fatal_and_exit "-E- remote incremental $zfs rollback command failed on $hostname" $mailto
210 # Now it should be safe to send the snaps
211 replicate "$zfs send -Rc -I${previous_backup_marker} ${current_backup_marker}"
213 fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
216 # We didn't find any backup markers, next check to see if we have a common snapshot.
218 # See what the most recent snapshot on the remote end is.
219 latest=$(tail -n 1 $remote_list)
221 # I did this to make sure that diff would always display the most recent common
222 # Since we're keying off the context of the diff, we need to ensure we will get context
223 # by injecting a known difference in case no others exist in the lists.
224 echo bogus.remote >> $remote_list
225 echo bogus.local >> $local_list
226 common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
228 if [[ -n "$common" ]]; then
229 # We found a common snapshot, incrementally send the new snaps
230 replicate "$zfs send -Rc -I${common/*@/@} ${current_backup_marker}"
232 fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
235 # We did not find any markers or a common snapshot
236 # At this point, we'll have to send the entire filesystem
237 # Destroy the local filesystem if it exists before receving the full replicate
238 zfs list ${backup_pool}/${remote_fs} > /dev/null 2>&1
240 if [[ $destroy_local_filesystem_on_full_replicate == 1 ]]; then
241 $zfs destroy -r ${backup_pool}/${remote_fs}
243 fatal_and_exit "-E- remote full $zfs destroy command failed on $hostname" $mailto
246 echo "-W- We need to destroy a local filesystem before receiving a full stream."
247 echo " However, since the option is set to prevent this, skipping replicate operation."
248 fatal_and_exit "unable to destroy local filesystem:\n$zfs destroy -r ${backup_pool}/${remote_fs} not able to run on $hostname" $mailto
251 # Send the full filesystem
252 replicate "$zfs send -Rc ${current_backup_marker}"
254 fatal_and_exit "-E- remote full $zfs send command failed on $hostname" $mailto
259 # destroy the previous backup markers now that we've replicated past them
260 # don't check the return codes here because these may not exist, and that is ok
261 $zfs destroy ${backup_pool}/${previous_backup_marker} > /dev/null 2>&1
262 $ssh $remote $zfs destroy ${previous_backup_marker} > /dev/null 2>&1
264 # Rename the current backup marker to be the previous backup marker
265 $zfs rename ${backup_pool}/${current_backup_marker} ${backup_pool}/${previous_backup_marker}
267 fatal_and_exit "-E- local $zfs rename command failed on $hostname" $mailto
269 $ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
271 fatal_and_exit "-E- remote $zfs rename command failed on $hostname" $mailto