3 # Usage: replicate <hostname> <zfs filesystem>
8 # Set this variable to '1' to use the legacy, non-marker, snapshot diff, replicate script logic
11 # Set the name of the local pool used to store the backup of the remote
14 # Make sure we have valid arguments
15 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
16 echo "Usage: $0 <hostname> <zfs filesystem>"
20 # Make sure the local pool and local receiving filesystem exist, or print some errors
21 if ! zpool list -H "$local_pool" >/dev/null 2>&1; then
22 echo >&2 "-E- The local pool, '$local_pool' doesn't seem to exist."
25 if ! zfs list "$local_pool/$remote_pool" >/dev/null 2>&1; then
26 echo >&2 "-E- The local filesystem for the remote pool, '$local_pool/$remote_pool' doesn't seem to exist."
27 echo >&2 " You will need to create this filesystem before this script can replicate your data."
28 echo >&2 " You can create this filsystem by executing this command: 'zfs create $local_pool/$remote_pool'"
32 # Obtain the zpool guid for the local pool
33 local_pool_guid=`zpool get guid $local_pool 2>&1 | grep $local_pool | awk '{ print $3 }'`
34 if ! zpool get guid $local_pool > /dev/null 2>&1; then
35 echo >&2 "-E- Unable to extract the guid for the local pool: $local_pool"
39 # Turn on shell verbosity
42 # Setup our backup marker names
43 current_backup_marker=${remote_fs}@current-backup-${local_pool_guid}
44 previous_backup_marker=${remote_fs}@previous-backup-${local_pool_guid}
46 # The ssh connection doesn't find zfs without this.
49 # List the snapshots on the remote machine.
50 remote_list=$(mktemp /tmp/replicate.XXXXXX)
52 $zfs list -H -t snapshot |
54 awk '{print$1}' > $remote_list
55 if [[ $? != 0 ]]; then
56 echo "-E- remote $zfs list command failed"
60 # List the snapshots on the local machine.
61 local_list=$(mktemp /tmp/replicate.XXXXXX)
62 $zfs list -H -t snapshot |
63 grep ^${local_pool}/${remote_fs}@ |
64 awk '{gsub(/^${local_pool}./,"",$1); print$1}' > $local_list
65 if [[ $? != 0 ]]; then
66 echo "-E- local $zfs list command failed"
70 if [ $use_legacy_replicate == 0 ]; then
71 # Destroy the current backup marker snapshot on the remote system if it exists
72 grep -q ${current_backup_marker} $remote_list
74 ssh $remote $zfs destroy ${current_backup_marker}
75 if [[ $? != 0 ]]; then
76 echo "-E- remote $zfs destroy command failed"
80 # Create the current backup marker snapshot on the remote system
81 ssh $remote $zfs snapshot ${current_backup_marker}
82 if [[ $? != 0 ]]; then
83 echo "-E- remote $zfs snapshot command failed"
87 # Check to see if the previous backup marker exists in the remote snapshot list.
88 # Check to see if the previous backup marker exists in the local snapshot list.
89 # If the previous backup markers exists, perform an incremental replicate.
90 # Otherwise, perform a full replicate.
91 grep -q ${previous_backup_marker} $remote_list
93 grep -q ${previous_backup_marker} $local_list
96 if [[ $full == 0 ]]; then
97 ssh $remote $zfs send -R -I${previous_backup_marker} ${current_backup_marker} |
98 $zfs receive -vF -d ${local_pool}/${remote_fs%/*}
99 if [[ $? != 0 ]]; then
100 echo "-E- remote incremental $zfs send command failed"
104 ssh $remote $zfs send -R ${current_backup_marker} |
105 $zfs receive -vF -d ${local_pool}/${remote_fs%/*}
106 if [[ $? != 0 ]]; then
107 echo "-E- remote full $zfs send command failed"
112 # destroy the previous backup markers now that we've replicated past them
113 $zfs destroy ${local_pool}/${previous_backup_marker} > /dev/null 2>&1
114 ssh $remote $zfs destroy ${previous_backup_marker} > /dev/null 2>&1
116 # Rename the current backup marker to be the previous backup marker
117 $zfs rename ${local_pool}/${current_backup_marker} ${local_pool}/${previous_backup_marker}
118 if [[ $? != 0 ]]; then
119 echo "-E- local $zfs rename command failed"
122 ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
123 if [[ $? != 0 ]]; then
124 echo "-E- remote $zfs rename command failed"
129 # See what the most recent snapshot on the remote end is.
130 latest=$(tail -n 1 $remote_list)
132 # I did this to make sure that diff would always display the most recent common
133 # Since we're keying off the context of the diff, we need to ensure we will get context
134 # by injecting a known difference in case no others exist in the lists.
135 echo bogus.remote >> $remote_list
136 echo bogus.local >> $local_list
137 common=$(diff -u $remote_list $local_list | grep '^ ' | tail -n 1)
139 if [ -n "$common" ]; then
140 # We found a common snapshot
141 ssh $remote $zfs send -R -I${common/*@/@} $latest |
142 $zfs receive -vF -d ${local_pool}/${remote_fs%/*}
144 # We did not find a common snapshot, so send the entire filesystem
145 ssh $remote $zfs send -R $latest |
146 $zfs receive -vF -d ${local_pool}/${remote_fs%/*}
151 #rm -f $local_list $remote_list