#!/usr/bin/bash # Push VM-Disk-Image on server set -eu show_help() { cat << EOF >&2 Usage: $(basename "$0") [-d] [-a] [-t] [vmnames]" Images from vmnames-List will be synced from server. Default by torrent. Using flag -d VMs will be synced by rsync Using flag -a images from images.list and xml-directory will be synced from server. Using flag -t all torrents and xml-VM-Definitions will be synced EOF } 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/ rsync -av --password-file=/etc/rsync.secret \ "rsync://vmuser@server:/vmimages-download/${VM_NAME}.qcow2.torrent" \ /var/lib/libvirt/images/ /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" } torrent_image() { if [[ -f "/var/lib/libvirt/images/${VM_NAME}.qcow2.torrent" ]]; then cd /var/lib/libvirt/images ctorrent -e 0 "${VM_NAME}.qcow2.torrent" /usr/local/bin/vmimage-torrent restart "${VM_NAME}.qcow2" else echo "No torrent-File found" exit 1 fi } 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/ } sync_all_torrents() { rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/*.torrent \ /var/lib/libvirt/images/ rsync -av --password-file=/etc/rsync.secret rsync://vmuser@server:/vmimages-download/xml \ /var/lib/libvirt/images/ } while getopts ':dat' OPTION; do case "$OPTION" in d) DOWNLOAD=1 ;; a) sync_all_images exit 1 ;; t) sync_all_torrents ;; ?) show_help exit 1 ;; esac done shift "$((OPTIND -1))" # if less than one arguments supplied, display usage if [[ $# -lt 1 ]]; then show_help exit 1 fi for VM_NAME in "$@"; do if [[ -v "DOWNLOAD" ]]; then echo "Downloading $VM_NAME" download_image else echo "Torrenting $VM_NAME" torrent_image fi done