#!/bin/bash

# Written by Joseph Hofmann <jh@combahton.net>
# Purpose: Automtically and fully migrates kvm servers from node a to b

for i in $(qm list |awk '{print $1}' |sed -e 1d); do
	echo -e "Shutting down $i...\n";
	qm shutdown $i;
	echo -e "Stopping hard $i...\n";
	sleep 30
	qm stop $i;
	sleep 30
	echo -e "Dumping $i...\n";
	vzdump --dumpdir=/root/$newnode_remote/ $i
	echo -e "Restoring $i on remote destination...\n";
	ssh -i $newnode root@$newnode "qmrestore /var/lib/vz/migrate/*$i*.vma $i"
	echo -e "Starting $i on remote destination...\n";
	ssh -i $newnode root@$newnode "qm start $i"
	ssh -i $newnode root@$newnode "rm /var/lib/vz/migrate/*$i*.vma"
done
