Skip to content

Instantly share code, notes, and snippets.

@halcyon
Forked from jim80net/vmigrate.sh
Created September 25, 2013 17:21
Show Gist options
  • Save halcyon/6702957 to your computer and use it in GitHub Desktop.
Save halcyon/6702957 to your computer and use it in GitHub Desktop.
#!/bin/bash
# Use this to send and receive files
# Dependencies:
# SSH-Keys setup for root users. I recommend using agent forwarding for this.
# mbuffer installed in /opt/local/bin/mbuffer. Adjust to reality.
#
# This script presumes:
# zsnapper is installed. Comment out the two lines where it is referenced if you don't use it
DATE=$(date +%Y%m%d%H%M)
case $# in
[2])
ACTION=SEND
UUID=$1
TARGET=$2
echo "OK, sending $1 to $2"
;;
[3])
ACTION=INCREMENT
UUID=$1
TARGET=$2
INCREMENTAL=$3
echo "OK, sending $UUID incrementally from snapshot /zones/$UUID@$INCREMENTAL"
;;
*)
echo "Please call by using $0 UUID TARGET <incremental-startpoint>"
exit 1
;;
esac
function die {
echo "ERROR: $1"
exit 99
}
##
# Dependency check
function check_prog {
if which $1 2>&1 > /dev/null
then
donothing=ok
else
die "$1 does not appear to be installed. Please install."
fi
}
check_prog mbuffer
[[ "THISWORKS" == $(ssh $TARGET echo "THISWORKS" 2>/dev/null) ]] || die "Cannot SSH to $TARGET"
function take_snap {
SNAPNAME="$1@migration-$DATE"
zfs snapshot $SNAPNAME && echo "SNAP CREATED: $SNAPNAME"
}
#$1 zfs@snapname
#$2 TARGET
#$3 zfs
function send_snap {
zfs send -v $1 | /opt/local/bin/mbuffer -q -s 128k -m 4G | ssh -c arcfour $2 "/opt/local/bin/mbuffer -q -s 128k -m 4G | zfs receive -F $3"
}
#$1 zfs@snapname
#$2 TARGET
#$3 zfs
#$4 INCREMENTAL
function send_increment {
zfs send -vi $3@$4 $1 | /opt/local/bin/mbuffer -q -s 128k -m 4G | ssh -c arcfour $2 "/opt/local/bin/mbuffer -q -s 128k -m 4G | zfs receive -F $3"
}
#$1 UUID
#$2 TARGET
function create_zone_entry {
echo "Sending manifest..."
scp /etc/zones/$1.xml $2:/etc/zones/ && echo "...done" || die "Unable to send manifest /etc/zones/$1.xml to $2"
echo "Installing index."
echo "$1:installed:/zones/$1:$1" | ssh $2 "/usr/bin/cat >> /etc/zones/index"
ssh -t $2 vmadm list -v | grep $1 && echo "Remote host checks out." || die "Failed to find remote vm installed."
}
##
# MAIN
for disk in $( zfs list | grep $UUID | grep -v cores | awk '{print $1}' )
do
take_snap $disk
DISKS="$DISKS $disk"
done
ssh -t $TARGET svcadm disable zsnapper:default
case $ACTION in
"SEND")
for sdisk in $DISKS
do
echo "Sending $sdisk"
send_snap $sdisk@migration-$DATE $TARGET $sdisk
done
echo "Done sending disks."
create_zone_entry $UUID $TARGET
;;
"INCREMENT")
for sdisk in $DISKS
do
echo "Incrementing $sdisk"
send_increment $sdisk@migration-$DATE $TARGET $sdisk $INCREMENTAL
done
echo "Done incrementing disks."
;;
esac
ssh -t $TARGET svcadm enable zsnapper:default
echo "##############################################################################################"
echo "##"
echo "# When ready, halt the zone on the sender side before starting it on the receive side with:"
echo "vmadm halt $UUID"
echo ""
echo "##"
echo "# If you want to increment the disks, then run this:"
echo "$0 $UUID $TARGET migration-$DATE"
echo ""
echo "##"
echo "# Start the zone on the target with the commmand:"
echo "ssh -t $TARGET vmadm boot $UUID"
echo ""
echo "##"
echo "# Make sure you cleanup the source VM:"
echo "vmadm destroy $UUID"
echo ""
echo "##############################################################################################"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment