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 # Setup our cleanup and exit trap
23 if [[ -e "$local_list" ]]; then
26 if [[ -e "$remote_list" ]]; then
29 $ssh $remote ls -d "$lockdir" > /dev/null 2>&1
30 if [[ $? == 0 ]]; then
31 $ssh $remote rm -rf "$lockdir"
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
40 $zfs destroy ${backup_pool}/${current_backup_marker}
43 if [[ -n "$previous_backup_marker" ]]; then
44 zfs list -t snapshot ${backup_pool}/${previous_backup_marker} > /dev/null 2>&1
46 $zfs destroy ${backup_pool}/${previous_backup_marker}
49 # send email notification
50 if [[ -n "$2" ]]; then
51 echo -e "$1" | $mailx -s "zfs replicate on $hostname failed" "$2"
56 trap fatal_and_exit INT
59 # Make sure we have valid arguments
60 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
61 fatal_and_exit "Usage: $0 <hostname> <zfs filesystem>"
65 if [[ $remote = "localhost" ]]; then
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
73 fatal_and_exit "-E- The local backup pool on $hostname, '$backup_pool' doesn't seem to exist." $mailto
75 zfs list "$backup_pool/$remote_pool" >/dev/null 2>&1
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
81 fatal_and_exit "-E- remote $zfs on $hostname create command failed" $mailto
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
89 fatal_and_exit "-E- Unable to extract the guid for the local backup pool on $hostname: $backup_pool" $mailto
92 # Turn on shell verbosity
95 # Create the remote lockdir before continuing with the replicate
96 # Spinlock on creating the lock
101 $ssh $remote mkdir "$lockdir" >/dev/null 2>&1
103 # Another zfs admin tool is running.
104 # Wait a random amount of time and try again
105 ransleep=$(($RANDOM % $maxsleeptime))
107 ((attempts=attempts+1))
109 # No other zfs admin tool is running, we can now.
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
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}
124 # List the snapshots on the remote machine.
125 remote_list=$(mktemp /tmp/replicate.XXXXXX)
127 $zfs list -H -t snapshot |
128 grep ^${remote_fs}@ |
129 awk '{print$1}' > $remote_list
131 fatal_and_exit "-E- remote $zfs list on $hostname command failed" $mailto
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
143 fatal_and_exit "-E- local $zfs list on $hostname command failed" $mailto
146 # Destroy the current backup marker snapshot on the remote system if it exists
147 grep -q ${current_backup_marker} $remote_list
149 $ssh $remote $zfs destroy ${current_backup_marker}
151 fatal_and_exit "-E- remote $zfs destroy on $hostname command failed" $mailto
155 # Create the current backup marker snapshot on the remote system
156 $ssh $remote $zfs snapshot ${current_backup_marker}
158 fatal_and_exit "-E- remote $zfs snapshot on $hostname command failed" $mailto
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
168 grep -q ${previous_backup_marker} $local_list
169 no_markers=$(($no_markers || $?))
171 if [ $no_markers == 0 ]; then
172 # We found backup markers, incrementally send the new snaps
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}
180 fatal_and_exit "-E- remote incremental $zfs rollback command failed on $hostname" $mailto
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}
187 $ssh $remote $zfs send -R -I${previous_backup_marker} ${current_backup_marker} |
188 $zfs receive -vF -d ${backup_pool}/${remote_pool}
191 fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
194 # We didn't find any backup markers, next check to see if we have a common snapshot.
196 # See what the most recent snapshot on the remote end is.
197 latest=$(tail -n 1 $remote_list)
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)
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}
212 $ssh $remote $zfs send -R -I${common/*@/@} ${current_backup_marker} |
213 $zfs receive -vF -d ${backup_pool}/${remote_pool}
216 fatal_and_exit "-E- remote incremental $zfs send command failed on $hostname" $mailto
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
224 if [[ $destroy_local_filesystem_on_full_replicate == 1 ]]; then
225 $zfs destroy -r ${backup_pool}/${remote_fs}
227 fatal_and_exit "-E- remote full $zfs destroy command failed on $hostname" $mailto
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
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}
240 $ssh $remote $zfs send -R ${current_backup_marker} |
241 $zfs receive -vF -d ${backup_pool}/${remote_pool}
244 fatal_and_exit "-E- remote full $zfs send command failed on $hostname" $mailto
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
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}
257 fatal_and_exit "-E- local $zfs rename command failed on $hostname" $mailto
259 $ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
261 fatal_and_exit "-E- remote $zfs rename command failed on $hostname" $mailto