/
start
executable file
·432 lines (369 loc) · 11.5 KB
/
start
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
#!/bin/bash
#-x
###############################
# nas backup connections
NAS=nas
NAS_IP=192.168.178.10
MOUNT_NFS="$NAS_IP:pool0"
MOUNT_LOCAL="/data/pool0"
###############################
# mail options
MAIL_PREFRACE="[nas-backup]"
###############################
# Backup dirs; relative from Mount or full path
NAS_MOUNT_DIR=$PWD/nasmount
BACKUP_DIRS="homes photo docs docker cloud video /etc"
KEYFILE="$PWD/key.txt"
###############################
# Backup Log folder
LOGBASE=$PWD/backup.log
LOGFILE=$LOGBASE/$NOW.backup.log
FILELIST=$LOGBASE/$NOW.tar.list
TAPENAMES=$LOGBASE/$NOW.backup.tapenames.txt
###############################
# Tape devie name
TAPE="/dev/st0"
TAPESIZE="400*1000*1000*1000"
TAPESPEED="70*1000*1000"
BLOCKSIZE="512K"
###############################
# Tar options
TAR_ARGS="-b 1024"
TAPE_BUFF="3g"
###############################
#compression
# please add the pipe in order to also diable this compression completely
#COMPRESSION_CMD="pigz -3 -p 32 "
#DECOMPRESSION_CMD="pigz -dc -3 -p 32"
COMPRESSION_CMD="zstd -3"
DECOMPRESSION_CMD="zstd -3 -d"
###############################
# Path to binaries
TAR=/bin/tar
MT=/usr/bin/mt-st
MKDIR=/bin/mkdir
MBUFFER=/usr/bin/mbuffer
OPENSSL=/usr/bin/openssl
PIPEMETER=/usr/bin/pipemeter
COMPRESSION=/usr/bin/zstd
UUENCODE=/usr/bin/uuencode
#endof cfg
###############################
WORKDIR=$PWD
SCRIPT=$(readlink -f $0 2>/dev/null)
# Get todays day like Mon, Tue and so on
NOW=$(date +"%a")
wait_for_next_tape() {
sendmail_event NEXTTAPE
eject_tape
echo_log "wait for tape"
while true
do
mt -f /dev/st0 status | grep ONLINE >/dev/null
rt=$?
if [[ $rt -eq 0 ]]
then
break;
fi
sleep 2
done
}
eject_tape() {
[ -e $MT ] && mt -f /dev/st0 status | grep ONLINE >/dev/null
rt=$?
if [[ $rt -eq 0 ]]
then
[ -e $MT ] && $MT -f $TAPE rewind
[ -e $MT ] && $MT -f $TAPE eject
fi
}
###############################################################################
# log helper functions
LOGDELIM="###############################################################################"
cleanup_log() {
echo "" > $LOGFILE
echo "" > $FILELIST
}
echo_log() {
>&1 echo $@
echo $@ >> $LOGFILE
}
error() {
error_log $@
sendmail_event FAIL
#exit -1
}
error_log() {
echo_log "$LOGDELIM"
echo_log "BACKUP FAILED"
echo_log "$LOGDELIM"
echo_log "reason: $@"
echo_log "$LOGDELIM"
}
###############################################################################
###############################################################################
# helper function for tape change
calc_time()
{
seconds=$( calc "(( $1 ) % 60 )" )
minutes=$( calc "(( $1 ) / 60 ) % 60 " | sed -n "s/\([0-9]*\).*/\1/p" )
hours=$( calc "(( $1 ) / 60 ) / 60 " | sed -n "s/\([0-9]*\).*/\1/p" )
echo "$hours:$minutes:$seconds"
}
sendmail_for_backup_needed()
{
sendmail \
"please start a tapebackup" \
"A tape backup would be needed now\n" \
"$( main estimate-tapes )"
}
estimate_size()
{
cd $NAS_MOUNT_DIR > /dev/null
du -sbc $BACKUP_DIRS | sed -n "s/\([0-8]*\).*/\1/p" | tail -n1
cd - > /dev/null
}
#### Custom functions #####
sendmail_event()
{
case $1 in
FULLSTART)
export start_time=$SECONDS
sendmail "Full Backup started" "Full backup started for $BACKUP_DIRS"
;;
FULLEND)
end_time=$SECONDS
time=$(( end_time - start_time ))
time_str=$( calc_time $time )
if [[ -e $FILELIST ]]
then
tar -cjf $FILELIST.bz2.tar $FILELIST
fi
sendmail \
"finished backup" \
$FILELIST.bz2.tar \
"sucessfull backuped the following dirs $BACKUP_DIRS \n" \
"Time needed was $time_str"
;;
NEXTTAPE)
sendmail "Next Tape please" "test" "Please change tape and not the order."
;;
FAIL)
end_time=$SECONDS
time=$(( end_time - start_time ))
time_str=$( calc_time $time )
sendmail \
"failed to backup" \
$TAPENAMES \
"failed to backup the following dirs $BACKUP_DIRS \n" \
"Time needed was $time_str"
;;
*)
error_log "failed invalid send event"
;;
esac
}
sendmail() {
SUBJECT=$1
shift
local attachments
if [[ -e $LOGFILE ]]
then
attachments+="$UUENCODE $LOGFILE $( basename $LOGFILE) ; "
fi
while true
do
if [[ -e $1 ]]
then
attachments+="$UUENCODE $1 $(basename $1) ; "
shift
else
break
fi
done
BODY=$@
eval "( echo -e '$BODY' ; $attachments ) | mail root --subject=\"$MAIL_PREFRACE $SUBJECT\" $LOG_ATTACHMENT"
echo "( echo -e '$BODY' ; $attachments ) | mail root --subject=\"$MAIL_PREFRACE $SUBJECT\" $LOG_ATTACHMENT"
}
###############################################################################
# mount nas directories
mount_nas() {
if [[ "$HOSTNAME" == "$NAS" ]]
then
echo_log "no need to mount nas"
[[ -e $NAS_MOUNT_DIR ]] || ln -s $MOUNT_LOCAL $NAS_MOUNT_DIR
else
echo_log "mount nas"
$MKDIR $NAS_MOUNT_DIR
sudo mount.nfs $MOUNT_NFS $NAS_MOUNT_DIR
fi
}
###############################################################################
# Make sure all dirs exits
verify() {
local s=0
[ -d $LOGBASE ] || $MKDIR -p $LOGBASE || ( error_log "error could not create log file -> stop" && s=1)
echo_log "verify programs"
[ -e $TAR ] || ( error_log "tar is not installed (curr set to '$TAR' -> stop" && s=1)
[ -e $MT ] || ( error_log "mt is not installed (curr set to '$MT' -> stop" && s=1)
[ -e $MKDIR ] || ( error_log "mkdir is not installed (curr set to '$MKDIR' -> stop" && s=1)
[ -e $MBUFFER ] || ( error_log "mbuffer is not installed (curr set to '$MBUFFER' -> stop" && s=1)
[ -e $OPENSSL ] || ( error_log "opensll is not installed (curr set to '$OPENSSL' -> stop" && s=1)
[ -e $PIPEMETER ] || ( error_log "pipemeter is not installed (curr set to '$PIPEMETER' -> stop" && s=1)
[ -e $COMPRESSION ] || ( error_log "compression is not installed (curr set to '$COMPRESSION' -> stop" && s=1)
[ -e $UUENCODE ] || ( error_log "uuencode is not installed (curr set to '$UUENCODE' -> stop" && s=1)
echo_log "verify dirs"
for d in $BACKUP_DIRS
do
local test_dir
if [[ $d = /* ]]
then
test_dir=$d
else
test_dir=$NAS_MOUNT_DIR/$d
fi
if [ ! -d $test_dir ];
then
echo_log "Error : $test_dir directory does not exits!"
s=1
fi
done
( [ -e $MT ] && [ -e $TAPE ]) || ( error_log "tape is not present at '$TAPE' -> stop" && s=1)
[ -e $KEYFILE ] || ( error_log "key file is empty please generate one with 'openssl rand 512 > $KEYFILE'" && s=1)
# if not; just die
[ $s -eq 1 ] && error "failed -> stop"
}
print_help() {
echo "usage:"
echo "sudo $( basename $SCRIPT ) backup-full | do a full backup of all files to the tape drive"
echo "sudo $( basename $SCRIPT ) estimate-tapes | estimates the number of tapes for the backup"
echo "sudo $( basename $SCRIPT ) tape-list | lists the content of the tape drive"
echo "sudo $( basename $SCRIPT ) recover-data <output dir> [selective file list] | recovers data from the tape drive "
}
#### Main logic ####
# Make sure log dir exits
main() {
echo_log "today is $NOW"
# Okay let us start backup procedure
# If it is monday make a full backup;
# For Tue to Fri make a partial backup
# Weekend no backups
case $1 in
"backup-full")
cleanup_log
mount_nas
verify
backup "backup-full"
;;
"tape-list")
cleanup_log
verify
backup "tape-list"
;;
"tape-restore")
cleanup_log
verify
backup "tape-restore" $@
;;
"estimate-tapes")
verify
backup "estimate-tapes"
;;
*)
print_help
;;
esac
}
calc() {
awk "BEGIN{printf \"%.2f\", $1}"
}
# Make a full backup
backup() {
local old=$(pwd)
case $1 in
backup-full)
cd $NAS_MOUNT_DIR
echo_log "start full backup"
sendmail_event FULLSTART
echo_log "estimate time for $BACKUP_DIRS"
echo_log "please insert tape in the mean time if not already done"
size=$( estimate_size )
echo_log "estimates size is \'$size\'b"
$TAR $TAR_ARGS -cvf - $BACKUP_DIRS 2> $FILELIST | \
pipemeter -s $size -a -b $BLOCKSIZE -l | \
$COMPRESSION_CMD | \
$OPENSSL enc -aes-256-cbc -pass file:$KEYFILE | \
$MBUFFER \
-A "bash -c \"source $SCRIPT; wait_for_next_tape\"" \
-P 95 \
-m $TAPE_BUFF \
-f \
-o $TAPE \
-L \
-s$BLOCKSIZE
rt=$?
if [ ! $rt -eq 0 ]
then
error "tar command failed with $rt"
else
sendmail_event FULLEND
fi
eject_tape
cd $old
;;
tape-list)
;&
tape-restore)
if [[ $1 == tape-restore ]]
then
OPT="x"
OUTPUT_DIR="-C $2"
shift
else
OPT="t"
fi
shift #remove tape-restor rest is files and paths
echo please enter the number of tapes to read
read tapes_count
$MBUFFER -n $tapes_count -i $TAPE \
-A "bash -c \"source $SCRIPT; wait_for_next_tape\"" \
-P 95 \
-m 0.5g \
-f \
-L \
-q \
-s$BLOCKSIZE |
$OPENSSL enc -d -aes-256-cbc -pass file:$KEYFILE |
$DECOMPRESSION_CMD | \
$TAR $TAR_ARGS -${OPT}vf - $OUTPUT_DIR $@ 2> $FILELIST
;;
estimate-tapes)
size=$( estimate_size )
echo $size
size_str=$( numfmt --to iec --format "%8.4f" "$size" )
tapenumer=$( calc "$size/($TAPESIZE)" )
tapetime=$( calc "$size/($TAPESPEED)" )
time_str=$( calc_time $tapetime )
echo_log "estimated tapes are $tapenumer for the $size_str"
echo_log "estimated time is $time_str (not counting the delay of changes)"
echo_log "with the backup of the following dirs $BACKUP_DIRS"
;;
*)
error "unkown backup cmd"
;;
esac
}
#### Main logic ####
# Make sure log dir exits
#sendmail "Full Backup started" "Full backup started for $BACKUP_DIRS"
case "$0" in
*start)
echo "Script is a subshell"
main $@
;;
*)
echo "Script is being sourced"
;;
esac
#> $LOGFILE 2>&1
#main