Files
eidf-infra-backup/lxc-backup.sh
T

87 lines
1.7 KiB
Bash
Executable File

#!/usr/bin/env bash
main() {
while getopts ":l:d:t:" opt; do
case "${opt}" in
l)
unset VM_FILTER
IFS="," read -r -a VM_FILTER <<< "$OPTARG"
;;
d)
local backup_location="${OPTARG}"
backup_location="${backup_location%/}"
;;
t)
declare -g TARGET="$OPTARG"
;;
h)
usage
;;
*)
usage
;;
esac
done
backup_location=${backup_location:-$PWD}
if [[ "${#VM_FILTER[*]}" -gt 0 ]]; then
backup_vms "$backup_location" "${VM_FILTER[@]}"
else
backup_vms "$backup_location"
fi
}
lxc() {
local lxc_cmd="${TARGET:-lxc}"
command "$lxc_cmd" "$@"
}
list_vms() {
lxc list -c n -f csv,noheader
}
usage() {
cat <<EOF
Usage: ${0##*/} [-l vm[,vm...]] [-d backup_dir] [-t lxc|incus] [-h]
Options:
-l Comma-separated list of VMs to back up
-d Directory where backups will be stored (default: current directory)
-t Manager type: lxc or incus (default: lxc)
-h Show this message
EOF
exit 1
}
backup_vms() {
local backup_location="$1"; shift
local vms=("$@")
# when no argument use list of all VMs
if ((${#vms[@]} == 0)); then
mapfile -t vms < <(list_vms)
fi
for vm in "${vms[@]}"; do
[[ -n $vm ]] || continue
start_backup "$vm" "$backup_location"
done
}
start_backup() {
local vm=$1
local backup_base="$2"
local destination="${backup_base}/${vm}"
local backup_name="${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz"
[[ -d "$backup_base" ]] || { echo "${backup_base} is not a directory"; exit 1; }
[[ -d "$destination" ]] || mkdir "$destination"
[[ -f "${destination}/${backup_name}" ]] && { echo "Backup already exists"; return 2; }
lxc export "$vm" "${destination}/${backup_name}"
}
main "$@"