-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.sh
565 lines (499 loc) · 15 KB
/
util.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
log() {
echo "[$(date --rfc-3339=seconds)]: $*" >> $TASK_LOG
if [ -n "${ENABLE_TELEGRAM}" ]; then
curl -s --data "text=$1" --data "chat_id=$TELEGRAM_GROUP_ID" 'https://api.telegram.org/bot'$TELEGRAM_BOT_TOKEN'/sendMessage' > /dev/null
fi
}
check_dependencies() {
if ! [ -x "$(command -v dialog)" ]; then
echo "Please install dialog"
exit 1
fi
if ! [ -x "$(command -v lsscsi)" ]; then
dialog --title "LTO Backup" --msgbox "lsscsi is not installed." $HEIGHT $WIDTH
exit 1
fi
if ! [ -x "$(command -v mbuffer)" ]; then
dialog --title "LTO Backup" --msgbox "mbuffer is not installed." $HEIGHT $WIDTH
exit 1
fi
if ! [ -x "$(command -v mt-st)" ]; then
dialog --title "LTO Backup" --msgbox "mt-st is not installed." $HEIGHT $WIDTH
exit 1
fi
if ! [ -x "$(command -v calc)" ]; then
dialog --title "LTO Backup" --msgbox "calc is not installed." $HEIGHT $WIDTH
exit 1
fi
}
select_tape() {
tape_section=$(dialog \
--backtitle "LTO Backup" \
--title "Tape Type" \
--clear \
--cancel-label "Exit" \
--menu "Please select your tape type:" $HEIGHT $WIDTH 4 \
"1" "LTO3" \
"2" "LTO4" \
"3" "LTO5" \
"4" "LTO6" \
--output-fd 1)
case $tape_section in
1 )
TAPE_SIZE=$LTO3_SIZE
$MT -f $TAPE_DEVICE stoptions scsi2logical
;;
2 )
TAPE_SIZE=$LTO4_SIZE
$MT -f $TAPE_DEVICE stoptions scsi2logical
;;
3 )
TAPE_SIZE=$LTO5_SIZE
;;
4 )
TAPE_SIZE=$LTO6_SIZE
;;
* )
clear
echo "Backup aborted."
exit
;;
esac
}
enable_telegram() {
dialog --title "Telegram" --yesno "Enable telegram notification?" $HEIGHT $WIDTH
rt=$?
case $rt in
0)
ENABLE_TELEGRAM=true
input_telegram_bot_key
;;
esac
}
input_telegram_bot_key() {
TELEGRAM_GROUP_ID=$( text_prompt "Telegram" "Enter the chat id" )
TELEGRAM_BOT_TOKEN=$( text_prompt "Telegram" "Enter the bot token" )
}
detect_tape() {
if lsscsi | grep tape; then
tape_devices=()
i=1
while read device; do
tape_devices+=($i "$device")
(( i++ ))
done < <(ls /dev/nst* -d | grep "/dev/nst[0-9]$")
tape_section=$(dialog \
--backtitle "LTO Backup" \
--title "Device Selection" \
--clear \
--cancel-label "Exit" \
--menu "Please select your tape device:" $HEIGHT $WIDTH 4 \
"${tape_devices[@]}" \
--output-fd 1)
TAPE_DEVICE=${tape_devices[$tape_section]}
$MT -f $TAPE_DEVICE status
rt=$?
if [ $rt -eq 0 ]; then
dialog --title "LTO Backup" --msgbox "Tape drive connected successfully" $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Failed to connect to tape drive" $HEIGHT $WIDTH
exit 1
fi
else
dialog --title "LTO Backup" --msgbox "No SCSI tape device is found." $HEIGHT $WIDTH
fi
}
eject_tape() {
log "Ejecting tape $TAPE_DEVICE"
[ -e $MT ] && $MT -f $TAPE_DEVICE status | grep ONLINE >/dev/null
rt=$?
if [[ $rt -eq 0 ]]
then
[ -e $MT ] && rewind_tape
[ -e $MT ] && $MT -f $TAPE_DEVICE eject
fi
}
rewind_tape() {
$MT -f $TAPE_DEVICE rewind
}
wait_for_tape() {
while true
do
$MT -f $TAPE_DEVICE status | grep ONLINE >/dev/null
rt=$?
if [[ $rt -eq 0 ]]
then
break;
fi
dialog --title "LTO Backup" --msgbox "Please load tape to device and select OK." $HEIGHT $WIDTH
done
log "Tape loaded $TAPE_DEVICE"
}
wait_for_tape_silent() {
log "Waiting new tape in: $TAPE_DEVICE"
while true
do
$MT -f $TAPE_DEVICE status | grep ONLINE >/dev/null
rt=$?
if [[ $rt -eq 0 ]]
then
break;
fi
sleep 2
done
log "Tape loaded $TAPE_DEVICE"
}
wait_for_next_tape() {
eject_tape
wait_for_tape
}
wait_for_next_tape_silent() {
eject_tape
wait_for_tape_silent
}
enable_decryption() {
dialog --title "Encryption" --yesno "Was data on the tape encrypted?" $HEIGHT $WIDTH
rt=$?
case $rt in
0)
ENABLE_ENCRYPTION=true
select_encryption_key
;;
esac
}
select_checksum_file() {
CHECKSUM_FILE=$(dialog \
--backtitle "LTO Backup" \
--title "Checksum File" \
--clear \
--cancel-label "Exit" \
--fselect / $HEIGHT $WIDTH \
--output-fd 1)
if [ -f "$CHECKSUM_FILE" ]; then
dialog --title "LTO Backup" --msgbox "You selected checksum file $CHECKSUM_FILE." $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Checksum file does not exist." $HEIGHT $WIDTH
exit 1
fi
}
select_encryption_key() {
ENCRYPTION_KEY=$(dialog \
--backtitle "LTO Backup" \
--title "Encryption Key" \
--clear \
--cancel-label "Exit" \
--fselect / $HEIGHT $WIDTH \
--output-fd 1)
if [ -f "$ENCRYPTION_KEY" ]; then
dialog --title "LTO Backup" --msgbox "You selected encryption key $ENCRYPTION_KEY." $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Encryption key does not exist." $HEIGHT $WIDTH
exit 1
fi
}
select_source() {
BACKUP_SOURCE=$(dialog \
--backtitle "LTO Backup" \
--title "Source Selection" \
--clear \
--cancel-label "Exit" \
--dselect / $HEIGHT $WIDTH \
--output-fd 1)
if [ -d "$BACKUP_SOURCE" ]; then
dialog --title "LTO Backup" --msgbox "You selected to backup ${BACKUP_SOURCE}." $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Backup folder does not exist." $HEIGHT $WIDTH
exit 1
fi
}
select_destination() {
RESTORE_DESTINATION=$(dialog \
--backtitle "LTO Backup" \
--title "Destination Selection" \
--clear \
--cancel-label "Exit" \
--dselect / $HEIGHT $WIDTH \
--output-fd 1)
if [ -d "$RESTORE_DESTINATION" ]; then
dialog --title "LTO Backup" --msgbox "You selected to restore to ${RESTORE_DESTINATION}." $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Restore folder does not exist." $HEIGHT $WIDTH
exit 1
fi
}
estimate_size() {
du -sh "$BACKUP_SOURCE" | cut -f1
}
estimate_time() {
estimated_seconds=$(calc "$1//($TAPE_SPEED)")
echo $((estimated_seconds/86400))" days "$(date -d "1970-01-01 + $estimated_seconds seconds" "+%H hours %M minutes %S seconds")
}
estimate_raw_size() {
du -sbc "$BACKUP_SOURCE" | cut -f1 | tail -n1
}
confirm() {
dialog --title "Confirmation" --yesno "$1" $HEIGHT $WIDTH
rt=$?
case $rt in
1)
clear
echo "Backup aborted."
exit
;;
esac
}
enable_encryption() {
dialog --title "Encryption" --yesno "Enable encryption?" $HEIGHT $WIDTH
rt=$?
case $rt in
0)
ENABLE_ENCRYPTION=true
dialog --title "Encryption" --yesno "Use existing encryption key?" $HEIGHT $WIDTH
rt=$?
case $rt in
0)
select_encryption_key
;;
1)
ENCRYPTION_KEY="$(date +%m%d%Y_%H%M%S)-$1.key"
$OPENSSL rand 512 > $ENCRYPTION_KEY
dialog --title "LTO Backup" --msgbox "Encryption key generated in ${ENCRYPTION_KEY}." $HEIGHT $WIDTH
;;
esac
;;
esac
}
enable_compression() {
dialog --title "Compression" --yesno "Enable compression?" $HEIGHT $WIDTH
rt=$?
case $rt in
0)
ENABLE_COMPRESSION=true
;;
esac
}
enable_decompression() {
dialog --title "Compression" --yesno "Was compression enabled during backup?" $HEIGHT $WIDTH
rt=$?
case $rt in
0)
ENABLE_COMPRESSION=true
;;
esac
}
text_prompt() {
input_text=$(dialog --title "$1" --backtitle "LTO Backup" --inputbox "$2" $HEIGHT $WIDTH --output-fd 1)
echo $input_text
}
select_task() {
task_section=$(dialog \
--backtitle "LTO Backup" \
--title "Select Task" \
--clear \
--cancel-label "Exit" \
--menu "Please select the task you want to perform:" $HEIGHT $WIDTH 4 \
"1" "Backup" \
"2" "Restore" \
"3" "List Backup" \
"4" "Verify Backup" \
--output-fd 1)
case $task_section in
1 )
select_source
confirm "Confirm backup $BACKUP_SOURCE to $TAPE_DEVICE?"
backup
;;
2 )
select_destination
confirm "Confirm restore $TAPE_DEVICE to $RESTORE_DESTINATION?"
restore
;;
3 )
list_backups
;;
4 )
verify_backup
;;
* )
clear
echo "Backup aborted."
exit
;;
esac
}
prepare_backup() {
# disable drive compression since we use zstd
$MT -f $TAPE_DEVICE compression 0
wait_for_tape
rewind_tape
backup_script
# Clear the log files
> $TASK_LOG
> $BACKUP_FILE_LOG
clear
}
prepare_restore() {
wait_for_tape
rewind_tape
skip_to_data
# Clear the log files
> $TASK_LOG
> $RESTORE_FILE_LOG
clear
}
backup_script() {
# Backup current script and config, allow restore with only tape
echo "ENABLE_COMPRESSION=$ENABLE_COMPRESSION" >> custom.sh
echo "ENABLE_ENCRYPTION=$ENABLE_ENCRYPTION" >> custom.sh
echo "BACKUP_SOURCE=$BACKUP_SOURCE" >> custom.sh
echo "TAPE_DEVICE=$TAPE_DEVICE" >> custom.sh
echo "ENCRYPTION_KEY=$ENCRYPTION_KEY" >> custom.sh
echo "TAPE_SIZE=$TAPE_SIZE" >> custom.sh
$TAR -cvf $TAPE_DEVICE *.sh > /dev/null 2>&1
rm custom.sh
}
skip_to_data() {
# Skip the script portion and jump to file 2
$MT -f $TAPE_DEVICE asf 1
}
backup() {
size=$( estimate_size )
raw_size=$( estimate_raw_size )
tape_required=$(numfmt --to iec --format "%1.0f" $( calc "$raw_size/($TAPE_SIZE)" ))
estimated_time=$( estimate_time $raw_size )
dialog --title "LTO Backup" --msgbox "Estimated backup size: ${size}.\nEstimated tape(s) required: ${tape_required}.\n\nEstimated time to completion: ${estimated_time}." $HEIGHT $WIDTH
label=$( text_prompt "Backup Name" "Enter the name for this backup task" )
if [ -z "$label" ]
then
clear
echo "Backup aborted."
exit
fi
enable_compression
enable_encryption $label
confirm "Run task?"
prepare_backup
log "Backup task: $label"
log "Backup started for $BACKUP_SOURCE to $TAPE_DEVICE"
log "Encryption: $ENABLE_ENCRYPTION"
log "Compression: $ENABLE_COMPRESSION"
backup_label="$label_$(date "+%Y-%m-%d_%N")"
## Start the backup task
$TAR $TAR_ARGS --label="$backup_label" -cvf - "$BACKUP_SOURCE" 2> $BACKUP_FILE_LOG | \
tee >(sha512sum > $backup_label.sha512) >(tar -tv > $backup_label.lst) | \
( [ -z "$ENABLE_COMPRESSION" ] && cat || $COMPRESSION_CMD ) | \
( [ -z "$ENABLE_ENCRYPTION" ] && cat || $OPENSSL enc $ENCRYPT_CMD -pass file:$ENCRYPTION_KEY ) | \
$MBUFFER \
-A "bash -c \"TASK_LOG=$TASK_LOG TAPE_DEVICE=$TAPE_DEVICE; MT=$MT; source util.sh; wait_for_next_tape_silent\"" \
-P 95 \
-m $TAPE_BUFFER_SIZE \
-f \
-o $TAPE_DEVICE \
-L \
-s $BLOCK_SIZE
rt=$?
if [ ! $rt -eq 0 ]
then
log "Backup failed $rt"
dialog --title "LTO Backup" --msgbox "Backup failed tar $rt." $HEIGHT $WIDTH
else
log "Backup finished"
dialog --title "LTO Backup" --msgbox "Backup finished successfully. Checksum file: $backup_label.sha512sum" $HEIGHT $WIDTH
fi
eject_tape
}
restore() {
tapes_count=$( text_prompt "Tape Count" "Enter the number of tapes used for restore" )
if [ -z "$tapes_count" ]
then
clear
echo "Restore aborted."
exit
fi
enable_decompression
enable_decryption
confirm "Run task?"
prepare_restore
log "Restore started for $TAPE_DEVICE to $RESTORE_DESTINATION with $tapes_count tapes"
$MBUFFER -n $tapes_count -i $TAPE_DEVICE \
-A "bash -c \"TASK_LOG=$TASK_LOG TAPE_DEVICE=$TAPE_DEVICE; MT=$MT; source util.sh; wait_for_next_tape_silent\"" \
-P 100 \
-m $TAPE_BUFFER_SIZE \
-f \
-L \
-q \
-s $BLOCK_SIZE |
( [ -z "$ENABLE_ENCRYPTION" ] && cat || $OPENSSL enc $DECRYPT_CMD -pass file:$ENCRYPTION_KEY ) | \
( [ -z "$ENABLE_COMPRESSION" ] && cat || $DECOMPRESSION_CMD ) | \
$TAR $TAR_ARGS -xvf - -C "$RESTORE_DESTINATION" | tee $RESTORE_FILE_LOG
rt=$?
if [ ! $rt -eq 0 ]
then
dialog --title "LTO Backup" --msgbox "Restore failed $rt." $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Restore finished successfully." $HEIGHT $WIDTH
fi
eject_tape
}
list_backups() {
tapes_count=$( text_prompt "Tape Count" "Enter the number of tapes used for restore" )
if [ -z "$tapes_count" ]
then
clear
echo "Restore aborted."
exit
fi
enable_decompression
enable_decryption
confirm "Run task?"
wait_for_tape
rewind_tape
skip_to_data
$MBUFFER -n $tapes_count -i $TAPE_DEVICE \
-A "bash -c \"TASK_LOG=$TASK_LOG TAPE_DEVICE=$TAPE_DEVICE; MT=$MT; source util.sh; wait_for_next_tape_silent\"" \
-P 100 \
-m $TAPE_BUFFER_SIZE \
-f \
-L \
-q \
-s $BLOCK_SIZE |
( [ -z "$ENABLE_ENCRYPTION" ] && cat || $OPENSSL enc $DECRYPT_CMD -pass file:$ENCRYPTION_KEY ) | \
( [ -z "$ENABLE_COMPRESSION" ] && cat || $DECOMPRESSION_CMD ) | \
$TAR $TAR_ARGS -tvf - -C $(mktemp -d) | awk '{print $NF}' | dialog --programbox "File List" $HEIGHT $WIDTH
eject_tape
}
verify_backup() {
tapes_count=$( text_prompt "Tape Count" "Enter the number of tapes used for verification" )
if [ -z "$tapes_count" ]
then
clear
echo "Verification aborted."
exit
fi
enable_decompression
enable_decryption
select_checksum_file
confirm "Run task?"
wait_for_tape
rewind_tape
skip_to_data
$MBUFFER -n $tapes_count -i $TAPE_DEVICE \
-A "bash -c \"TASK_LOG=$TASK_LOG TAPE_DEVICE=$TAPE_DEVICE; MT=$MT; source util.sh; wait_for_next_tape_silent\"" \
-P 100 \
-m $TAPE_BUFFER_SIZE \
-f \
-L \
-q \
-s $BLOCK_SIZE |
( [ -z "$ENABLE_ENCRYPTION" ] && cat || $OPENSSL enc $DECRYPT_CMD -pass file:$ENCRYPTION_KEY ) | \
( [ -z "$ENABLE_COMPRESSION" ] && cat || $DECOMPRESSION_CMD ) | \
sha512sum -c $CHECKSUM_FILE
rt=$?
if [ ! $rt -eq 0 ]
then
dialog --title "LTO Backup" --msgbox "Checksum check failed $rt." $HEIGHT $WIDTH
else
dialog --title "LTO Backup" --msgbox "Checksum verification OK." $HEIGHT $WIDTH
fi
eject_tape
}