本文共 3235 字,大约阅读时间需要 10 分钟。
#!/bin/sh
#1、此脚本需要新服务器的的root可以直接登录,root的密码是没有特殊字符的密码。允许老的服务器用scp命令向新服务器传送文件。
#由于root密码被修改成简单密码,所以不要停止iptables,可以在iptables里面加入允许老服务器的ip的22端口可以访问。
#2、为了方便查看那些云主机已经迁移好了,需要连接到新服务器上,运行tty,查看到tty值,把tty的结果替换脚本中的”/dev/pts/ttynum“字符。
#同时不要关闭已经打开的新的服务器的连接。
#3、脚本执行完以后,后台确认一下是否全部完成。然后删除/tmp/a.sh zcc.host zcc.hostbak
#4、删除新服务器上的/tmp/newserver.sh okhost文件。同时把root密码修改成老的root密码。
#4、sh -x a.sh 新服务器的ip 新服务器的密码
newip=$1;
newpasswd=$2;
if [ ! -f /tmp/newserver.sh ];then
cat >> /tmp/newserver.sh << "EOF"
#!/bin/sh
sed -i 's#rhel6.2.0#pc#' /vm/vm/$1/$1.xml;
[ $? != 0 ] && exit 1;
sed -i 's#/usr/libexec/qemu-kvm#/usr/bin/kvm#' /vm/vm/$1/$1.xml;
[ $? != 0 ] && exit 2;
if [ -f /vm/vm/$1/snapshot/*.xml ];then
sed -i 's#rhel6.2.0#pc#' /vm/vm/$1/snapshot/*.xml;
[ $? != 0 ] && exit 3;
sed -i 's#/usr/libexec/qemu-kvm#/usr/bin/kvm#' /vm/vm/$1/snapshot/*.xml;
[ $? != 0 ] && exit 4;
mkdir -p /var/lib/libvirt/qemu/snapshot/$1 && cp /vm/vm/$1/snapshot/*.xml /var/lib/libvirt/qemu/snapshot/$1;
[ $? != 0 ] && exit 5;
fi
virsh define /vm/vm/$1/$1.xml;
virsh start $1;
hostip=$(less /vm/vm/$1/$1.xml| grep value |grep -o -P '(\d{1,3}\.){3}\d{1,3}')
if [ -n $hostip ];then
for pingnum in $(seq 1 10);
do
ping -c 2 $hostip
if [ $? = 0 ] ; then
echo `date +%F` `date +%T` $1 done >> /tmp/okhost
echo =================================== > /dev/pts/ttynum
echo ===========$1======done======= > /dev/pts/ttynum
echo =================================== > /dev/pts/ttynum
break
fi
if (( $pingnum == 6 )) ; then
echo `date +%F` `date +%T` $1 done but dead ping >> /tmp/okhost
echo =================================== > /dev/pts/ttynum
echo ===========$1======done======= > /dev/pts/ttynum
echo =================================== > /dev/pts/ttynum
fi
done
fi
EOF
fi
chmod 700 /tmp/newserver.sh
#scp newserver.sh to newserver
expect -c "
spawn scp -r /tmp/newserver.sh root@$newip:/tmp/
expect {
\"*assword\" {set timeout 9000; send \"$newpasswd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
[ $? != 0 ] && echo "create newserver or scp wang!" && exit;
echo "create and scp newserver ok"
if [ ! -f /tmp/zcc.host ];then
virsh list --all | sed '1,2d' | sed '$d' | awk '{print $2}'| sort > /tmp/zcc.host
cp /tmp/zcc.host /tmp/zcc.hostbak
fi
while read host
do
virsh destroy $host 2>/dev/null
sleep 3
virsh snapshot-list $host > /tmp/$$.snapshot
snapshotname=$(grep -o -P '\d{14}' /tmp/$$.snapshot)
rm /tmp/$$.snapshot
if [ -n "$snapshotname" ];then
virsh snapshot-delete $host $snapshotname;
fi
virsh undefine $host
#scp -r /vm/vm/$host root@10.0.10.11:/vm/vm/
expect -c "
spawn scp -r /vm/vm/$host/ root@$newip:/vm/vm/
expect {
\"*assword\" {set timeout 9000; send \"$newpasswd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
if [ $? = 0 ];then
echo =============================
echo ====$host=====scp======OK====
echo =============================
mv /vm/vm/$host /vm/vm/$host.$(date +%Y-%m-%d.%H:%M:%S).drop
sed -i "/^$host$/d" /tmp/zcc.host
fi
expect -c "
spawn ssh root@$newip \"nohup /tmp/newserver.sh $host\" &
expect {
\"*assword\" {set timeout 9000; send \"$newpasswd\r\";}
\"yes/no\" {send \"yes\r\"; exp_continue;}
}
expect eof"
done < /tmp/zcc.host
本文转自 freeterman 51CTO博客,原文链接:http://blog.51cto.com/myunix/1202228,如需转载请自行联系原作者