This repository has been archived by the owner on Dec 4, 2020. It is now read-only.
/
functions.sh
executable file
·603 lines (491 loc) · 16.6 KB
/
functions.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
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
#!/bin/sh
# Functions / variables for lpreserver
######################################################################
# DO NOT EDIT
# Source external functions
. /usr/local/share/pcbsd/scripts/functions.sh
# Installation directory
PROGDIR="/usr/local/share/lpreserver"
# Location of settings
DBDIR="/var/db/lpreserver"
if [ ! -d "$DBDIR" ] ; then mkdir -p ${DBDIR} ; fi
CMDLOG="${DBDIR}/lp-lastcmdout"
CMDLOG2="${DBDIR}/lp-lastcmdout2"
REPCONF="${DBDIR}/replication"
LOGDIR="/var/log/lpreserver"
REPLOGSEND="${LOGDIR}/lastrep-send-log"
REPLOGRECV="${LOGDIR}/lastrep-recv-log"
MSGQUEUE="${DBDIR}/.lpreserver.msg.$$"
export DBDIR LOGDIR PROGDIR CMDLOG REPCONF REPLOGSEND REPLOGRECV MSGQUEUE
# Create the logdir
if [ ! -d "$LOGDIR" ] ; then mkdir -p ${LOGDIR} ; fi
#Set our Options
setOpts() {
if [ -e "${DBDIR}/recursive-off" ] ; then
export RECURMODE="OFF"
else
export RECURMODE="ON"
fi
if [ -e "${DBDIR}/emaillevel" ] ; then
export EMAILMODE="`cat ${DBDIR}/emaillevel`"
fi
if [ -e "${DBDIR}/duwarn" ] ; then
export DUWARN="`cat ${DBDIR}/duwarn`"
else
export DUWARN=85
fi
case $EMAILMODE in
ALL|WARN|ERROR) ;;
*) export EMAILMODE="WARN";;
esac
if [ -e "${DBDIR}/emails" ] ; then
export EMAILADDY="`cat ${DBDIR}/emails`"
fi
}
setOpts
# Check if a directory is mounted
isDirMounted() {
mount | grep -q "on $1 ("
return $?
}
mkZFSSnap() {
if [ "$RECURMODE" = "ON" ] ; then
flags="-r"
else
flags="-r"
fi
zdate=`date +%Y-%m-%d-%H-%M-%S`
zfs snapshot $flags ${1}@$2${zdate} >${CMDLOG} 2>${CMDLOG}
return $?
}
listZFSSnap() {
zfs list -t snapshot | grep -e "^NAME" -e "^${1}@"
}
rmZFSSnap() {
`zfs list -t snapshot | grep -q "^$1@$2 "` || exit_err "No such snapshot!"
if [ "$RECURMODE" = "ON" ] ; then
flags="-r"
else
flags="-r"
fi
zfs destroy -r ${1}@${2} >${CMDLOG} 2>${CMDLOG}
return $?
}
revertZFSSnap() {
# Make sure this is a valid snapshot
`zfs list -t snapshot | grep -q "^$1@$2 "` || exit_err "No such snapshot!"
# Rollback the snapshot
zfs rollback -R -f ${1}@$2
}
enable_cron()
{
cronscript="${PROGDIR}/backend/runsnap.sh"
# Make sure we remove any old entries for this dataset
cat /etc/crontab | grep -v " $cronscript $1" > /etc/crontab.new
mv /etc/crontab.new /etc/crontab
if [ "$2" = "OFF" ] ; then
return
fi
case $2 in
daily) cLine="0 $4 * * *" ;;
hourly) cLine="0 * * * *" ;;
30min) cLine="0,30 * * * *" ;;
10min) cLine="*/10 * * * *" ;;
5min) cLine="*/5 * * * *" ;;
*) exit_err "Invalid time specified" ;;
esac
echo -e "$cLine\troot ${cronscript} $1 $3" >> /etc/crontab
}
enable_watcher()
{
cronscript="${PROGDIR}/backend/zfsmon.sh"
# Check if the zfs monitor is already enabled
grep -q " $cronscript" /etc/crontab
if [ $? -eq 0 ] ; then return; fi
cLine="*/30 * * * *"
echo -e "$cLine\troot ${cronscript}" >> /etc/crontab
}
snaplist() {
zfs list -t snapshot | grep "^${1}@" | cut -d '@' -f 2 | awk '{print $1}'
}
echo_log() {
echo "`date`: $@" >> ${LOGDIR}/lpreserver.log
}
# E-Mail a message to the set addresses
# 1 = subject tag
# 2 = Message
email_msg() {
if [ -z "$EMAILADDY" ] ; then return ; fi
echo -e "$2" | mail -s "$1 - `hostname`" $EMAILADDY
}
queue_msg() {
echo -e "$1" >> ${MSGQUEUE}
if [ -n "$2" ] ; then
cat $2 >> ${MSGQUEUE}
fi
}
echo_queue_msg() {
if [ ! -e "$MSGQUEUE" ] ; then return ; fi
cat ${MSGQUEUE}
rm ${MSGQUEUE}
}
add_rep_task() {
# add freenas.8343 backupuser 22 tank1/usr/home/kris tankbackup/backups sync
HOST=$1
USER=$2
PORT=$3
LDATA=$4
RDATA=$5
TIME=$6
case $TIME in
[0-9][0-9]|sync) ;;
*) exit_err "Invalid time: $TIME"
esac
echo "Adding replication task for local dataset $LDATA"
echo "----------------------------------------------------------"
echo " Remote Host: $HOST"
echo " Remote User: $USER"
echo " Remote Port: $PORT"
echo "Remote Dataset: $RDATA"
echo " Time: $TIME"
echo "----------------------------------------------------------"
echo "Don't forget to ensure that this user / dataset exists on the remote host"
echo "with the correct permissions!"
rem_rep_task "$LDATA"
echo "$LDATA:$TIME:$HOST:$USER:$PORT:$RDATA" >> ${REPCONF}
if [ "$TIME" != "sync" ] ; then
cronscript="${PROGDIR}/backend/runrep.sh"
cLine="0 $TIME * * *"
echo -e "$cLine\troot ${cronscript} ${LDATA}" >> /etc/crontab
fi
}
rem_rep_task() {
if [ ! -e "$REPCONF" ] ; then return ; fi
cat ${REPCONF} | grep -v "^${1}:" > ${REPCONF}.tmp
mv ${REPCONF}.tmp ${REPCONF}
# Make sure we remove any old replication entries for this dataset
cronscript="${PROGDIR}/backend/runrep.sh"
cat /etc/crontab | grep -v " $cronscript $1" > /etc/crontab.new
mv /etc/crontab.new /etc/crontab
}
list_rep_task() {
if [ ! -e "$REPCONF" ] ; then return ; fi
echo "Scheduled replications:"
echo "---------------------------------"
while read line
do
LDATA=`echo $line | cut -d ':' -f 1`
TIME=`echo $line | cut -d ':' -f 2`
HOST=`echo $line | cut -d ':' -f 3`
USER=`echo $line | cut -d ':' -f 4`
PORT=`echo $line | cut -d ':' -f 5`
RDATA=`echo $line | cut -d ':' -f 6`
echo "$LDATA -> $USER@$HOST[$PORT]:$RDATA Time: $TIME"
done < ${REPCONF}
}
check_rep_task() {
export DIDREP=0
if [ ! -e "$REPCONF" ] ; then return 0; fi
repLine=`cat ${REPCONF} | grep "^${1}:"`
if [ -z "$repLine" ] ; then return 0; fi
# We have a replication task for this dataset, lets check if we need to do it now
LDATA="$1"
REPTIME=`echo $repLine | cut -d ':' -f 2`
# Export the replication variables we will be using
export REPHOST=`echo $repLine | cut -d ':' -f 3`
export REPUSER=`echo $repLine | cut -d ':' -f 4`
export REPPORT=`echo $repLine | cut -d ':' -f 5`
export REPRDATA=`echo $repLine | cut -d ':' -f 6`
if [ "$2" = "force" ] ; then
# Ready to do a forced replication
export DIDREP=1
echo_log "Starting replication MANUAL task on ${DATASET}: ${REPLOGSEND}"
queue_msg "`date`: Starting replication MANUAL task on ${DATASET}\n"
start_rep_task "$LDATA"
return $?
fi
# If we are checking for a sync task, and the rep isn't marked as sync we can return
if [ "$2" = "sync" -a "$REPTIME" != "sync" ] ; then return 0; fi
# Doing a replication task, check if one is in progress
export pidFile="${DBDIR}/.reptask-`echo ${LDATA} | sed 's|/|-|g'`"
if [ -e "${pidFile}" ] ; then
pgrep -F ${pidFile} >/dev/null 2>/dev/null
if [ $? -eq 0 ] ; then
echo_log "Skipped replication on $LDATA, previous replication is still running."
return 0
else
rm ${pidFile}
fi
fi
# Save this PID
echo "$$" > ${pidFile}
# Is this a sync-task we do at the time of a snapshot?
if [ "$2" = "sync" -a "$REPTIME" = "sync" ] ; then
export DIDREP=1
echo_log "Starting replication SYNC task on ${DATASET}: ${REPLOGSEND}"
queue_msg "`date`: Starting replication SYNC task on ${DATASET}\n"
start_rep_task "$LDATA"
return $?
else
# Ready to do a scheduled replication
export DIDREP=1
echo_log "Starting replication SCHEDULED task on ${DATASET}: ${REPLOGSEND}"
queue_msg "`date`: Starting replication SCHEDULED task on ${DATASET}\n"
start_rep_task "$LDATA"
return $?
fi
}
start_rep_task() {
LDATA="$1"
hName=`hostname`
# Check for the last snapshot marked as replicated already
lastSEND=`zfs get -r backup:lpreserver ${LDATA} | grep LATEST | awk '{$1=$1}1' OFS=" " | tail -1 | cut -d '@' -f 2 | cut -d ' ' -f 1`
# Lets get the last snapshot for this dataset
lastSNAP=`zfs list -t snapshot -d 1 -H ${LDATA} | tail -1 | awk '{$1=$1}1' OFS=" " | cut -d '@' -f 2 | cut -d ' ' -f 1`
if [ "$lastSEND" = "$lastSNAP" ] ; then
queue_msg "`date`: Last snapshot $lastSNAP is already marked as replicated!"
rm ${pidFile}
return 1
fi
# Starting replication, first lets check if we can do an incremental send
if [ -n "$lastSEND" ] ; then
zFLAGS="-Rv -I $lastSEND $LDATA@$lastSNAP"
else
zFLAGS="-Rv $LDATA@$lastSNAP"
# This is a first-time replication, lets create the new target dataset
ssh -p ${REPPORT} ${REPUSER}@${REPHOST} zfs create ${REPRDATA}/${hName} >${CMDLOG} 2>${CMDLOG}
fi
zSEND="zfs send $zFLAGS"
zRCV="ssh -p ${REPPORT} ${REPUSER}@${REPHOST} zfs receive -dvuF ${REPRDATA}/${hName}"
queue_msg "Using ZFS send command:\n$zSEND | $zRCV\n\n"
# Start up our process
$zSEND 2>${REPLOGSEND} | $zRCV >${REPLOGRECV} 2>${REPLOGRECV}
zStatus=$?
queue_msg "ZFS SEND LOG:\n--------------\n" "${REPLOGSEND}"
queue_msg "ZFS RCV LOG:\n--------------\n" "${REPLOGRECV}"
if [ $zStatus -eq 0 ] ; then
# SUCCESS!
# Lets mark our new latest snapshot and unmark the last one
if [ -n "$lastSEND" ] ; then
zfs set backup:lpreserver=' ' ${LDATA}@$lastSEND
fi
zfs set backup:lpreserver=LATEST ${LDATA}@$lastSNAP
echo_log "Finished replication task on ${DATASET}"
save_rep_props
zStatus=$?
else
# FAILED :-(
# Lets save the output for us to look at later
FLOG=${LOGDIR}/lpreserver_failed.log
echo "Failed with command:\n$zSEND | $zRCV\n" > ${FLOG}
echo "\nSending log:\n" >> ${FLOG}
cat ${REPLOGSEND} >> ${FLOG}
echo "\nRecv log:\n" >> ${FLOG}
cat ${REPLOGRECV} >> ${FLOG}
echo_log "FAILED replication task on ${DATASET}: LOGFILE: $FLOG"
fi
rm ${pidFile}
return $zStatus
}
save_rep_props() {
# If we are not doing a recursive backup / complete dataset we can skip this
if [ "$RECURMODE" != "ON" ] ; then return 0; fi
if [ "`basename $DATASET`" != "$DATASET" ] ; then return 0; fi
echo_log "Saving dataset properties for: ${DATASET}"
queue_msg "`date`: Saving dataset properties for: ${DATASET}\n"
# Lets start by building a list of props to keep
rProp=".lp-props-`echo ${REPRDATA}/${hName} | sed 's|/|#|g'`"
zfs get -r all $DATASET | grep ' local$' | awk '{$1=$1}1' OFS=" " | sed 's| local$||g' \
| ssh -p ${REPPORT} ${REPUSER}@${REPHOST} "cat > $rProp"
if [ $? -eq 0 ] ; then
echo_log "Successful save of dataset properties for: ${DATASET}"
queue_msg "`date`: Successful save of dataset properties for: ${DATASET}\n"
return 0
else
echo_log "Failed saving dataset properties for: ${DATASET}"
queue_msg "`date`: Failed saving dataset properties for: ${DATASET}\n"
return 1
fi
}
listStatus() {
for i in `grep "${PROGDIR}/backend/runsnap.sh" /etc/crontab | awk '{print $8}'`
do
echo -e "DATASET - SNAPSHOT - REPLICATION"
echo "------------------------------------------"
lastSEND=`zfs get -r backup:lpreserver ${i} | grep LATEST | awk '{$1=$1}1' OFS=" " | tail -1 | cut -d '@' -f 2 | cut -d ' ' -f 1`
lastSNAP=`zfs list -t snapshot -d 1 -H ${i} | tail -1 | awk '{$1=$1}1' OFS=" " | cut -d '@' -f 2 | cut -d ' ' -f 1`
if [ -z "$lastSEND" ] ; then lastSEND="NONE"; fi
if [ -z "$lastSNAP" ] ; then lastSNAP="NONE"; fi
echo "$i - $lastSNAP - $lastSEND"
done
}
add_zpool_disk() {
pool="$1"
disk="$2"
disk="`echo $disk | sed 's|/dev/||g'`"
if [ -z "$pool" ] ; then
exit_err "No pool specified"
exit 0
fi
if [ -z "$disk" ] ; then
exit_err "No disk specified"
exit 0
fi
if [ ! -e "/dev/$disk" ] ; then
exit_err "No such device: $disk"
exit 0
fi
# Check if pool exists
zpool status $pool >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi
# Cleanup the target disk
echo "Deleting all partitions on: $disk"
rc_nohalt "gpart destroy -F $disk" >/dev/null 2>/dev/null
rc_nohalt "dd if=/dev/zero of=/dev/${disk} bs=1m count=1" >/dev/null 2>/dev/null
rc_nohalt "dd if=/dev/zero of=/dev/${disk} bs=1m oseek=`diskinfo /dev/${disk} | awk '{print int($3 / (1024*1024)) - 4;}'`" >/dev/null 2>/dev/null
# Grab the first disk in the pool
mDisk=`zpool list -H -v | grep -v "^$pool" | awk '{print $1}' | grep -v "^mirror" | grep -v "^raidz" | head -n 1`
# Is this MBR or GPT?
echo $mDisk | grep -q 's[0-4][a-z]$'
if [ $? -eq 0 ] ; then
# MBR
type="MBR"
# Strip off the "a-z"
rDiskDev=`echo $mDisk | rev | cut -c 2- | rev`
else
# GPT
type="GPT"
# Strip off the "p[1-9]"
rDiskDev=`echo $mDisk | rev | cut -c 3- | rev`
fi
# Make sure this disk has a layout we can read
gpart show $rDiskDev >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then
exit_err "failed to get disk device layout $rDiskDev"
fi
# Get the size of "freebsd-zfs & freebsd-swap"
sSize=`gpart show ${rDiskDev} | grep freebsd-swap | cut -d "(" -f 2 | cut -d ")" -f 1`
zSize=`gpart show ${rDiskDev} | grep freebsd-zfs | cut -d "(" -f 2 | cut -d ")" -f 1`
echo "Creating new partitions on $disk"
if [ "$type" = "MBR" ] ; then
# Create the new MBR layout
rc_halt_s "gpart create -s MBR -f active $disk"
rc_halt_s "gpart add -a 4k -t freebsd $disk"
rc_halt_s "gpart set -a active -i 1 $disk"
rc_halt_s "gpart create -s BSD ${disk}s1"
rc_halt_s "gpart add -t freebsd-zfs -s $zSize ${disk}s1"
if [ -n "$sSize" ] ; then
rc_halt_s "gpart add -t freebsd-swap -s $sSize ${disk}s1"
fi
aDev="${disk}s1a"
else
# Creating a GPT disk
rc_halt_s "gpart create -s GPT $disk"
rc_halt_s "gpart add -b 34 -s 1M -t bios-boot $disk"
rc_halt_s "gpart add -t freebsd-zfs -s $zSize ${disk}"
if [ -n "$sSize" ] ; then
rc_halt_s "gpart add -t freebsd-swap -s $sSize ${disk}"
fi
aDev="${disk}p2"
fi
# Now we can insert the target disk
echo "Attaching to zpool: $aDev"
rc_halt_s "zpool attach $pool $mDisk $aDev"
# Lastly we need to stamp GRUB
echo "Stamping GRUB on: $disk"
rc_halt_s "grub-install --force /dev/${disk}"
echo "Added $disk ($aDev) to zpool $pool. Resilver will begin automatically."
exit 0
}
list_zpool_disks() {
pool="$1"
if [ -z "$pool" ] ; then
exit_err "No pool specified"
exit 0
fi
# Check if pool exists
zpool status $pool >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi
zpool list -H -v $pool
}
rem_zpool_disk() {
pool="$1"
disk="$2"
if [ -z "$pool" ] ; then
exit_err "No pool specified"
exit 0
fi
if [ -z "$disk" ] ; then
exit_err "No disk specified"
exit 0
fi
# Check if pool exists
zpool status $pool >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi
zpool detach $pool $disk
if [ $? -ne 0 ] ; then
exit_err "Failed detaching $disk"
fi
echo "$disk was detached successfully!"
exit 0
}
offline_zpool_disk() {
pool="$1"
disk="$2"
if [ -z "$pool" ] ; then
exit_err "No pool specified"
exit 0
fi
if [ -z "$disk" ] ; then
exit_err "No disk specified"
exit 0
fi
# Check if pool exists
zpool status $pool >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi
zpool offline $pool $disk
exit $?
}
online_zpool_disk() {
pool="$1"
disk="$2"
if [ -z "$pool" ] ; then
exit_err "No pool specified"
exit 0
fi
if [ -z "$disk" ] ; then
exit_err "No disk specified"
exit 0
fi
# Check if pool exists
zpool status $pool >/dev/null 2>/dev/null
if [ $? -ne 0 ] ; then exit_err "Invalid pool: $pool"; fi
zpool online $pool $disk
exit $?
}
init_rep_task() {
LDATA="$1"
repLine=`cat ${REPCONF} | grep "^${LDATA}:"`
if [ -z "$repLine" ] ; then return 0; fi
# We have a replication task for this set, get some vars
hName=`hostname`
REPHOST=`echo $repLine | cut -d ':' -f 3`
REPUSER=`echo $repLine | cut -d ':' -f 4`
REPPORT=`echo $repLine | cut -d ':' -f 5`
REPRDATA=`echo $repLine | cut -d ':' -f 6`
# First check if we even have a dataset on the remote
ssh -p ${REPPORT} ${REPUSER}@${REPHOST} zfs list ${REPRDATA}/${hName} 2>/dev/null >/dev/null
if [ $? -eq 0 ] ; then
# Lets cleanup the remote side
echo "Removing remote dataset: ${REPRDATA}/${hName}"
ssh -p ${REPPORT} ${REPUSER}@${REPHOST} zfs destroy -r ${REPRDATA}/${hName}
if [ $? -ne 0 ] ; then
echo "Warning: Could not delete remote dataset ${REPRDATA}/${hName}"
fi
fi
# Now lets mark none of our datasets as replicated
lastSEND=`zfs get -r backup:lpreserver ${LDATA} | grep LATEST | awk '{$1=$1}1' OFS=" " | tail -1 | cut -d '@' -f 2 | cut -d ' ' -f 1`
if [ -n "$lastSEND" ] ; then
zfs set backup:lpreserver=' ' ${LDATA}@$lastSEND
fi
}