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