This script can guide admin thru vzmigration process.
This vzmigration is made for minimum downtime, livemode and will keep old server VM running
Code:
echo "ONE VM MIGRATION AND FIXING SCRIPT
--------------------------------------
This script migrates one VM to another server (live mode) and can keep old server VM running. Result log is sent to email provided.
Command used to backup one VM:
vzmigrate -v --remove-area no --live --readonly --keep-dst --ssh="-p 22" destinationnodeip 110
---------------------------------------------------" && yum install mutt -y
echo "This script assumes VZ conf path is /etc/vz/conf/VMID.conf - if its other, you need to edit this script"
cpath=/etc/vz/conf
ls $cpath
echo "
IP of destination node server:"
read ip
echo "VMID to migrate:"
read VMID
vzlist -o ctid,ip
echo "
VMID old IP is: $(vzlist $VMID -o ip -H) is that right? If yes, hit Enter or paste it"
read OLDIP
if [ "$OLDIP" == "" ];then
OLDIP=$(vzlist $VMID -o ip -H)
fi
echo "VMID New IP: (assign it now in new server HyperVM, then paste here)"
read NEWIP
mail=[email protected]
vzmigrate -v --remove-area no --live --keep-dst --ssh="-p 22" $ip $VMID | tee migration.log
mv $cpath/$VMID.conf.migrated $cpath/$VMID.conf | tee -a migration.log
vzctl start $VMID
echo "Migration for $VMID VM should be complete.. check following log. and new HyperVM server vzlist. Then run script ./home/vzdumprestore which contains VM IP changing & fixing routines.
$(cat migration.log)" | mutt -s "VM Migration logs" -- $mail
rm -rf migration.log
This is second script which can be used at new server for VM restoration (vzrestore):
Code:
clearecho "This script can:
- restore vzdump file
- delete old VM ip and replace by new one set by user
- replace old ips in hosts, dns zone files
- add /var/log/httpd folder, restart named and httpd
- script will report old ip, new ip and vzlist before change to the email $mail"
ls | grep tgz
echo "Do just VM fixing (hit enter) or do vzdump file restore also? (y=yes) ... assuming vzdump-VMID.tgz file is in same directory as this script."
read dumpok
echo "VMID to work with:"
read VMID
if [ "$dumpok" == "y" ];then
vzdump --restore vzdump-$VMID.tgz $VMID;vzctl start $VMID;sleep 2
echo "VM $VMID vzdump restore finished, now respond to the script questions in SSH terminal to continue."
fi
mail=[email protected]
listofvms="vzlist at $(hostname):
$(vzlist -o ctid,ip,hostname)"
vzlist -o ctid,ip
echo "VMID old IP is: $(vzlist $VMID -o ip -H) is that right? If yes, hit Enter or paste it"
read OLDIP
if [ "$OLDIP" == "" ];then
OLDIP=$(vzlist $VMID -o ip -H)
fi
echo "VMID New IP: (add this one in new HyperVM now)"
read NEWIP
echo "What is the path to VMs folders? Is it default /vz/root ? then type: /vz/root"
read vmsroot
vzctl set $VMID --ipdel $OLDIP --save
vzctl set $VMID --ipadd $NEWIP --save
find $vmsroot/$VMID/etc -type f -print0 | xargs -0 sed -i "s|$OLDIP|$NEWIP|g";
find $vmsroot/$VMID/var/named -type f -print0 | xargs -0 sed -i "s|$OLDIP|$NEWIP|g";
find $vmsroot/$VMID/var/named/chroot/var/named -type f -print0 | xargs -0 sed -i "s|$OLDIP|$NEWIP|g";
echo "cat $vmsroot/$VMID/etc/hosts"
cat $vmsroot/$VMID/etc/hosts
echo "cat $vmsroot/$VMID/var/named/chroot/var/named/*"
cat $vmsroot/$VMID/var/named/chroot/var/named/*
vzctl exec $VMID mkdir /var/log/httpd
vzctl exec $VMID /etc/init.d/named restart
vzctl exec $VMID /etc/init.d/httpd restart
echo "VM $VMID restore, ip change and fixing script finished. http://$NEWIP
Ask VM owner to change nameserver IPs in domain registar if host domain.
If anything dont works, errors can be old IP in /etc/hosts, /var/named/chroot/var/named or restart httpd, try /script/fixdns/fixweb
$listofvms" | mutt -s "VM restore script finished" -- $mail
echo "
This VM restore and IP change script finished. Ask VM owner to change nameserver IPs in domain registar if host domain.
First run these commands if above was outputted old ips in files:
find $vmsroot/$VMID/etc -type f -print0 | xargs -0 sed -i 's|$OLDIP|$NEWIP|g';find $vmsroot/$VMID/var/named -type f -print0 | xargs -0 sed -i 's|$OLDIP|$NEWIP|g';find $vmsroot/$VMID/var/named/chroot/var/named -type f -print0 | xargs -0 sed -i 's|$OLDIP|$NEWIP|g';vzctl exec $VMID /etc/init.d/named restart;vzctl exec $VMID /etc/init.d/httpd restart;cat $vmsroot/$VMID/etc/hosts;
Change IPs in WHMCS and send client email with new IP"
No guarantee is provided for runing thes scripts, but i tested them and it worked for me.
Here is third script which can automated process of migrating ALL OpenVZ VMs:
Code:
echo "This script migrates all VMs to another server (live mode) and can keep old server VMs running. Result log for each VM is sent to email provided.
Command used to backup one VM: vzmigrate -v --remove-area no --live --readonly --keep-dst --ssh="-p 22" destinationnodeip 110
---------------------------------------------------"
echo "IP of destination server:"
read ip
ls /etc/vz/conf
echo "Path to vm config files: (def. is /etc/vz/conf , hit enter to confirm)"
read cpath
if [ "$cpath" == "" ];then
cpath=/etc/vz/conf
fi
echo "Email where report will be sent: (def. [email protected] , hit enter to confirm)"
read email
if [ "$email" == "" ];then
email=[email protected]
echo "That is ALL, migration can take many hours. You will receive an email after each VM migration finish. Hit any key to start."
read start
for CT in $(vzlist -H -o veid); do
vzmigrate -v --remove-area no --live --readonly --keep-dst --ssh="-p 22" $ip $CT | tee migration.log
mv $cpath/$CT.conf.migrated $cpath/$CT.conf | tee -a migration.log
echo "Migration for $CT VM should be complete.. check following log. and new HyperVM server vzlist. Then run script ./home/vzdumprestore which contains VM IP changing & fixing routines.
$(cat migration.log)" | mutt -s "VM Migration logs" -- $email
done
rm -rf migration.log
Bookmarks