add flag to select compression algorithm

This commit is contained in:
adam committed 2026-01-14 00:35:59 +00:00
1 parent ae4f8e685d
commit 5e07f58248
1 file changed
+19 -5
+19 -5
View File
@@ -1,7 +1,7 @@
#!/usr/bin/env bash #!/usr/bin/env bash
main() { main() {
while getopts ":l:d:t:c:h" opt; do while getopts ":l:d:t:x:c:h" opt; do
case "${opt}" in case "${opt}" in
l) l)
unset VM_FILTER unset VM_FILTER
@@ -14,6 +14,17 @@ main() {
t) t)
declare -g TARGET="$OPTARG" declare -g TARGET="$OPTARG"
;; ;;
x)
declare -g EXTENSION
if [[ "${OPTARG,,}" == 'none' ]]; then
EXTENTION=""
else
command -v "$OPTARG" >/dev/null 2>&1 || { echo "$OPTARG command not found"; exit 1; }
EXTENTION="${OPTARG,,}"
fi
declare -g COMPRESSION="$OPTARG"
;;
c) c)
declare -g run_cleanup=y declare -g run_cleanup=y
declare -g files_to_keep="${OPTARG}" declare -g files_to_keep="${OPTARG}"
@@ -47,12 +58,13 @@ list_vms() {
usage() { usage() {
cat <<EOF cat <<EOF
Usage: ${0##*/} [-l vm[,vm...]] [-d backup_dir] [-t lxc|incus] [-c <keep n backups>] [-h] Usage: ${0##*/} [-l vm[,vm...]] [-d backup_dir] [-t lxc|incus] [-x <compression alogorithm>] [-c <keep n backups>] [-h]
Options: Options:
-l Comma-separated list of VMs to back up -l Comma-separated list of VMs to back up
-d Directory where backups will be stored (default: current directory) -d Directory where backups will be stored (default: current directory)
-t Manager type: lxc or incus (default: lxc) -t Manager type: lxc or incus (default: lxc)
-x Compression algorithm (e.g. gzip, zstd, none)
-c Cleanup old backups keeping a set number (default: 2) -c Cleanup old backups keeping a set number (default: 2)
-h Show this message -h Show this message
@@ -78,21 +90,23 @@ backup_vms() {
start_backup() { start_backup() {
local vm=$1 local vm=$1
local backup_base="$2" local backup_base="$2"
local compression="${COMPRESSION:-none}"
local ext="${EXTENTION}" ; [[ -n "$ext" ]] && ext=".${ext}"
local destination="${backup_base}/${vm}" local destination="${backup_base}/${vm}"
local backup_name="${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar" local backup_name="${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar${ext}"
[[ -d "$backup_base" ]] || { echo "${backup_base} is not a directory"; exit 1; } [[ -d "$backup_base" ]] || { echo "${backup_base} is not a directory"; exit 1; }
[[ -d "$destination" ]] || mkdir "$destination" [[ -d "$destination" ]] || mkdir "$destination"
[[ -f "${destination}/${backup_name}" ]] && { echo "Backup already exists"; return 2; } [[ -f "${destination}/${backup_name}" ]] && { echo "Backup already exists"; return 2; }
lxc export "$vm" --instance-only --compression none "${destination}/${backup_name}" || { echo "Error exporting $vm"; return 1; } lxc export "$vm" --instance-only --compression "${compression}" "${destination}/${backup_name}" || { echo "Error exporting $vm"; return 1; }
[[ -n "$run_cleanup" ]] && prune_backups "$destination" [[ -n "$run_cleanup" ]] && prune_backups "$destination"
} }
prune_backups() { prune_backups() {
local backup_directory="$1" local backup_directory="$1"
mapfile -t backup_files < <(find "$backup_directory" -maxdepth 1 -name "*.tar" -printf '%T@ %p\n' | sort -n | head -n "-${files_to_keep}" | cut -d' ' -f2-) mapfile -t backup_files < <(find "$backup_directory" -maxdepth 1 -type f -printf '%T@ %p\n' | sort -n | head -n "-${files_to_keep}" | cut -d' ' -f2-)
if [[ ${#backup_files[@]} -gt 0 ]]; then if [[ ${#backup_files[@]} -gt 0 ]]; then
echo "Removing ${#qcow2_files[@]} old backup files for $vm (keeping last $total_files_to_keep files)" echo "Removing ${#qcow2_files[@]} old backup files for $vm (keeping last $total_files_to_keep files)"