-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunifi-backup.sh
859 lines (769 loc) · 29.2 KB
/
unifi-backup.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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
#!/bin/bash
VERSION="1.0.6.2"
APP_NAME="unifi-backup.sh"
SCRIPT_URL='https://raw.githubusercontent.com/ryszard-suchocki/unifi-backup/main/unifi-backup.sh'
SCRIPT_DESCRIPTION=""
SCRIPT_LOCATION="${BASH_SOURCE[@]}"
# Realizacja scenariusza:
# przygotowanie do backup - pełny (full, zawsze commit do bazy/podstawy), różnicowy (diff, baza + jedna zależna. Pośrednie "incr" spłaszczone do "diff"(blockpull)), przyrostowa (od ostatniej pełnej,różnicowej lub poprzedniej typu "incr")
# dwa tryby kopii - archiwum (kompresja, dedup) i rsync (czysta kopia, gotowa do rozruchu). Zawsze zrzut konfiguracji VM (konfiguracja do incr,diff lub full - nie zwiera ostatniego incr)
# przewidzenie kolizji nazwowej dysku przy trybie rsync (vmdisk.qcow2 w /folder/A/ i /folder/B/ - dla VM to różne dyski, dla rsync do /folder/C/ nastąpi kolizja).
# --------- problematyczne zrzucenie konfiguracji docelowej. Po przekopiowaniu do jednego folderu, który nie jest docelowy, zrzut wydaje się bezcelowy (trzeba by zmodyfikować cały łańcuch aka "rebase")
#
# domena bez jakiejkowiek kopii
# domena z conajmniej jedną kopią / wykorzystany external snapshot
# domena w ruchu / obsługa virsh
# domena zatrzymana bez kopii/ obsługa przez qemu-img i virt-xml
# domena zatrzymana z kopią / obsługa przez qemu-img i virt-xml
#
# virsh - sprawdzić czy domena istnieje / jest zdefiniowana
# virsh - okreslic aktualnie wykorzystywane dyski (dziala dla offline i online)
# qemu-img - okreslic zaleznosci aka backing files
# utworzyć funkcje generyczną do snapshotu:
# - dla offline dla każdego dysku oddzielnie (zapewnic, by VM sie nie uruchomiła)(image exchange / virt-xml)
# - dla online virsh create-snapshot-as
#
# Utworzyc funkcje generyczna do commitu:
# - flatten - roznicowa (diff)
# - consolidation - pełna (full)
# - kasowanie nieużywanych dysków w starym łańcuchu. Oczyszczenie folderów z plików nie wykorzystywanych w działającej VM.
QEMU_IMG="/usr/bin/qemu-img"
VIRSH="/usr/bin/virsh"
QEMU="/usr/bin/qemu-system-x86_64"
QEMU_IMG_SHARE_FLAG="--force-share"
DEBUG=0
VERBOSE=0
DUMP_STATE=0
QUIESCE=0
BACKUP_DIRECTORY=
SNAPSHOT_PREFIX="bimg"
DUMP_STATE_TIMEOUT=180 # 16GB RAM / 50MB/s = 16 384 / 50 = 328s
DUMP_STATE_DIRECTORY=
PRINT_FILES=0
_ret=0 # return variable
function update()
{
TMP_FILE=$(mktemp -p "" "XXXXX.sh")
curl -s -L "$SCRIPT_URL" > "$TMP_FILE"
NEW_VER=$(grep "^VERSION" "$TMP_FILE" | awk -F'[="]' '{print $3}')
ABS_SCRIPT_PATH=$(readlink -f "$SCRIPT_LOCATION")
if [ "$VERSION" \< "$NEW_VER" ]
then
printf "Updating script \e[31;1m%s\e[0m -> \e[32;1m%s\e[0m\n" "$VERSION" "$NEW_VER"
echo "cp \"$TMP_FILE\" \"$ABS_SCRIPT_PATH\"" > ./updater.sh
echo "rm -f \"$TMP_FILE\"" >> updater.sh
echo "echo Running script again: `basename ${BASH_SOURCE[@]}` $@" >> ./updater.sh
echo "exec \"$ABS_SCRIPT_PATH\" $@" >> ./updater.sh
chmod +x ./updater.sh
chmod +x "$TMP_FILE"
exec ./updater.sh
else
echo "Unifi-backup script: $VERSION (latest)"
rm -f "$TMP_FILE"
fi
rm -f updater.sh
}
update "$@"
#echo "$@"
function print_usage() {
[ -n "$1" ] && (echo "" ; print_v e "$1\n")
cat <<EOU
$APP_NAME version $VERSION - Ryszard Suchocki feat. Davide Guerri
Usage:
$0 [-a <action> -m <mode>] [-b <directory>] [-q|-s <directory>] [-h] [-d] [-v] [-V] [-p] <domain name>|all
Options:
-a <action>
-m <method> Specified action mode/method: backup:[full|diff|incr|enable|disable|showchain] sync:[inplace|full|diff] maintenance:[enter|save|drop]
-b <directory> Copy previous snapshot/base image to the specified <directory> # not yet implemented
-q Use quiescence (qemu agent must be installed in the domain)
-s <directory> Dump domain status in the specified directory
-d Debug
-h Print usage and exit
-v Verbose
-V Print version and exit
-p Print files to backup
Version Requirements:
bash >= 4.3.0
qemu_img >= 1.2.0
qemu >= 1.2.0
virsh >= 0.9.13
EOU
}
function print_v() {
local level=$1
case $level in
v) # Verbose
[ $VERBOSE -eq 1 ] && echo -e "[VVV] ${*:2}"
;;
d) # Debug
[ $DEBUG -eq 1 ] && echo -e "[DBG] ${*:2}"
;;
e) # Error
echo -e "[ERR] ${*:2}"
;;
w) # Warning
echo -e "[WAR] ${*:2}"
;;
*) # Any other level
echo -e "[INF] ${*:2}"
;;
esac
}
# Mutual exclusion management: only one instance of this script can be running
# at one time.
function try_lock() {
local domain_name=$1
exec 29>"/var/lock/$domain_name.fi-backup.lock"
flock -n 29
if [ $? -ne 0 ]; then
return 1
else
return 0
fi
}
function unlock() {
local domain_name=$1
rm "/var/lock/$domain_name.fi-backup.lock"
exec 29>&-
}
function is_domain_running() {
local domain_name=$1
local dom_state=
dom_state=$($VIRSH domstate "$domain_name" 2>&1)
if [ "$dom_state" != "running" ]; then
# print_v w "Warning: Active block commit requires '$domain_name' to be running"
return 1
else
return 0
fi
}
function is_domain_defined() {
tmp=$(virsh dominfo "$1" &>/dev/null)
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
function dump_config() {
local domain_name=$1
local print_path=$2
local dump_path="$(dirname $(virsh domblklist ${domain_name} --details | grep disk | head -n1 | awk '{print $4}'))"
local command="virsh dumpxml ${domain_name}"
print_v d $command
local command_output1=$($command > ${dump_path}/${domain_name}.xml)
local command_output2=$($command --inactive > ${dump_path}/${domain_name}-inactive.xml)
local command_output3=$($command --migratable > ${dump_path}/${domain_name}-migratable.xml)
print_v d $command_output1
print_v d $command_output2
print_v d $command_output3
if [ ! -z $print_path ]; then
echo "${dump_path}/${domain_name}.xml"
fi
}
function dump_state() {
local domain_name=$1
local timestamp=$2
local _ret=
local _timeout=
local _dump_state_filename="$DUMP_STATE_DIRECTORY/$domain_name.statefile-$timestamp.gz"
local output=
output=$($VIRSH qemu-monitor-command "$domain_name" '{"execute": "migrate", "arguments": {"uri": "exec:gzip -c > ' "'$_dump_state_filename'" '"}}' 2>&1)
if [ $? -ne 0 ]; then
print_v e "Failed to dump domain state: '$output'"
return 1
fi
_timeout=5
print_v d "Waiting for dump file '$_dump_state_filename' to be created"
while [ ! -f "$_dump_state_filename" ]; do
_timeout=$((_timeout - 1))
if [ "$_timeout" -eq 0 ]; then
print_v e "Timeout while waiting for dump file to be created"
return 4
fi
sleep 1
print_v d "Still waiting for dump file '$_dump_state_filename' to be created ($_timeout)"
done
print_v d "Dump file '$_dump_state_filename' created"
if [ ! -f "$_dump_state_filename" ]; then
print_v e "Dump file not created ('$_dump_state_filename'), something went wrong! ('$output' ?)"
return 1
fi
_timeout="$DUMP_STATE_TIMEOUT"
print_v d "Waiting for '$domain_name' to be paused"
while true; do
output=$(virsh domstate "$domain_name")
if [ $? -ne 0 ]; then
print_v e "Failed to check domain state"
return 2
fi
if [ "$output" == "paused" ]; then
print_v d "Domain paused!"
break
fi
if [ "$_timeout" -eq 0 ]; then
print_v e "Timeout while waiting for VM to pause: '$output'"
return 3
fi
print_v d "Still waiting for '$domain_name' to be paused ($_timeout)"
sleep 1
_timeout=$((_timeout - 1))
done
return 0
}
function get_block_devices() {
local domain_name=$1
local -n return_var=$2
local _ret=
return_var=()
while IFS= read -r file; do
return_var+=("$file")
done < <($VIRSH -q -r domblklist "$domain_name" --details|awk \
'"disk"==$2 {$1=$2=$3=""; print $0}'|sed 's/^[ \t]*//')
return 0
}
function get_backing_file() {
local file_name=$1
local -n _return_var=$2
local _ret=
local _backing_file=
local _qemu_info=
local version=
# version=$(qemu_version)
#
# grep returns 1 if no matching string found - means error or last file in chain...
#
_qemu_info=$($QEMU_IMG info $QEMU_IMG_SHARE_FLAG "$file_name")
_ret=$?
if [[ $_ret -gt 0 ]]; then
print_v e "Error in getting backing file: Check if running with sufficient permissions (sudo, apparmor status, etc)"
return $_ret
else
_backing_file=$(echo "$_qemu_info" | awk '/^backing file:/ {$1=$2=""; print $0}' | grep -oE "((\/)+[\/._a-zA-Z0-9\-]*)")
fi
_return_var="$_backing_file"
return 0
}
function get_snapshot_chain() {
local endmost_child=$1
local -n return_var=$2
local _parent_backing_file=
local _backing_file=
local i=0
local _ret=
return_var[$i]="$endmost_child"
_ret=1
get_backing_file "$endmost_child" _parent_backing_file
if [ $? -eq 0 ]; then
while [ -e "$_parent_backing_file" ]; do
((i++))
return_var[$i]="$_parent_backing_file"
#get next backing file if it exists
_backing_file="$_parent_backing_file"
get_backing_file "$_backing_file" _parent_backing_file
#print_v d "Next file in backing file chain: '$_parent_backing_file'"
_ret=0
done
fi
return $_ret
}
function commit_offline_domain(){
local domain_name=$1
local base_image=$2
local snap_image=$3
# nie ma opcji commitowania całego łańcucha. Obejscie - blockpull różnic, następnie commit. Qemu-img commit wysyla dane do bezposredniego backing chain.
local command="rebase_offline_domain $domain_name $base_image $snap_image"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
print_v d $command_output
if [ $_ret -eq 0 ]; then
local command="qemu-img commit $snap_image"
print_v d $command
command_output=$(${command} 2>&1)
_ret=$?
if [ $_ret -eq 0 ]; then
return 0
else
return 1
fi
else
return 1
fi
}
function rebase_offline_domain(){
local domain_name=$1
local base_image=$2
local snap_image=$3
command="qemu-img rebase -p -b ${base_image} ${snap_image}"
print_v d $command
command_output=$(${command} 2>&1)
if [ $? -eq 0 ]; then
return 0
else
return 1
fi
}
function snapshot_domain() {
local domain_name=$1
local _ret=0
local backing_file=
local block_device=
local block_devices=
local extra_args=
local command_output=
local new_backing_file=
local parent_backing_file=
local timestamp=
local resume_vm=0
timestamp=$(date "+%Y%m%d-%H%M%S")
print_v d "Snapshot for domain '$domain_name' requested"
print_v d "Using timestamp '$timestamp'"
if ! is_domain_running "${domain_name}"; then
print_v d "Domain ${domain_name} is not running"
get_block_devices "$domain_name" block_devices
for ((i=0; i<"${#block_devices[@]}";i++)); do
local filename=$(basename -- "${block_devices[$i]}")
local extension="${filename##*.}"
local filename="${filename%.*}"
local base_image="${block_devices[$i]}"
print_v d "Base image: $base_image"
local base_path=$(dirname $base_image)
print_v d "Base image path: $base_path"
local snap_image="${base_path}/${filename}.${SNAPSHOT_PREFIX}-${timestamp}"
print_v d "New overlay image: $snap_image"
local target_dev=$(virsh domblklist "$domain_name" --details | grep disk | grep $base_image | awk '{print $3}')
local command="qemu-img create -f qcow2 -o backing_fmt=qcow2,backing_file=$base_image $snap_image"
print_v d $command
command_output=$(${command} 2>&1)
_ret=$?
if [ $_ret -eq 0 ]; then
print_v d "${command_output}"
command="virt-xml $domain_name --remove-device --disk target=${target_dev} --define"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
if [ $_ret -eq 0 ]; then
print_v d "Target device '$target_dev' dismounted"
print_v d "${command_output}"
else
print_v e "Target device '$target_dev' couldn't be dismounted"
print_v e "${command_output}"
fi
command="virt-xml $domain_name --add-device --disk target=${target_dev},driver_type=qcow2,path=${snap_image} --define"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
if [ $_ret -eq 0 ]; then
print_v d "Snapshot image '$snap_image' mounted as target device '$target_dev'"
print_v d "${command_output}"
else
print_v e "Snapshot image '$snap_image' couldn't be mounted as target device '$target_dev'"
print_v e "${command_output}"
fi
else
print_v e "Unable snapshot '$base_image' disk."
print_v e "${command_output}"
fi
done
return $_ret
else
print_v d "Domain ${domain_name} is running"
if [ "$DUMP_STATE" -eq 1 ]; then # Dump VM state
print_v v "Dumping domain state"
dump_state "$domain_name" "$timestamp"
_ret=$?
if [ $_ret -ne 0 ]; then
print_v e \
"Domain state dump failed!"
return 1
else
resume_vm=1 # Should something go wrong, resume the domain
trap 'virsh resume "$domain_name" >/dev/null 2>&1' SIGINT SIGTERM
if [ "$resume_vm" -eq 1 ]; then
print_v d "Resuming domain"
virsh resume "$domain_name" >/dev/null 2>&1
if [ $? -ne 0 ]; then
print_v e "Problem resuming domain '$domain_name'"
return 1
else
print_v v "Domain resumed"
trap "" SIGINT SIGTERM
fi
fi
fi
fi
# Create an external snapshot for each block device
print_v d "Snapshotting block devices for '$domain_name' using suffix '$SNAPSHOT_PREFIX-$timestamp'"
if [ $QUIESCE -eq 1 ]; then
print_v d "Quiesce requested"
extra_args="--quiesce"
fi
print_v v "$VIRSH -q snapshot-create-as $domain_name" \
"$SNAPSHOT_PREFIX-$timestamp --no-metadata --disk-only --atomic "\
"$extra_args"
command_output=$($VIRSH -q snapshot-create-as "$domain_name" \
"$SNAPSHOT_PREFIX-$timestamp" --no-metadata --disk-only --atomic \
$extra_args 2>&1)
_ret=$?
if [ $_ret -eq 0 ]; then
print_v v "Snapshot for block devices of '$domain_name' successful"
else
print_v e \
"Snapshot for '$domain_name' failed! Exit code: $_ret\n'$command_output'"
if [[ $command_output =~ "Permission denied" ]]; then
print_v e "Check apparmor status or check if running with sufficient permissions"
fi
fi
return $_ret
fi
}
function print_backing_chain(){
local domain_name=$1
get_block_devices "$domain_name" block_devices
for ((i=0; i<"${#block_devices[@]}";i++)); do
local snapshot_chain_flat
local target_dev=$(virsh domblklist "$domain_name" --details | grep disk | grep ${block_devices[$i]} | awk '{print $3}')
print_v d "Backing chain for block device: ${target_dev}"
get_snapshot_chain "${block_devices[$i]}" snapshot_chain_flat
for ((j=0 ; j<"${#snapshot_chain_flat[@]}";j++)); do
snapshot_chain[$i,$j]="${snapshot_chain_flat[$j]}"
print_v d "$j)"
echo "${snapshot_chain[$i,$j]}"
done
unset snapshot_chain_flat
done
return 0
}
function purge_backing_files(){
local -n old_backing_files=$1
local -n new_backing_files=$2
local files_to_remove=(`echo ${old_backing_files[@]} ${new_backing_files[@]} | tr ' ' '\n' | sort | uniq -u`)
print_v d "Files to remove: ${files_to_remove}"
print_v d "Files to remove: ${#files_to_remove[@]}"
for ((k=0 ; k<"${#files_to_remove[@]}";k++)); do
local command="rm ${files_to_remove[$k]}"
print_v d $command
local command_output=$($command)
print_v d $command_output
done
}
function flatten_consolidate(){
local domain_name=$1
local request_action=$2
local block_devices=''
local block_devices_modif=''
local backin_file=''
local snapshot_chain_flat=''
local snapshot_chain_flat_modif=''
local block_devices_count=0
local _ret=0
declare -A snapshot_chain
get_block_devices "$domain_name" block_devices
for ((i=0; i<"${#block_devices[@]}";i++)); do
get_snapshot_chain "${block_devices[$i]}" snapshot_chain_flat
for ((j=0 ; j<"${#snapshot_chain_flat[@]}";j++)); do
snapshot_chain[$i,$j]="${snapshot_chain_flat[$j]}"
print_v d "${snapshot_chain[$i,$j]}"
done
if [ ! -z $request_action ] && [ $request_action == "flatten" ]; then # FLATTEN - Block pull to top most file
if [ "${#snapshot_chain_flat[@]}" -gt 2 ]; then
print_v d ""
print_v d "Flattening possible - ${block_devices[$i]} to ${snapshot_chain_flat[(${#snapshot_chain_flat[@]}-2)]}"
if is_domain_running "${domain_name}" && [ $? == 0 ]; then
command="virsh blockpull --domain ${domain_name} --path ${block_devices[$i]} --base ${snapshot_chain_flat[(${#snapshot_chain_flat[@]}-1)]} --verbose --wait"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
print_v d $command_output
else
command="rebase_offline_domain $domain_name ${snapshot_chain_flat[(${#snapshot_chain_flat[@]}-1)]} ${snapshot_chain_flat[0]}"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
print_v d $command_output
fi
if [ $_ret -eq 0 ]; then
get_block_devices "$domain_name" block_devices_modif
get_snapshot_chain "${block_devices_modif[$i]}" snapshot_chain_flat_modif
purge_backing_files snapshot_chain_flat snapshot_chain_flat_modif
fi
print_v d ""
fi
fi
if [ ! -z $request_action ] && [ $request_action == "consolidate" ]; then # CONSOLIDATION - "block commit" to base image. Caution - base image must be writtable.
if [ "${#snapshot_chain_flat[@]}" -gt 1 ]; then
print_v d ""
print_v d "Consolidation possible. ${block_devices[$i]} to ${snapshot_chain_flat[(${#snapshot_chain_flat[@]}-1)]}"
if is_domain_running "${domain_name}" && [ $? == 0 ]; then
command="virsh blockcommit --domain ${domain_name} --path ${block_devices[$i]} --verbose --pivot"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
print_v d $command_output
else
local base_image="${snapshot_chain_flat[(${#snapshot_chain_flat[@]}-1)]}"
print_v d "Base image: $base_image"
local snap_image="${snapshot_chain_flat[0]}"
print_v d "Overlay image: $snap_image"
local target_dev=$(virsh domblklist "$domain_name" --details | grep disk | grep ${snap_image} | awk '{print $3}')
command="commit_offline_domain $domain_name ${base_image} ${snap_image}"
print_v d $command
command_output=$($command 2>&1)
if [ $? -eq 0 ]; then
print_v d "${command_output}"
command="virt-xml $domain_name --remove-device --disk target=${target_dev} --define"
print_v d $command
command_output=$($command 2>&1)
_ret=$?
if [ $_ret -eq 0 ]; then
print_v d "Target device '$target_dev' dismounted"
print_v d "${command_output}"
else
print_v e "Target device '$target_dev' couldn't be dismounted"
print_v e "${command_output}"
fi
command="virt-xml $domain_name --add-device --disk target=${target_dev},driver_type=qcow2,path=${base_image} --define"
print_v d $command
command_output=$($command 2>&1)
if [ $? -eq 0 ]; then
_ret=0
print_v d "Base image '$base_image' mounted as target device '$target_dev'"
print_v d "${command_output}"
else
_ret=1
print_v e "Base image '$base_image' couldn't be mounted as target device '$target_dev'"
print_v e "${command_output}"
return 1
fi
else
_ret=1
fi
fi
if [ $_ret -eq 0 ]; then
get_block_devices "$domain_name" block_devices_modif
get_snapshot_chain "${block_devices_modif[$i]}" snapshot_chain_flat_modif
purge_backing_files snapshot_chain_flat snapshot_chain_flat_modif
fi
print_v d ""
fi
fi
block_devices_count=$(($i+1))
unset snapshot_chain_flat
if [ $_ret -ne 0 ]; then
return 1
fi
done
return 0
}
TEMP=$(getopt -n "$APP_NAME" -o a:m:b:s:qhdvVp --long action:,mode:,backup_dir:,dump_state_dir:,quiesce,help,debug,version,verbose,print-files -- "$@")
if [ $? != 0 ] ; then echo "Failed parsing options." >&2 ; exit 1 ; fi
eval set -- "$TEMP"
while true; do
case "$1" in
-b|--backup_dir)
BACKUP_DIRECTORY=$2
if [ ! -d "$BACKUP_DIRECTORY" ]; then
print_v e "Backup directory '$BACKUP_DIRECTORY' doesn't exist!"
exit 1
fi
shift; shift
;;
-a|--action)
declare -a ACTIONLIST=()
ACTIONLIST=("backup" "sync" "maintenance")
ACTION=$2
if ! [[ " ${ACTIONLIST[*]} " =~ " ${ACTION} " ]]; then
print_usage "-a requires specifying 'backup', 'sync' or 'maintenance' "
exit 1
fi
declare -a MODELIST=()
if [ "$ACTION" == "backup" ]; then
MODELIST=("full" "incr" "diff" "enable" "disable" "showchain")
elif [ "$ACTION" == "sync" ]; then
MODELIST=("inplace" "full" "incr")
elif [ "$ACTION" == "maintenance" ]; then
MODELIST=("enter" "save" "drop")
else
print_usage "-a requires specifying 'backup', 'sync' or 'maintenance' "
fi
shift;shift
;;
-m|--mode)
MODE=$2
if ! [[ " ${MODELIST[*]} " =~ " ${MODE} " ]]; then
print_usage "-m requires specifying proper mode/method."
exit 1
fi
shift;shift
;;
-q|--quiesce)
QUIESCE=1
shift
;;
-p|--print-files)
PRINT_FILES=1
shift
;;
-s|--dump_state_dir)
DUMP_STATE=1
DUMP_STATE_DIRECTORY=$2
if [ ! -d "$DUMP_STATE_DIRECTORY" ]; then
print_v e \
"Dump state directory '$DUMP_STATE_DIRECTORY' doesn't exist!"
exit 1
fi
shift;shift
;;
-d|--debug)
DEBUG=1
VERBOSE=1
shift
;;
-h|--help)
print_usage
exit 1
shift
;;
-v|--verbose)
VERBOSE=1
shift
;;
-V|--version)
echo "$APP_NAME version $VERSION"
shift
exit 0
;;
-- ) shift; break ;;
* ) break ;;
esac
done
DOMAIN_NAME="$1"
if ! is_domain_defined $DOMAIN_NAME; then
print_v e "Domain $DOMAIN_NAME not defined!"
exit 1
fi
try_lock "$DOMAIN_NAME"
_ret=$?
if [ $_ret -ne 0 ]; then
print_v e "Another instance of $0 is already running on '$DOMAIN_NAME'! Skipping backup of '$DOMAIN_NAME'"
fi
if [ "$ACTION" == "backup" ]; then
case $MODE in
"full")
flatten_consolidate "${DOMAIN_NAME}" "consolidate"
_ret=$?
if [ $_ret -eq 0 ]; then
if [ $PRINT_FILES -eq 1 ];then
print_backing_chain "${DOMAIN_NAME}"
dump_config "${DOMAIN_NAME}" "true"
else
dump_config "${DOMAIN_NAME}"
fi
SNAPSHOT_PREFIX="incr"
snapshot_domain "${DOMAIN_NAME}"
_ret=$?
else
_ret=1
fi
;;
"diff")
SNAPSHOT_PREFIX="diff"
snapshot_domain "${DOMAIN_NAME}"
_ret=$?
if [ $_ret -eq 0 ]; then
flatten_consolidate "${DOMAIN_NAME}" "flatten"
_ret=$?
if [ $_ret -eq 0 ]; then
if [ $PRINT_FILES -eq 1 ];then
print_backing_chain "${DOMAIN_NAME}"
dump_config "${DOMAIN_NAME}" "true"
else
dump_config "${DOMAIN_NAME}"
fi
SNAPSHOT_PREFIX="incr"
snapshot_domain "${DOMAIN_NAME}"
_ret=$?
else
_ret=1
fi
else
_ret=1
fi
;;
"incr")
if [ $PRINT_FILES -eq 1 ];then
print_backing_chain "${DOMAIN_NAME}"
dump_config "${DOMAIN_NAME}" "true"
else
dump_config "${DOMAIN_NAME}"
fi
SNAPSHOT_PREFIX="incr"
snapshot_domain "${DOMAIN_NAME}"
_ret=$?
;;
"enable")
if [ $PRINT_FILES -eq 1 ];then
print_backing_chain "${DOMAIN_NAME}"
dump_config "${DOMAIN_NAME}" "true"
else
dump_config "${DOMAIN_NAME}"
fi
SNAPSHOT_PREFIX="incr"
snapshot_domain "${DOMAIN_NAME}"
_ret=$?
;;
"disable")
print_v w " * * * This option will deactivate incremental backup * * * "
print_v w "All changes waiting for commit will be flushed NOW to original disk"
print_v w "Press [CTRL] + [C] to cancel action..."
sleep 5
flatten_consolidate "${DOMAIN_NAME}" "consolidate"
_ret=$?
if [ $PRINT_FILES -eq 1 ];then
print_backing_chain "${DOMAIN_NAME}"
dump_config "${DOMAIN_NAME}" "true"
else
dump_config "${DOMAIN_NAME}"
fi
;;
"showchain")
print_backing_chain "${DOMAIN_NAME}"
_ret=$?
;;
esac
fi
if [ "$ACTION" == "sync" ]; then
echo "Syncing"
case $MODE in
"inplace")
;;
"full")
;;
"incr")
;;
esac
fi
if [ "$ACTION" == "maintenance" ]; then
#echo "Maintenance"
case $MODE in
"enter")
SNAPSHOT_PREFIX="maintenance"
snapshot_domain "${DOMAIN_NAME}"
_ret=$?
;;
"save")
print_v w " * * * This option will save maintenance result * * * "
print_v w "All changes waiting for commit will be flushed NOW to original disk (base)"
print_v w "Whole BACKUP CHAIN will be CONSOLIDATED to the one disk (base)"
print_v w "Press [CTRL] + [C] to cancel action..."
sleep 5
flatten_consolidate "${DOMAIN_NAME}" "consolidate"
_ret=$?
;;
"drop")
;;
esac
fi
unlock $DOMAIN_NAME
exit $_ret