This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
lpreserver
executable file
·429 lines (311 loc) · 8.58 KB
/
lpreserver
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#!/bin/sh
# Command-line interaction script for life-preserver
# Author: Kris Moore
# License: BSD
# "Life-Preserver" Copyright 2013 PC-BSD Software (iXsystems)
######################################################################
# Source external functions
. /usr/local/share/pcbsd/scripts/functions.sh
require_root()
{
UTEST="`whoami`"
if [ "$UTEST" != "root" ] ; then
exit_err "This command must be run as root!"
fi
}
# Source our functions
PROGDIR="/usr/local/share/lpreserver"
# Source our local functions
. ${PROGDIR}/backend/functions.sh
title()
{
echo "Life-Preserver
---------------------------------"
};
# Function to display help information
help_main()
{
title
cat<<__EOF__
Available commands
Type in help <command> for information and usage about that command
get - Get list of lpreserver options
help - This help file
mksnap - Create a ZFS snapshot of a zpool/dataset
listsnap - List snapshots of a zpool/dataset
listcron - Listing of scheduled snapshots
cronsnap - Schedule snapshot creation via cron
revertsnap - Revert zpool/dataset to a snapshot
replicate - Enable / Disable ZFS replication to a remote system
set - Set lpreserver options
status - List datasets, along with last snapshot / replication date
rmsnap - Remove a snapshot
__EOF__
};
help_replicate()
{
title
echo "Help replicate
Life-Preserver replication sub-system
Replication uses ZFS to send your snapshots to a remote system which also has
a ZFS pool, such as FreeNAS or another PC-BSD system. The <hostdataset> must
also be scheduled for snapshots for replication to occur.
On the remote system, you will need to have created the <remotedataset> beforehand.
If you plan to replicate to a non-root user, please set the following permissions
on the remote dataset:
# zfs allow -u <user> create,receive,mount,userprop,destroy,send,hold <remotedataset>
Available Flags:
list - List replication targets
add - Add a new replication target
remove - Remove a replication target
Add Options:
add <remotehost> <user> <port> <hostdataset> <remotedataset> <time>
Example 1:
add freenas.8343 backupuser 22 tank1/usr/home/kris tankbackup/backups sync
Will schedule replication of tank1/usr/home/kris to tankbackup/backups with each snapshot
Example 2:
add freenas.8343 backupuser 22 tank1 tankbackup/backups 22
Will schedule replication of tank1 to tankbackup/backups at 10PM, notated in 24hour time
Remove Options:
remove <dataset>
Remove a replication task indicated by <dataset>
Use 'replicate list' for a list of IDs.
Usage:
lpreserver replicate <subcmd> <flags>
Example:
lpreserver replicate
"
};
help_set()
{
title
echo "Help set
Set Life-Preserver options
Usage:
lpreserver set
Config options
duwarn - Set to a disk percentage [0-99] at which to warn of low disk space
email - Set the e-mail address to receive notifications
This will require that the \"mail\" command is setup for outgoing mail
emailopts - (ALL/WARN/ERROR)
ALL - Send mail for every snapshot, plus warnings and errors
WARN - Send mail for warnings and errors (Default)
ERROR - Send mail for errors only
recursive - (ON/OFF)
ON - Will create / destroy snapshots recursively, for all children of
a target zpool/dataset. (Default)
OFF - Will only create / destroy snapshots on the specified dataset.
Example:
lpreserver set email "kris@example.org,admin@example.org"
lpreserver set emailopts ALL
"
};
help_get()
{
title
echo "Help get
Listing of Life-Preserver options set
Usage:
lpreserver get
Example:
lpreserver get
"
};
help_listcron()
{
title
echo "Help listcron
List scheduled cron snapshots
Usage:
lpreserver listcron
"
};
help_status()
{
title
echo "Help status
List monitored dataset's last snapshot and replication
Usage:
lpreserver status
"
};
help_listsnap()
{
title
echo "Help listsnap
List ZFS snapshots of a dataset
Usage:
lpreserver listsnap <dataset>
Example:
lpreserver listsnap tank0/usr/home/kris
"
};
help_cronsnap()
{
title
echo "Help cronsnap
Schedule a ZFS snapshot
Usage:
For a listing of all scheduled snapshots
# lpreserver listcron
or
To start / stop snapshot scheduling
# lpreserver cronsnap <dataset> <action> <frequency> <numToKeep>
action = start / stop
frequency = daily@XX / hourly / 30min / 10min / 5min
^^ Hour to execute
numToKeep = Number of snapshots to keep total
Example:
lpreserver cronsnap tank1/usr/home/kris start daily@22 10
or
lpreserver cronsnap tank1/usr/home/kris stop
"
};
help_revertsnap()
{
title
echo "Help revertsnap
Revert ZFS to a snapshot of a dataset
Usage:
lpreserver revertsnap <dataset> <SNAP>
Example:
lpreserver revertsnap tank1/usr/home/kris 20130729-140510
"
};
help_rmsnap()
{
title
echo "Help rmsnap
Remove ZFS snapshot of a dataset
Usage:
lpreserver rmsnap <dataset> <SNAP>
Example:
lpreserver rmsnap tank1/usr/home/kris 20130729-140510
NOTE:
By default snapshots are recursive and will remove snapshots
of all children datasets automatically. To change this use:
# lpreserver set recursive off
"
};
help_mksnap()
{
title
echo "Help mksnap
Create a new ZFS snapshot of a dataset and optionally replicate it immediately
Usage:
lpreserver mksnap --replicate <dataset> <snapshotname>
Example:
lpreserver mksnap tank1/usr/home/kris mysnapshot
or
lpreserver mksnap --replicate tank1/usr/home/kris mysnapshot
NOTE:
By default snapshots are recursive and will create snapshots
of all children datasets automatically. To change this use:
# lpreserver set recursive off
"
};
# Check if we need to enable the zfs monitor
enable_watcher
# Check what the user wants to do
case "$1" in
help) case "$2" in
get) help_get ;;
set) help_set ;;
mksnap) help_mksnap ;;
listcron) help_listcron ;;
listsnap) help_listsnap ;;
revertsnap) help_revertsnap ;;
replicate) help_replicate ;;
cronsnap) help_cronsnap ;;
rmsnap) help_rmsnap ;;
status) help_status ;;
*) help_main ;;
esac ;;
get) require_root
# Display our options
title
echo " Recursive mode: $RECURMODE"
echo "E-mail notifications: $EMAILMODE"
echo " E-mail addresses: $EMAILADDY"
echo " Disk space warn at: ${DUWARN}%"
exit 0
;;
set) require_root
OPT="${2}"
case $OPT in
emailopts) case $3 in
ALL|WARN|ERROR) echo "E-Mail mode set to: $3"
echo "$3" > ${DBDIR}/emaillevel ;;
*) exit_err "Unknown option" ;;
esac
;;
email) echo "Setting email notification to: $3"
echo "$3" > ${DBDIR}/emails ;;
duwarn) if [ ! $(is_num "$3") ] ; then exit_err "Invalid number, must be 0-99"; fi
echo "Setting disk warning capacity to: ${3}%"
echo "$3" > ${DBDIR}/duwarn ;;
recursive) case $3 in
OFF|Off|off) echo "Recursive mode disabled"
touch ${DBDIR}/recursive-off ;;
ON|On|on) echo "Recursive mode enabled"
rm ${DBDIR}/recursive-off 2>/dev/null ;;
*) exit_err "Unknown option" ;;
esac
;;
*) exit_err "Invalid option!" ;;
esac
;;
mksnap) require_root
if [ "$2" = "--replicate" ] ; then
REP="YES"
DATASET="$3"
SNAPNAME="$4"
else
REP="NO"
DATASET="$2"
SNAPNAME="$3"
fi
${PROGDIR}/backend/zfsmksnap.sh "${DATASET}" "$SNAPNAME" "$REP"
;;
listcron) require_root
${PROGDIR}/backend/zfslistcron.sh
;;
listsnap) require_root
DATASET="$2"
${PROGDIR}/backend/zfslistsnap.sh "${DATASET}"
;;
cronsnap) require_root
DATASET="$2"
${PROGDIR}/backend/zfscronsnap.sh "${DATASET}" "$3" "$4" "$5"
;;
revertsnap) require_root
DATASET="$2"
SNAP="$3"
${PROGDIR}/backend/zfsrevertsnap.sh "${DATASET}" "$SNAP"
;;
rmsnap) require_root
DATASET="$2"
SNAP="$3"
${PROGDIR}/backend/zfsrmsnap.sh "${DATASET}" "${SNAP}"
;;
status) listStatus
;;
replicate) require_root
shift
case ${1} in
add) add_rep_task "$2" "$3" "$4" "$5" "$6" "$7" ;;
list) list_rep_task ;;
remove) cat ${REPCONF} | grep -q "^${2}:"
if [ $? -eq 0 ] ; then
rem_rep_task "$2"
echo "Removed replication task for $2"
else
exit_err "No such replication task for dataset $2"
fi
;;
*) exit_err "Invalid option!" ;;
esac
;;
*) help_main ;;
esac
exit 0