Repository navigation
Expand file tree
/
Copy pathlib.sh
More file actions
694 lines (625 loc) · 25.5 KB
/
Copy pathlib.sh
File metadata and controls
694 lines (625 loc) · 25.5 KB
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
#!/bin/sh
# ------------------------------------------------------------
# lib.sh – Shared functions
# ------------------------------------------------------------
# ------------------------------------------------------------
# Prevent multiple loads of the library
# ------------------------------------------------------------
[ -n "${LIB_SH_LOADED}" ] && return
LIB_SH_LOADED=1
readonly LIB_SH_LOADED
# ------------------------------------------------------------
# Utility helpers
# ------------------------------------------------------------
log_internal() {
if [ -n "${BACKUP_LOG_FILE:-}" ] && [ -e "${BACKUP_LOG_FILE}" ]
then
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "${*}" | tee -a "${BACKUP_LOG_FILE}"
else
# BACKUP_LOG_FILE not set yet (e.g. missing .env) — stdout only
printf '%s - %s\n' "$(date '+%Y-%m-%d %H:%M:%S')" "${*}"
fi
}
log() {
log_internal "${@}"
}
die() {
EXIT_CODE_TRAP=${?}
[ "${EXIT_CODE_TRAP}" -eq 0 ] && EXIT_CODE_TRAP=1
log_internal "${*}"
exit ${EXIT_CODE_TRAP}
unset EXIT_CODE_TRAP
}
fail_internal() {
EXIT_CODE_TRAP=${?}
[ "${EXIT_CODE_TRAP}" -eq 0 ] && EXIT_CODE_TRAP=1
printf '%s\n' "${*}"
exit ${EXIT_CODE_TRAP}
unset EXIT_CODE_TRAP
}
is_writable() {
if [ ! -w "${1}" ]
then
die "${1} is not writable"
fi
}
# ------------------------------------------------------------
# Security
# ------------------------------------------------------------
die_privileged() {
die "Error: This script must not be run as root or with sudo (mass file/dir removal)."
}
# ------------------------------------------------------------
# Block sudo
# ------------------------------------------------------------
sudo() {
die_privileged
}
# ------------------------------------------------------------
# Block doas
# ------------------------------------------------------------
doas() {
die_privileged
}
# ------------------------------------------------------------
# Block elevated privilege execution
# ------------------------------------------------------------
block_root() {
if [ "$(id -u)" -eq 0 ]
then
die_privileged
fi
}
block_root
# ------------------------------------------------------------
# Global variables
# ------------------------------------------------------------
# ------------------------------------------------------------
# Script/base dir
# ------------------------------------------------------------
get_base_dir() {
if [ -f "${0}" ]
then
# $0 points to a file on disk
printf '%s\n' "$(cd -- "$(dirname -- "${0}")" && pwd -P)"
else
# The script was sourced in a shell where $0 is not the script path
# Fallback to the current working directory
printf '%s\n' "$(pwd -P)"
fi
}
CURRENT_BACKUP_DIR="/tmp"
BASE_DIR="$(get_base_dir)"
# ------------------------------------------------------------
# Fail if a variable value is blank or unsafe
# Rejects:
# - blank/unset values
# - values made only of '/' characters ('/', '////')
# - relative paths ('backup', 'foo/bar')
# - unexpanded variable expansions ('$HOME', '${VAR}')
# - tilde home shorthands ('~/backup')
# - '..' path traversal ('../x', '/a/../b')
# - multiple slashes in a row ('//', '/a//b')
# - shell metacharacters (backticks, parentheses, braces, brackets)
# ------------------------------------------------------------
check_path() {
VAR_PTR="${1}"
VAR_VALUE="${2}"
USERS_HOME="${HOME}"
# 1. Blank or unset
[ -z "${VAR_VALUE}" ] && die "${VAR_PTR} is blank"
# 1.1 Newline
case "${VAR_VALUE}" in
*'\n'*) die "${VAR_VALUE} contains a newline!" ;;
*) ;;
esac
# 2. Refers to the user’s home directory
if [ "${VAR_VALUE}" = "${USERS_HOME}" ] || [ "${VAR_VALUE}" = "${USERS_HOME}/" ]
then
die "${VAR_PTR} refers user's home directory"
fi
# 3. Unexpanded variable expansion (contains a literal '$')
case "${VAR_VALUE}" in
*\$*) die "${VAR_PTR} contains an unexpanded variable expansion (${VAR_VALUE})";;
esac
# 4. Tilde shorthand
case "${VAR_VALUE}" in
*~*) die "${VAR_PTR} contains a tilde '~' — use an absolute path (${VAR_VALUE})";;
esac
# 5. Path traversal via '..'
case "${VAR_VALUE}" in
*..*) die "${VAR_PTR} contains '..' — path traversal is not allowed (${VAR_VALUE})";;
esac
# 6. Multiple slashes in a row
case "${VAR_VALUE}" in
*//*) die "${VAR_PTR} contains multiple slashes in a row (${VAR_VALUE})";;
esac
# 7. Must start with a slash (absolute path)
case "${VAR_VALUE}" in
/*) ;;
*) die "${VAR_PTR} is not an absolute path (${VAR_VALUE})";;
esac
# 7.1 No trailing slash
case "${VAR_VALUE}" in
*/) die "${VAR_PTR} must not end with '/' (${VAR_VALUE})";;
esac
# 8. Unsafe characters – allow only alphanum, '/', '_', '.', '-'
if ! printf '%s\n' "${VAR_VALUE}" | grep -qE '^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/][ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz/0123456789._-]+$'
then
die "${VAR_PTR} contains unsafe characters (${VAR_VALUE}) — only alphanumeric, '/', '_', '.', and '-' are allowed"
fi
# 9. Value made only of slashes
case "$VAR_VALUE" in
*[!/]*) ;;
*) die "$VAR_PTR contains only slashes ($VAR_VALUE)";;
esac
}
# ------------------------------------------------------------
# Configure user's uri_default for qemu:///system
# ------------------------------------------------------------
check_libvirt() {
virsh version >/dev/null 2>&1 || die "Cannot reach libvirt (is libvirtd running?)"
if [ ! -f "$HOME/.config/libvirt/libvirt.conf" ] || ! grep -q '^uri_default *= *"qemu:///system"' "$HOME/.config/libvirt/libvirt.conf"
then
export LIBVIRT_DEFAULT_URI="qemu:///system"
fi
}
# ------------------------------------------------------------
# Check if qemu-img is installed
# ------------------------------------------------------------
check_qemu_img() {
qemu-img --version >/dev/null 2>&1 || die "Cannot reach qemu-img (is qemu-img installed?)"
}
# ------------------------------------------------------------
# Check if rsync is installed
# ------------------------------------------------------------
check_rsync() {
rsync --version >/dev/null 2>&1 || die "Cannot reach rsync (is rsync installed?)"
}
# ------------------------------------------------------------
# Check if all of the mandatory variables are set in the environment
# ------------------------------------------------------------
check_mandatory_variables_set() {
# .env.template is a single source of truth for mandatory variables
MANDATORY_VARIABLES_NAMES="$(awk -F= '!/^#/ && !/^$/ {print $1}' "${BASE_DIR}/.env.template")" || die "Failed to extract variables names from ${BASE_DIR}/.env.template"
# set -f prevents pathname expansion of names read from .env.template
# Save and restore the previous state instead of blindly unsetting it.
case "${-}" in
*f*)
HAD_F=1
;;
*)
HAD_F=0
set -f
;;
esac
for VAR_PTR in ${MANDATORY_VARIABLES_NAMES}
do
# Names from .env.template are interpolated via eval below, so they
# must be valid shell identifiers before being used.
# Explicit enumeration avoids locale-dependent bracket ranges
# (e.g. [A-Z] does not reliably mean ASCII A-Z in UTF-8 locales).
if ! printf '%s\n' "${VAR_PTR}" | grep -qE '^[ABCDEFGHIJKLMNOPQRSTUVWXYZ][ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_]+$'
then
die "Invalid variable name in .env.template: ${VAR_PTR}"
fi
VAR_VALUE=""
VAR_VALUE="$(env | awk -v VAR_PTR="${VAR_PTR}" -F '=' '$1==VAR_PTR { print $2}')"
if [ -z "${VAR_VALUE}" ]
then
die "Mandatory variable ${VAR_PTR} is not defined or blank"
fi
if [ "${VAR_PTR}" = "BACKUP_DIR" ] || [ "${VAR_PTR}" = "ANOTHER_SERVER_ANOTHER_BACKUP_DIR" ]
then
check_path "${VAR_PTR}" "${VAR_VALUE}"
fi
unset VAR_VALUE
done
if [ "${HAD_F}" -eq 0 ]
then
set +f
fi
# Clean up variables to mimic local scoping
unset MANDATORY_VARIABLES_NAMES VAR_PTR VAR_VALUE HAD_F
# DAYS_TO_KEEP_BACKUPS feeds `find -mtime`: a leading '+' is mandatory for
# the 'older than N days' semantic (a bare number would match a 24h window,
# e.g. 0 would delete everything modified in the last day)
if ! printf '%s\n' "${DAYS_TO_KEEP_BACKUPS}" | grep -qE '^\+[0123456789]+$'
then
die "DAYS_TO_KEEP_BACKUPS must be of the form '+N' (older than N days), got '${DAYS_TO_KEEP_BACKUPS}'"
fi
if ! printf '%s\n' "${BACKUP_TIMEOUT_SECONDS}" | grep -qE '^[0123456789]+$'
then
die "BACKUP_TIMEOUT_SECONDS must be a plain number of seconds, got '${BACKUP_TIMEOUT_SECONDS}'"
fi
if ! printf '%s\n' "${QEMU_IMG_CONVERT_WITH_COMPRESSION}" | grep -qE '^[01]$'
then
die "QEMU_IMG_CONVERT_WITH_COMPRESSION must be 0 or 1, got '${QEMU_IMG_CONVERT_WITH_COMPRESSION}'"
fi
for VM_NAME in ${VM_NAMES_TO_BACK_UP}
do
if ! printf '%s\n' "${VM_NAME}" | grep -qE '^[ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz][ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789._-]*$'
then
die "Check if ${VM_NAME} in VM_NAMES_TO_BACK_UP is a typo"
fi
done
unset VM_NAME
if ! printf '%s\n' "${ANOTHER_SERVER_IP}" | grep -qE '^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$'
then
die "ANOTHER_SERVER_IP is not a valid IP v4 (${ANOTHER_SERVER_IP}). If you do not rc.sh, fill ANOTHER_SERVER_IP with the default 127.0.0.2 in .env"
fi
}
# ------------------------------------------------------------
# Environment loader
# ------------------------------------------------------------
environment() {
# BASE_DIR is set up the call stack
# Loads environment variables from .env
ENV_FILE="${BASE_DIR}/.env"
if [ ! -f "${ENV_FILE}" ]
then
die "No ${ENV_FILE} file found, craft one from ${ENV_FILE}.template"
fi
# allexport .env
case "${-}" in
*a*)
HAD_A=1
;;
*)
HAD_A=0
set -a
;;
esac
# source: not found workaround
. "${ENV_FILE}"
[ "${HAD_A}" -eq 0 ] && set +a
# allexport .env
unset ENV_FILE HAD_A
check_mandatory_variables_set
check_libvirt
check_qemu_img
export LC_ALL=C
CURRENT_BACKUP_DIR="${BACKUP_DIR}/$(date +%Y-%m-%d)"
# Uncomment following line to observe locale issues
#export LANGUAGE=ru_RU:ru LANG=ru_RU.UTF-8 LC_ALL=ru_RU.UTF-8
}
# ------------------------------------------------------------
# Backup dirs and log file
# ------------------------------------------------------------
create_backup_dirs_and_log() {
# Creates a local backup dir if missing
mkdir -p "${BACKUP_DIR}" || die "Can not create ${BACKUP_DIR}"
is_writable "${BACKUP_DIR}"
mkdir -p "${ANOTHER_SERVER_ANOTHER_BACKUP_DIR}" || die "Can not create ${ANOTHER_SERVER_ANOTHER_BACKUP_DIR}"
is_writable "${ANOTHER_SERVER_ANOTHER_BACKUP_DIR}"
touch "${BACKUP_LOG_FILE}" || die "Can not create ${BACKUP_LOG_FILE}"
}
get_disk_actual_free_space() {
# BACKUP_DIR is set up the call stack
DF_OUTPUT="$(df --portability --block-size=1 "${BACKUP_DIR}")" || die "Failed to calculate free disk space in ${BACKUP_DIR}"
DISK_ACTUAL_FREE_SPACE="$(printf '%s\n' "${DF_OUTPUT}" | awk -v target="Available" 'NR==1 { for(i=1;i<=NF;i++) if($i==target) col=i } NR==2 { print $col }')"
if ! printf '%d\n' "${DISK_ACTUAL_FREE_SPACE}" | grep -qE '^[0123456789]+$'
then
die "Could not parse free disk space for ${BACKUP_DIR}"
fi
printf '%d\n' "${DISK_ACTUAL_FREE_SPACE}"
unset DF_OUTPUT DISK_ACTUAL_FREE_SPACE
}
create_current_backup_dir() {
# Creates a ${BACKUP_DIR}/YYYY-mm-dd for the current run of the script
check_path "CURRENT_BACKUP_DIR" "${CURRENT_BACKUP_DIR}"
mkdir -p "${CURRENT_BACKUP_DIR}" || die "Can not create ${CURRENT_BACKUP_DIR}"
is_writable "${CURRENT_BACKUP_DIR}"
}
# ------------------------------------------------------------
# Marker/lock file
# ------------------------------------------------------------
check_running() {
# if marker/lock file ${RUNNING_FILE} exists
if [ -e "${RUNNING_FILE}" ]
then
die "Another copy of this file may be running. Stop it, remove ${RUNNING_FILE} and repeat. Exiting"
fi
}
create_running() {
# creates a marker/lock file ${RUNNING_FILE}
touch "${RUNNING_FILE}"
is_writable "${RUNNING_FILE}"
}
rm_running() {
# removes the marker/lock file ${RUNNING_FILE}
rm -f "${RUNNING_FILE}"
}
# ------------------------------------------------------------
# Pipe (|) separated VM disk list
# ------------------------------------------------------------
get_vm_disk_names_and_absolute_paths() {
# Extract disk name | absolute path to disk file
# Filter on the Device column ('disk') to skip cdrom and avoid false
# positives from source paths containing the word 'disk'
DISK_FILES=""
DISK_FILES="$(virsh domblklist --details "${1}" | awk '$2 == "disk" && $4 != "-" {print $3 "|" $4}')"
[ -z "${DISK_FILES}" ] && die "Could not parse disk list for ${1}"
printf '%s\n' "${DISK_FILES}"
unset DISK_FILES
}
# ------------------------------------------------------------
# Extract actual-size in bytes from `qemu-img info --force-share --output=json ...`
# ------------------------------------------------------------
get_vm_disk_actual_size() {
ACTUAL_JSON="$(qemu-img info --force-share --output=json "${1}")" || die "Failed to get ${1} actual-size"
ACTUAL_SIZE=$(printf '%s\n' "${ACTUAL_JSON}" | awk -F'[: ,]+' '$2 == "\"actual-size\"" {v=$3} END {print v}')
if ! printf '%d\n' "${ACTUAL_SIZE}" | grep -qE '^[0123456789]+$'
then
die "Could not parse actual-size for ${1}"
fi
printf '%d\n' "${ACTUAL_SIZE}"
unset ACTUAL_JSON ACTUAL_SIZE
}
# ------------------------------------------------------------
# Online (live, running VM) backup
# ------------------------------------------------------------
online_backup() {
# VM_BACKUP_DIR VM_NAME is set up the call stack
VM_DISKS_FILE="${VM_BACKUP_DIR}/disks.psv"
BACKUP_TASK_FILE="${VM_BACKUP_DIR}/${VM_NAME}-backup-job-descriptor.xml"
# Build the backup job descriptor in several heredoc steps
cat > "${BACKUP_TASK_FILE}" <<EOF
<domainbackup>
<disks>
EOF
DISK_NAME=""
DISK_FILE_ABSOLUTE_PATH=""
while IFS='|' read -r DISK_NAME DISK_FILE_ABSOLUTE_PATH
do
DISK_FILE_NAME=""
DISK_FILE_NAME="$(basename "${DISK_FILE_ABSOLUTE_PATH}")"
TARGET_DISK_FILE_ABSOLUTE_PATH="${VM_BACKUP_DIR}/${DISK_FILE_NAME}"
# Workaround target file permissions
TARGET_DISK_CAPACITY=0
TARGET_DISK_CAPACITY="$(virsh domblkinfo "${VM_NAME}" "${DISK_NAME}" | awk '$1 == "Capacity:" {print $2}')"
if [ -z "${TARGET_DISK_CAPACITY}" ]
then
die "Failed to get capacity for ${VM_NAME} ${DISK_NAME}"
fi
qemu-img create -f qcow2 -o compression_type=zstd "${TARGET_DISK_FILE_ABSOLUTE_PATH}" "${TARGET_DISK_CAPACITY}" || die "Failed to create a target file for ${TARGET_DISK_FILE_ABSOLUTE_PATH}"
cat >> "${BACKUP_TASK_FILE}" <<EOF
<disk name='${DISK_NAME}' type='file'>
<target file='${TARGET_DISK_FILE_ABSOLUTE_PATH}'/>
<driver type='qcow2'/>
</disk>
EOF
unset DISK_FILE_NAME TARGET_DISK_FILE_ABSOLUTE_PATH TARGET_DISK_CAPACITY
done < "${VM_DISKS_FILE}"
cat >> "${BACKUP_TASK_FILE}" <<EOF
</disks>
</domainbackup>
EOF
unset DISK_NAME DISK_FILE_ABSOLUTE_PATH
# launch backup
virsh backup-begin "${VM_NAME}" --reuse-external --backupxml "${BACKUP_TASK_FILE}" || die "Failed to start backup for ${VM_NAME}"
# wait completion (bounded by BACKUP_TIMEOUT_SECONDS)
BACKUP_DEADLINE=$(( $(date +%s) + BACKUP_TIMEOUT_SECONDS ))
while :
do
# job complete?
virsh domjobinfo "${VM_NAME}" | grep -q "None" && break
# timeout?
if [ "$(date +%s)" -ge "${BACKUP_DEADLINE}" ]
then
log "Backup job for ${VM_NAME} did not finish within ${BACKUP_TIMEOUT_SECONDS}s"
cleanup_on_exit
fi
# wait
sleep 10
done
unset BACKUP_DEADLINE
if [ "${QEMU_IMG_CONVERT_WITH_COMPRESSION}" = "1" ]
then
for backup_to_shrink in "${VM_BACKUP_DIR}"/*.qcow2
do
[ -f "${backup_to_shrink}" ] || continue
SHRUNK_BACKUP="${backup_to_shrink}-shrunk"
log "Converting ${backup_to_shrink} to ${SHRUNK_BACKUP}"
qemu-img convert -O qcow2 -o compression_type=zstd -c "${backup_to_shrink}" "${SHRUNK_BACKUP}" || die "Failed to convert ${backup_to_shrink} to ${SHRUNK_BACKUP}"
rm "${backup_to_shrink}" || die "Failed to remove ${backup_to_shrink}"
unset SHRUNK_BACKUP
done
fi
unset VM_DISKS_FILE BACKUP_TASK_FILE
}
# ------------------------------------------------------------
# Offline (shut down VM) backup
# ------------------------------------------------------------
offline_backup() {
# VM_BACKUP_DIR is set up the call stack
VM_DISKS_FILE="${VM_BACKUP_DIR}/disks.psv"
DISK_NAME=""
DISK_FILE_ABSOLUTE_PATH=""
while IFS='|' read -r DISK_NAME DISK_FILE_ABSOLUTE_PATH
do
DISK_FILE_NAME=""
DISK_FILE_NAME="$(basename "${DISK_FILE_ABSOLUTE_PATH}")"
TARGET_DISK_FILE_ABSOLUTE_PATH="${VM_BACKUP_DIR}/${DISK_FILE_NAME}"
if [ "${QEMU_IMG_CONVERT_WITH_COMPRESSION}" = "1" ]
then
TARGET_DISK_FILE_ABSOLUTE_PATH="${TARGET_DISK_FILE_ABSOLUTE_PATH}-shrunk"
qemu-img convert -O qcow2 -o compression_type=zstd -c "${DISK_FILE_ABSOLUTE_PATH}" "${TARGET_DISK_FILE_ABSOLUTE_PATH}" || die "Failed to convert ${DISK_FILE_ABSOLUTE_PATH} to ${TARGET_DISK_FILE_ABSOLUTE_PATH}"
else
qemu-img convert -O qcow2 -o compression_type=zstd "${DISK_FILE_ABSOLUTE_PATH}" "${TARGET_DISK_FILE_ABSOLUTE_PATH}" || die "Failed to convert ${DISK_FILE_ABSOLUTE_PATH} to ${TARGET_DISK_FILE_ABSOLUTE_PATH}"
fi
log "Done converting ${DISK_FILE_ABSOLUTE_PATH} to ${TARGET_DISK_FILE_ABSOLUTE_PATH}"
unset DISK_FILE_NAME TARGET_DISK_FILE_ABSOLUTE_PATH
done < "${VM_DISKS_FILE}"
unset VM_DISKS_FILE DISK_NAME DISK_FILE_ABSOLUTE_PATH
}
# ------------------------------------------------------------
# Back the VM up
# ------------------------------------------------------------
backup_vm() {
# Exports VM configuration (XML) and copies disks
VM_NAME="${1}"
log "${VM_NAME} backup start"
# Per-VM dir in the ${CURRENT_BACKUP_DIR}
VM_BACKUP_DIR="${CURRENT_BACKUP_DIR}/${VM_NAME}"
# Capture VM state once and reuse it below: polling `virsh domstate` per-disk
# (and again after the loop) could see a state flip mid-run (VM started or
# stopped), which would mix offline disk copies and live backup jobs for a
# single VM
VM_STATE=""
VM_STATE="$(virsh domstate "${VM_NAME}")" || die "Failed to get ${VM_NAME} state"
if printf '%s\n' "${VM_STATE}" | grep -q "running"
then
log "${VM_NAME} is running, will use a live backup job"
online_backup
elif printf '%s\n' "${VM_STATE}" | grep -q "paused"
then
log "${VM_NAME} is paused, skipping"
return 0
else
log "${VM_NAME} is not running, will use an offline backup"
offline_backup
fi
unset VM_NAME VM_BACKUP_DIR VM_STATE
}
# ------------------------------------------------------------
# Export VM and disk configuration
# exports VM configuration to XML
# exports VM disk configuration to PSV
#
# VM_NAMES_TO_BACK_UP is set in the .env
# CURRENT_BACKUP_DIR is set in the environment
# ------------------------------------------------------------
export_vm_and_disk_configuration() {
log "Export VM and disk configuration start"
for VM_NAME in ${VM_NAMES_TO_BACK_UP}
do
# Per-VM dir in the ${CURRENT_BACKUP_DIR}
VM_BACKUP_DIR="${CURRENT_BACKUP_DIR}/${VM_NAME}"
check_path "VM_BACKUP_DIR" "${VM_BACKUP_DIR}"
mkdir -p "${VM_BACKUP_DIR}" || die "Failed to create ${VM_BACKUP_DIR}"
# Dump VM config
virsh dumpxml --migratable "${VM_NAME}" > "${VM_BACKUP_DIR}/${VM_NAME}.xml" || die "Failed to dump an XML config for ${VM_NAME}"
# Collect VM disk file paths to PSV file
VM_DISKS_FILE="${VM_BACKUP_DIR}/disks.psv"
get_vm_disk_names_and_absolute_paths "${VM_NAME}" > "${VM_DISKS_FILE}"
unset VM_BACKUP_DIR VM_DISKS_FILE
done
log "Export VM and disk configuration finish"
}
# ------------------------------------------------------------
# Check available disk space
# Sums actual-size of all VM disks, compares to available at df "${CURRENT_BACKUP_DIR}"
#
# VM_NAMES_TO_BACK_UP is set in the .env
# CURRENT_BACKUP_DIR is set in the environment
# ------------------------------------------------------------
check_available_disk_space() {
log "Check available disk space start"
ALL_DISKS_ACTUAL_SIZE=0
for VM_NAME in ${VM_NAMES_TO_BACK_UP}
do
VM_BACKUP_DIR="${CURRENT_BACKUP_DIR}/${VM_NAME}"
VM_DISKS_FILE="${VM_BACKUP_DIR}/disks.psv"
DISK_NAME=""
DISK_FILE_ABSOLUTE_PATH=""
while IFS='|' read -r DISK_NAME DISK_FILE_ABSOLUTE_PATH
do
DISK_ACTUAL_SIZE=$(get_vm_disk_actual_size "${DISK_FILE_ABSOLUTE_PATH}")
ALL_DISKS_ACTUAL_SIZE=$((ALL_DISKS_ACTUAL_SIZE+DISK_ACTUAL_SIZE))
unset DISK_ACTUAL_SIZE
done < "${VM_DISKS_FILE}"
unset VM_BACKUP_DIR VM_DISKS_FILE DISK_NAME DISK_FILE_ABSOLUTE_PATH
done
DISK_ACTUAL_FREE_SPACE=$(get_disk_actual_free_space)
REQUIRED_SPACE="${ALL_DISKS_ACTUAL_SIZE}"
if [ "${QEMU_IMG_CONVERT_WITH_COMPRESSION}" = "1" ]
then
REQUIRED_SPACE=$((ALL_DISKS_ACTUAL_SIZE * 2))
fi
if [ "${DISK_ACTUAL_FREE_SPACE}" -lt "${REQUIRED_SPACE}" ]
then
die "Insufficient disk space in ${BACKUP_DIR}: need ${REQUIRED_SPACE} bytes, have ${DISK_ACTUAL_FREE_SPACE}"
fi
log "Check available disk space finish"
unset ALL_DISKS_ACTUAL_SIZE DISK_ACTUAL_FREE_SPACE REQUIRED_SPACE
}
# ------------------------------------------------------------
# Back the VMs up
# ------------------------------------------------------------
backup_vms() {
log "Backup start"
for VM_NAME_TO_BACK_UP in ${VM_NAMES_TO_BACK_UP}
do
backup_vm "${VM_NAME_TO_BACK_UP}"
done
log "Backup finish"
}
# ------------------------------------------------------------
# Validate *.qcow2 files in "${CURRENT_BACKUP_DIR}"
# ------------------------------------------------------------
validate_backups() {
BACKUPS_TO_CHECK_FILE="$(mktemp)" || die "Failed to create a BACKUPS_TO_CHECK_FILE"
find "${CURRENT_BACKUP_DIR}" -type f \( -name '*.qcow2' -o -name '*.qcow2-shrunk' \) > "${BACKUPS_TO_CHECK_FILE}" || die "Failed to list backups in ${CURRENT_BACKUP_DIR}"
while IFS= read -r line
do
log "Analyzing: ${line}"
qemu-img info "${line}" || die "qemu-img info failed for ${line}"
qemu-img check "${line}" || die "qemu-img check failed for ${line}"
done < "${BACKUPS_TO_CHECK_FILE}"
rm -f "${BACKUPS_TO_CHECK_FILE}"
unset BACKUPS_TO_CHECK_FILE
}
# ------------------------------------------------------------
# Kill the the backup jobs
# ------------------------------------------------------------
kill_backup_jobs() {
log "Kill backup jobs start"
for VM_NAME_TO_BACK_UP in ${VM_NAMES_TO_BACK_UP}
do
if virsh domstate "${VM_NAME_TO_BACK_UP}" | grep -q "running"
then
virsh domjobabort "${VM_NAME_TO_BACK_UP}" || true
log "${VM_NAME_TO_BACK_UP} backup job killed"
else
log "${VM_NAME_TO_BACK_UP} is not running, skipping"
fi
done
log "Kill backup jobs finish"
}
# ------------------------------------------------------------
# Trap handler — aborts backup jobs and removes lock on SIGINT/SIGTERM
# ------------------------------------------------------------
cleanup_on_exit() {
kill_backup_jobs
exit 1
}
# ------------------------------------------------------------
# Removes obsolete backups
# ------------------------------------------------------------
clean_obsolete_backups() {
log "Clean obsolete backups start"
if [ -d "${ANOTHER_SERVER_ANOTHER_BACKUP_DIR}" ]
then
find "${ANOTHER_SERVER_ANOTHER_BACKUP_DIR}/" -type d -depth -mindepth 1 -mtime "${DAYS_TO_KEEP_BACKUPS}" -exec rm -rf {} \;
fi
if [ -d "${BACKUP_DIR}" ]
then
find "${BACKUP_DIR}/" -type d -depth -mindepth 1 -mtime "${DAYS_TO_KEEP_BACKUPS}" -exec rm -rf {} \;
fi
log "Clean obsolete backups finish"
}
# ------------------------------------------------------------
# Pushes backups to remote server
# ------------------------------------------------------------
push_backups_to_another_server() {
log "Push ${BACKUP_DIR} to ${ANOTHER_SERVER_IP}:${ANOTHER_SERVER_ANOTHER_BACKUP_DIR} start"
BACKUP_VM_DIRS="$(find "${BACKUP_DIR}" -mindepth 2 -maxdepth 2 -type d 2>/dev/null | wc -l)"
if [ "${BACKUP_VM_DIRS}" -eq 0 ]
then
log "Refusing to rsync --delete: no per-VM backup directories found in ${BACKUP_DIR}"
else
rsync --times --partial --recursive --delete --rsh="ssh -o BatchMode=yes" "${BACKUP_DIR}/" "${ANOTHER_SERVER_IP}:${ANOTHER_SERVER_ANOTHER_BACKUP_DIR}/" || die "Push failed"
fi
log "Push ${BACKUP_DIR} to ${ANOTHER_SERVER_IP}:${ANOTHER_SERVER_ANOTHER_BACKUP_DIR} finish"
}