diff --git a/lxc-backup.sh b/lxc-backup.sh index 93d502a..616a3bb 100755 --- a/lxc-backup.sh +++ b/lxc-backup.sh @@ -1,27 +1,64 @@ #!/usr/bin/env bash -lxc() { - _LXC="${TARGET:-lxc}" - command $_LXC "$@" -} - -lxc_list() { - lxc list -c n -f csv,noheader "$@" -} - -usage() { echo "Usage: ${0##*/} [-l vm1,vm2,...]"; exit 1; } - main() { while getopts ":l:" opt; do case "${opt}" in l) - IFS="," read -r -a LIMIT <<< "$OPTARG" + unset VM_FILTER + IFS="," read -r -a VM_FILTER <<< "$OPTARG" ;; *) usage ;; esac done + + local backup_location="/spool/backups" + + 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() { echo "Usage: ${0##*/} [-l vm1,vm2,...]"; 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}" + echo "$vm -> $destination" + + # Check destination base exists + # Check VM folder exists + # Check if archive already exists + echo "lxc export "$vm" "$destinationi/${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz"" + lxc export "$vm" "$destinationi/${vm}_$(date +"%Y-%m-%d_%H-%M-%S").tar.gz" } main "$@"