-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_tzvolsync
More file actions
290 lines (231 loc) · 7.75 KB
/
02_tzvolsync
File metadata and controls
290 lines (231 loc) · 7.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
#!/bin/bash
# vim: sw=5
###########################################################################
#
# Program name: tzvolsync
# File name: tzvolsync.sh
# Author: Peter Blajev
# First revision: 12/20/2010
#
# Description:
# Sends incr. ZFS data streams to a remote backup system.
#
# Designed to be ran from cron:
# 0,30 * * * 1,3,5,0 /usr/local/sbin/tzvolsync run >> /var/log/tzvolsync.log 2>&1
# 15,45 * * * 2,4,6 /usr/local/sbin/tzvolsync run >> /var/log/tzvolsync.log 2>&
#
###########################################################################
# ---------------------------------------------------------------------------
# Things we can setup
# USER that should be used to connect to the remote system
USER=zfshelp
# How many snapshots to keep
TOKEEP=3
# Config file
configFile="/usr/local/etc/tzvolsync.conf"
# ---------------------------------------------------------------------------
# Do not edit below this line
# Tailored for Solaris
PATH="/usr/gnu/bin:/usr/bin:/bin:/usr/X11/bin:/usr/sbin:/sbin"
HOSTNAME=`hostname`
self="`basename $0`"
myid=`id -u`
myname=`id -un`
# ===========================================================================
# START the INITIAL Run of the recursive sequence
# ===========================================================================
if [ "$1" == "run" ]; then
# This is the initial run
# Is root the one who ran tzvolsync?
if [ $myid -ne 0 ]; then
echo "
${self}:
Must be run as root (uid=0).
You are currently $myname (uid=$myid).
"
exit 1
fi
# Make sure no other process is running
RUNFILE=/var/run/$self.pid
if [ -f $RUNFILE ]; then
echo "(`date +%H:%M:%S`) Old $self is running. PID in $RUNFILE: \
`cat $RUNFILE`"
exit 1
fi
# Save the process ID.
echo $$ > $RUNFILE
# Parse the config file
if [ ! -r $configFile ]; then
echo "${self}: Config file $configFile not found."
rm -f $RUNFILE
exit 1
fi
cat $configFile | \
grep -v "^\#" | \
grep -v "^ " | \
grep -v "^$" |
while read configLine; do
MASTER=`echo $configLine | awk -F "|" '{print $1}'`
VOLUME=`echo $configLine | awk -F "|" '{print $2}'`
SLAVE=`echo $configLine | awk -F "|" '{print $3}'`
if [ "$MASTER" != "$HOSTNAME" ]; then
# this entry is not for us
continue
fi
# run recursive
$0 $MASTER $VOLUME $SLAVE
done
rm -f $RUNFILE
echo ""
echo "--------------------------------------------------------------------"
echo "==> [`date +\"%m/%d/%Y - %H:%M\"`] Done"
exit 0
fi
# ===========================================================================
# END the INITIAL Run of the recursive sequence
# ===========================================================================
# ---------------------------------------------------------------------------
# Make sure the system is good to run the command
goodtogo ()
{
EXITSTAT=0
# Did we get a dataset
[ "$2" == "" ] && EXITSTAT=1
# Is the DATASETIN a ZFS pool?
POOLS=`zpool list \
| grep -v NAME \
| awk '{print $1}'`
for POOL in $POOLS; do
[ "$2" == "$POOL" ] && EXITSTAT=2
done
# Does the FS exit?
zfs list $2 > /dev/null 2>&1
[ $? -ne 0 ] && EXITSTAT=3
# Did we get a slave server
[ "$3" == "" ] && EXITSTAT=4
# Is the slave server running any replication at this time?
ISRUNNING=`ssh -n $USER@$3 ps -ef | grep -E 'zfs send|zfs receive' | grep -v grep`
[ -n "$ISRUNNING" ] && EXITSTAT=5
echo $EXITSTAT
} # END goodtogo
# ---------------------------------------------------------------------------
# START main program
# ---------------------------------------------------------------------------
MASTER=$1
VOLUME=$2
SLAVE=$3
echo ""
echo "--------------------------------------------------------------------"
echo "==> [`date +\"%m/%d/%Y - %H:%M\"`] $MASTER:$VOLUME -> $SLAVE"
case `goodtogo $MASTER $VOLUME $SLAVE` in
1)
echo ""
echo " ERROR: Dataset empty."
exit 1
;;
2)
echo ""
echo " ERROR: Looks like $VOLUME is a pool."
exit 1
;;
3)
echo ""
echo " ERROR: ZFS dataset $VOLUME is invalid."
exit 1
;;
4)
echo ""
echo " ERROR: Destination host not set."
exit 1
;;
5)
echo ""
echo " ERROR: Process \"zfs [send,receive]\" found on $SLAVE"
exit 1
;;
esac
# At this point we are ready to start replication
# ---------------------------------------------------------------------------
# Find the last snapshots on both servers
LASTSNAP=`zfs list -r -t snapshot -o name $VOLUME | \
grep $VOLUME@$self | tail -1 | tr '@' ' ' | awk '{print \$2}'`
if [ "$LASTSNAP" == "" ]; then
echo ""
echo " No old snapshots for $VOLUME. Running for the first time."
INCREMENTAL=no
# There should be no dataset existing on the remote server
ssh -n $USER@$SLAVE "sudo /usr/sbin/zfs list $VOLUME" > /dev/null 2>&1
if [ $? -eq 0 ]; then
echo " ERROR: Dataset $VOLUME exists on the remote server."
echo " Destroy it first and then rerun $self."
exit 1
fi
else
# Make sure the snapshot exists on the slave
LASTRSNAP=`ssh -n $USER@$SLAVE "sudo /usr/sbin/zfs list -r -t snapshot -o name $VOLUME 2>&1 | grep $VOLUME@$self" | \
tail -1 | tr '@' ' ' | awk '{print $2}'`
if [ "$LASTSNAP" == "$LASTRSNAP" ]; then
echo " Last snapshot $VOLUME@$LASTSNAP"
echo " found on both hosts."
INCREMENTAL=yes
else
echo " Snapshot $VOLUME@$LASTSNAP not found or"
echo " not the most recent on the remote server."
exit 1
fi
fi
# ---------------------------------------------------------------------------
# New snapshot
NEWSNAP=`date +$self%Y%m%d%H%M`
if [ "$NEWSNAP" == "$LASTSNAP" ]; then
echo " Snapshot $NEWSNAP exists. Wait at least 1 min. between runs."
exit 1
fi
CMD="zfs snapshot $VOLUME@$NEWSNAP"
echo ""
#echo " (`date +%H:%M:%S`) New snapshot"
echo " (`date +%H:%M:%S`) New snapshot: $VOLUME@$NEWSNAP"
echo " Run: $CMD"
$CMD
if [ $? -ne 0 ]; then
echo " ERROR: Creating sanpshot failed."
exit 1
fi
# ---------------------------------------------------------------------------
# Send data stream to the slave
CMDOPT=""
ROPT=""
[ "$INCREMENTAL" == "yes" ] && CMDOPT="-i $VOLUME@$LASTSNAP"
CMD="zfs send $CMDOPT $VOLUME@$NEWSNAP | ssh $USER@$SLAVE 'sudo /usr/sbin/zfs receive $ROPT $VOLUME'"
echo ""
echo " (`date +%H:%M:%S`) Sending data to the slave"
echo " Run: $CMD"
echo $CMD | bash
if [ $? -ne 0 ]; then
echo " Something went wrong. Removing the snapshot."
zfs destroy $VOLUME@$NEWSNAP
else
# Set ReadOnly flag on the remote dataset
[ "$INCREMENTAL" == "no" ] \
&& ssh -n $USER@$SLAVE "sudo /usr/sbin/zfs set readonly=on $VOLUME"
fi
# ---------------------------------------------------------------------------
# Remove old snapshots
NUMSNAPS=`zfs list -r -t snapshot -H -o name $VOLUME | \
grep $VOLUME@$self | wc -l`
NUMTODEL=`expr $NUMSNAPS - $TOKEEP`
echo ""
echo " (`date +%H:%M:%S`) Removing old snapshots"
if [ $NUMTODEL -lt 1 ]; then
echo " No snapshots to delete."
else
SNAPSTODEL=`zfs list -r -t snapshot -H -o name $VOLUME | \
grep $VOLUME@$self | head -${NUMTODEL}`
echo " Removing: $SNAPSTODEL"
for SNAP in $SNAPSTODEL; do
zfs destroy $SNAP
ssh -n $USER@$SLAVE "sudo /usr/sbin/zfs destroy $SNAP"
done
fi
# ---------------------------------------------------------------------------
exit 0