#!/usr/bin/env bash # e - script stops on error (return != 0) # u - error if undefined variable # o pipefail - script fails if one of piped commands fails # x - output each line (debug) set -euo pipefail function usage() { echo "Usage: $0 [options] [--] Options: -h|help Display this message -i|install Install VM Dumps -d|delete Delete VM Dumps" } # default values INSTALL=0 DELETE=0 # handle commandline arguments while getopts ":hid" opt do # shellcheck disable=SC2214 case $opt in h|help) usage; exit 0;; i|install) INSTALL=1;; d|delete) DELETE=1;; *) echo -e "\n Option does not exist: $OPTARG\n"; usage; exit 1;; esac done shift $((OPTIND-1)) if [ "$BASE_IP" = "" ]; then BASE_IP=192.168.0.20 fi IP_LAST_OCTET="${BASE_IP##*.}" IP_MAIN_OCTET="${BASE_IP%.*}" IP_LAST_OCTET=$((IP_LAST_OCTET-1)) BASE_ID=100 cp meta/tagged_for_upload /tmp/upload_cache while IFS= read -r LINE; do UPLOAD_PATH="/var/lib/vz/dump/vzdump-qemu-$(basename "$LINE" .vma.zst)-$(date +"%Y_%m_%d-%H_%M_%S").vma.zst" echo "Uploading VM dump '$LINE'..." CURRENT_NODE="$(dirname "$LINE")" CURRENT_NODE="${CURRENT_NODE##*-}" IP="$IP_MAIN_OCTET.$((IP_LAST_OCTET+CURRENT_NODE))" rsync --info=progress2 "$LINE" root@"$IP":"$UPLOAD_PATH" if [[ $INSTALL -eq 1 ]]; then echo "Installing VM dump '$LINE'..." ssh -n root@"$IP" "qmrestore $UPLOAD_PATH $BASE_ID --force --unique" BASE_ID=$((BASE_ID+1)) fi if [[ $DELETE -eq 1 ]]; then echo "Deleting VM dump '$LINE'..." ssh -n root@"$IP" "rm -rf $UPLOAD_PATH" fi ESCAPED_LINE=$(printf '%s\n' "$LINE" | sed -e 's/[\/&]/\\&/g') sed -i "/$ESCAPED_LINE/d" meta/tagged_for_upload done < /tmp/upload_cache echo "Done."