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
22 # Setup our cleanup and exit trap
24 if [[ -e "$local_list" ]]; then
27 if [[ -e "$remote_list" ]]; then
30 $ssh $remote ls -d "$lockdir" > /dev/null 2>&1
31 if [[ $? == 0 ]]; then
32 $ssh $remote rm -rf "$lockdir"
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
41 $zfs destroy ${backup_pool}/${current_backup_marker}
44 if [[ -n "$previous_backup_marker" ]]; then
45 zfs list -t snapshot ${backup_pool}/${previous_backup_marker} > /dev/null 2>&1
47 $zfs destroy ${backup_pool}/${previous_backup_marker}
50 # send email notification
51 if [[ -n "$2" ]]; then
52 echo -e "$1" | $mailx -s "zfs replicate on $hostname failed" "$2"
57 trap fatal_and_exit INT
60 # Make sure we have valid arguments
61 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
62 fatal_and_exit "Usage: $0 <hostname> <zfs filesystem>"
66 if [[ $remote = "localhost" ]]; then
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
74 fatal_and_exit "-E- The local backup pool, '$backup_pool' doesn't seem to exist." $mailto
76 zfs list "$backup_pool/$remote_pool" >/dev/null 2>&1
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
82 fatal_and_exit "-E- remote $zfs create command failed" $mailto
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
90 fatal_and_exit "-E- Unable to extract the guid for the local backup pool: $backup_pool" $mailto
93 # Turn on shell verbosity
96 # Create the remote lockdir before continuing with the replicate
97 # Spinlock on creating the lock
102 $ssh $remote mkdir "$lockdir" >/dev/null 2>&1
104 # Another zfs admin tool is running.
105 # Wait a random amount of time and try again
106 ransleep=$(($RANDOM % $maxsleeptime))
108 ((attempts=attempts+1))
110 # No other zfs admin tool is running, we can now.
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
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}
125 # List the snapshots on the remote machine.
126 remote_list=$(mktemp /tmp/replicate.XXXXXX)
128 $zfs list -H -t snapshot |
129 grep ^${remote_fs}@ |
130 awk '{print$1}' > $remote_list
132 fatal_and_exit "-E- remote $zfs list command failed" $mailto
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
144 fatal_and_exit "-E- local $zfs list command failed" $mailto
147 # Destroy the current backup marker snapshot on the remote system if it exists
148 grep -q ${current_backup_marker} $remote_list
150 $ssh $remote $zfs destroy ${current_backup_marker}
152 fatal_and_exit "-E- remote $zfs destroy command failed" $mailto
156 # Create the current backup marker snapshot on the remote system
157 $ssh $remote $zfs snapshot ${current_backup_marker}
159 fatal_and_exit "-E- remote $zfs snapshot command failed" $mailto
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
169 grep -q ${previous_backup_marker} $local_list
170 no_markers=$(($no_markers || $?))
172 if [ $no_markers == 0 ]; then
173 # We found backup markers, incrementally send the new snaps
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}
181 fatal_and_exit "-E- remote incremental $zfs rollback command failed" $mailto
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}
188 $ssh $remote $zfs send -R -I${previous_backup_marker} ${current_backup_marker} |
189 $zfs receive -vF -d ${backup_pool}/${remote_pool}
192 fatal_and_exit "-E- remote incremental $zfs send command failed" $mailto
195 # We didn't find any backup markers, next check to see if we have a common snapshot.
197 # See what the most recent snapshot on the remote end is.
198 latest=$(tail -n 1 $remote_list)
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)
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}
213 $ssh $remote $zfs send -R -I${common/*@/@} ${current_backup_marker} |
214 $zfs receive -vF -d ${backup_pool}/${remote_pool}
217 fatal_and_exit "-E- remote incremental $zfs send command failed" $mailto
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
225 if [[ $destroy_local_filesystem_on_full_replicate == 1 ]]; then
226 $zfs destroy -r ${backup_pool}/${remote_fs}
228 fatal_and_exit "-E- remote full $zfs destroy command failed" $mailto
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
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}
241 $ssh $remote $zfs send -R ${current_backup_marker} |
242 $zfs receive -vF -d ${backup_pool}/${remote_pool}
245 fatal_and_exit "-E- remote full $zfs send command failed" $mailto
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
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}
258 fatal_and_exit "-E- local $zfs rename command failed" $mailto
260 $ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
262 fatal_and_exit "-E- remote $zfs rename command failed" $mailto