#!/bin/bash helpFunction() { echo "" echo "Usage: $0 -v VmName -t TargetHost -d TargetDataset" echo -e "\t-v Name of the vm to backup" echo -e "\t-t Backup target host name" echo -e "\t-s VM source zfs dataset" echo -e "\t-d Backup target zfs dataset" exit 1 # Exit script after printing help } while getopts "v:t:s:d:" opt do case "$opt" in v ) VM="$OPTARG" ;; t ) DESTHOST="$OPTARG" ;; s ) SRCZFS="$OPTARG" ;; d ) DESTZFS="$OPTARG" ;; ? ) helpFunction ;; # Print helpFunction in case parameter is non-existent esac done # Print helpFunction in case parameters are empty if [ -z "$VM" ] || [ -z "$DESTHOST" ] || [ -z "$DESTZFS" ] || [ -z "$SRCZFS" ] then echo "Some or all of the parameters are empty"; helpFunction fi DATE=$(date "+%Y%m%d") DATE30=$(date -d "-30day" "+%Y%m%d") SNAPNAME="backup_$DATE" VMDISKS=$(virsh domblklist $VM | egrep -o vd.) DISKSPEC="" for disk in $VMDISKS do DISKSPEC+="--discspec $disk,file=/srv/snapshots/$VM/$VM-$disk-$SNAPNAME.qcow2,snapshot=external " done echo $DISKSPEC