128d051a3489903a0e1d84cc67a76ed5ed61e933
[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
8 # Set this variable to '1' to use the legacy, non-marker, snapshot diff, replicate script logic
9 use_legacy_replicate=0
10
11 # Set the name of the local pool used to store the backup of the remote
12 local_pool=backups
13
14 # Make sure we have valid arguments
15 if [[ -z "$remote" ]] || [[ -z "$remote_fs" ]]; then
16   echo "Usage: $0 <hostname> <zfs filesystem>"
17   exit 1
18 fi
19
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."
23   exit 1
24 fi
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'"
29   exit 1
30 fi
31
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"
36   exit 1
37 fi
38
39 # Turn on shell verbosity
40 set -x
41
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}
45
46 # The ssh connection doesn't find zfs without this.
47 zfs=/usr/sbin/zfs
48
49 # List the snapshots on the remote machine.
50 remote_list=$(mktemp /tmp/replicate.XXXXXX)
51 ssh $remote \
52     $zfs list -H -t snapshot |
53     grep ^${remote_fs}@ |
54     awk '{print$1}' > $remote_list
55 if [[ $? != 0 ]]; then
56   echo "-E- remote $zfs list command failed"
57   exit 1
58 fi
59
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"
67   exit 1
68 fi
69
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
73   if [ $? == 0 ]; then
74     ssh $remote $zfs destroy ${current_backup_marker} 
75     if [[ $? != 0 ]]; then
76       echo "-E- remote $zfs destroy command failed"
77       exit 1
78     fi
79   fi
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"
84     exit 1
85   fi
86
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
92   full=$?
93   grep -q ${previous_backup_marker} $local_list
94   full=$(($full || $?))
95
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"
101       exit 1
102     fi
103   else
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"
108       exit 1
109     fi
110   fi
111  
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
115
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"
120     exit 1
121   fi
122   ssh $remote $zfs rename ${current_backup_marker} ${previous_backup_marker}
123   if [[ $? != 0 ]]; then
124     echo "-E- remote $zfs rename command failed"
125     exit 1
126   fi
127    
128 else
129   # See what the most recent snapshot on the remote end is.
130   latest=$(tail -n 1 $remote_list)
131
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)
138
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%/*}
143   else
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%/*}
147   fi
148 fi
149
150 # Remove tmp files
151 #rm -f $local_list $remote_list
152