lmn-client/roles/lmn_vm/files/sync-vm.sh
2023-02-12 14:38:29 +01:00

64 lines
1.9 KiB
Bash
Executable file

#!/usr/bin/bash
# Push VM-Disk-Image on server
set -eu
show_help() {
cat << EOF
Usage: $(basename "$0") [-u vmname] [-d vmname] [-a]"
When using option -u (upload), the disk from VM vmname will be synced on server.
Otherwise the images from images.list and xml-directory will be synced from server.
EOF
}
upload_image() {
# check if VM-Diskimage exists
if [[ ! -f "/var/lib/libvirt/images/${VM_NAME}.qcow2" ]]; then
echo "File not found ${VM_NAME}.qcow2"
exit 1
fi
# check if VM-Machine-Definition XML exists
if [[ ! -f "/var/lib/libvirt/images/xml/${VM_NAME}.xml" ]]; then
echo "File not found ${VM_NAME}.xml"
exit 1
fi
rsync -av --password-file=/etc/rsync.secret "/var/lib/libvirt/images/${VM_NAME}.qcow2" \
rsync://vmuser@server:/vmimages-upload/
rsync -av --password-file=/etc/rsync.secret "/var/lib/libvirt/images/xml/${VM_NAME}.xml" \
rsync://vmuser@server:/vmimages-upload/xml/
}
download_image() {
rsync -av --password-file=/etc/rsync.secret \
"rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2" \
/var/lib/libvirt/images/
rsync -av --password-file=/etc/rsync.secret \
"rsync://vmuser@server:/vmimages-download/xml/${VM_NAME}.xml" \
/var/lib/libvirt/images/xml/
}
sync_all_images() {
rsync -av --password-file=/etc/rsync.secret --files-from=/var/lib/libvirt/images/images.list \
rsync://vmuser@server:/vmimages-download/ /var/lib/libvirt/images/
rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/xml \
/var/lib/libvirt/images/
}
while getopts ':u:d:a' OPTION; do
case "$OPTION" in
u)
VM_NAME=$OPTARG
upload_image
;;
d)
VM_NAME=$OPTARG
download_image
;;
a)
sync_all_images
;;
?)
show_help
exit 1
;;
esac
done