forked from aaronhurt/zfs-replicate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zfs-replicate.sh
executable file
·270 lines (258 loc) · 10.5 KB
/
zfs-replicate.sh
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
#!/usr/bin/env bash
## zfs-replicate.sh
## file revision $Id$
##
############################################
##### warning gremlins live below here #####
############################################
## check log count and delete old
check_old_log() {
## declare log array
declare -a logs=()
## initialize index
local index=0
## find existing logs
for log in $(${FIND} ${LOGBASE} -maxdepth 1 -type f -name autorep-\*); do
## get file change time via stat (platform specific)
case "$(uname -s)" in
Linux|SunOS)
local fstat=$(stat -c %Z ${log})
;;
*)
local fstat=$(stat -f %c ${log})
;;
esac
## append logs to array with creation time
logs[$index]="${fstat}\t${log}\n"
## increase index
let "index += 1"
done
## set log count
local lcount=${#logs[@]}
## check count ... if greater than keep loop and delete
if [ $lcount -gt ${LOG_KEEP} ]; then
## build new array in descending age order and reset index
declare -a slogs=(); local index=0
## loop through existing array
for log in $(echo -e ${logs[@]:0} | sort -rn | cut -f2); do
## append log to array
slogs[$index]=${log}
## increase index
let "index += 1"
done
## delete excess logs
printf "deleting old logs: %s ...\n" "${slogs[@]:${LOG_KEEP}}"
rm -rf ${slogs[@]:${LOG_KEEP}}
fi
}
## exit 0 and delete old log files
exit_clean() {
## print errors
if [ "${1}x" != "x" ] && [ ${1} != 0 ]; then
printf "Last operation returned error code: %s\n" "${1}"
fi
## check log files
check_old_log
## always exit 0
printf "Exiting...\n"
exit 0
}
## lockfile creation and maintenance
check_lock () {
## check our lockfile status
if [ -f "${1}" ]; then
## get lockfile contents
local lpid=$(cat "${1}")
## see if this pid is still running
local ps=$(ps auxww|grep $lpid|grep -v grep)
if [ "${ps}x" != 'x' ]; then
## looks like it's still running
printf "ERROR: This script is already running as: %s\n" "${ps}"
else
## well the lockfile is there...stale?
printf "ERROR: Lockfile exists: %s\n" "${1}"
printf "However, the contents do not match any "
printf "currently running process...stale lockfile?\n"
fi
## tell em what to do...
printf "To run script please delete: %s\n" "${1}"
## compress log and exit...
exit_clean
else
## well no lockfile..let's make a new one
printf "Creating lockfile: %s\n" "${1}"
echo $$ > "${1}"
fi
}
## delete lockiles
clear_lock() {
## delete lockfiles...and that's all we do here
if [ -f "${1}" ]; then
printf "Deleting lockfile: %s\n" "${1}"
rm "${1}"
fi
}
## check remote system health
check_remote() {
## do we have a remote check defined
if [ "${REMOTE_CHECK}x" != 'x' ]; then
## run the check
$REMOTE_CHECK > /dev/null 2>&1
## exit if above returned non-zero
if [ $? != 0 ]; then
printf "ERROR: Remote health check '%s' failed!\n" "${REMOTE_CHECK}"
exit_clean
fi
fi
}
## main replication function
do_send() {
## check our send lockfile
check_lock "${LOGBASE}/.send.lock"
## create initial send command based on arguments
## if first snapname is NULL we do not generate an inremental
if [ "${1}" == "NULL" ]; then
local sendargs="-R"
else
local sendargs="-R -I ${1}"
fi
printf "Sending snapshots...\n"
printf "RUNNING: %s send %s %s | %s %s\n" "${ZFS}" "${sendargs}" "${2}" "${RECEIVE_PIPE}" "${3}"
${ZFS} send ${sendargs} ${2} | ${RECEIVE_PIPE} ${3}
## clear lockfile
clear_lock "${LOGBASE}/.send.lock"
}
## create and manage our zfs snapshots
do_snap() {
## make sure we aren't ever creating simultaneous snapshots
check_lock "${LOGBASE}/.snapshot.lock"
## set our snap name
local sname="autorep-${NAMETAG}"
## generate snapshot list and cleanup old snapshots
for foo in $REPLICATE_SETS; do
## split dataset into local and remote parts and trim trailing slashes
local local_set=$(echo $foo|cut -f1 -d:|sed 's/\/*$//')
local remote_set=$(echo $foo|cut -f2 -d:|sed 's/\/*$//')
## check for root datasets
if [ $ALLOW_ROOT_DATASETS -ne 1 ]; then
if [ "${local_set}" == $(basename "${local_set}") ] && \
[ "${remote_set}" == $(basename "${remote_set}") ]; then
printf "WARNING: Replicating root datasets can lead to data loss.\n"
printf "To allow root dataset replication and disable this warning "
printf "set ALLOW_ROOT_DATASETS=1 in this script. Skipping: %s\n\n" "${foo}"
## skip this set
continue
fi
fi
## get current existing snapshots that look like
## they were made by this script
if [ $RECURSE_CHILDREN -ne 1 ]; then
local temps=$($ZFS list -Hr -o name -s creation -t snapshot -d 1 ${local_set}|\
grep "${local_set}\@autorep-")
else
local temps=$($ZFS list -Hr -o name -s creation -t snapshot ${local_set}|\
grep "${local_set}\@autorep-")
fi
## just a counter var
local index=0
## our snapshot array
declare -a snaps=()
## to the loop...
for sn in $temps; do
## while we are here...check for our current snap name
if [ "${sn}" == "${local_set}@${sname}" ]; then
## looks like it's here...we better kill it
## this shouldn't happen normally
printf "Destroying DUPLICATE snapshot %s@%s\n" "${local_set}" "${sname}"
$ZFS destroy ${local_set}@${sname}
else
## append this snap to an array
snaps[$index]=$sn
## increase our index counter
let "index += 1"
fi
done
## set our snap count and reset our index
local scount=${#snaps[@]}; local index=0
## set our base snap for incremental generation below
if [ $scount -ge 1 ]; then
local base_snap=${snaps[$scount-1]}
fi
## how many snapshots did we end up with..
if [ $scount -ge $SNAP_KEEP ]; then
## oops...too many snapshots laying around
## we need to destroy some of these
while [ $scount -ge $SNAP_KEEP ]; do
## snaps are sorted above by creation in
## ascending order
printf "Destroying OLD snapshot %s\n" "${snaps[$index]}"
$ZFS destroy ${snaps[$index]}
## decrease scount and increase index
let "scount -= 1"; let "index += 1"
done
fi
## come on already...make that snapshot
printf "Creating ZFS snapshot %s@%s\n" "${local_set}" "${sname}"
## check if we are supposed to be recurrsive
if [ $RECURSE_CHILDREN -ne 1 ]; then
printf "RUNNING: %s snapshot %s@%s\n" "${ZFS}" "${local_set}" "${sname}"
$ZFS snapshot ${local_set}@${sname}
else
printf "RUNNING: %s snapshot -r %s@%s\n" "${ZFS}" "${local_set}" "${sname}"
$ZFS snapshot -r ${local_set}@${sname}
fi
## check return
if [ $? -ne 0 ]; then
## oops...that's not right
exit_clean $?
fi
## send incremental if snap count 1 or more
## otherwise send a regular stream
if [ $scount -ge 1 ]; then
do_send ${base_snap} ${local_set}@${sname} ${remote_set}
else
do_send "NULL" ${local_set}@${sname} ${remote_set}
fi
done
## clear our lockfile
clear_lock "${LOGBASE}/.snapshot.lock"
}
## it all starts here...
init() {
## sanity check
if [ $SNAP_KEEP -lt 2 ]; then
printf "ERROR: You must keep at least 2 snaps for incremental sending.\n"
printf "Please check the setting of 'SNAP_KEEP' in the script.\n"
exit_clean
fi
## check remote health
printf "Checking remote system...\n"
check_remote
## do snapshots and send
printf "Creating snapshots...\n"
do_snap
## that's it...sending called from do_snap
printf "Finished all operations for ...\n"
## show a nice message and exit...
exit_clean
}
## attempt to load configuration
if [ "${1}x" != "x" ] && [ -f "${1}" ]; then
## source passed config
printf "Sourcing configuration from %s\n" "${1}"
. "${1}"
elif [ -f "config.sh" ]; then
## source default config
printf "Sourcing configuration from config.sh\n"
. "config.sh"
else
## display error
printf "ERROR: Cannot continue without a valid configuration file!\n"
printf "Usage: %s <config>\n" "${0}"
## exit
exit 0
fi
## make sure our log dir exits
[ ! -d "${LOGBASE}" ] && mkdir -p "${LOGBASE}"
## this is where it all starts
init > "${LOGFILE}" 2>&1