xen主机备份还原
A script for backing up file-backed Xen DomU is introduced in this post. This script can be changed to similar platform.
本文介绍了用于备份文件支持的Xen DomU的脚本。 该脚本可以更改为类似平台。
In our cluster, virtual machines are stored under /lhome/xen/. Virtual machine with id vmid is stored in directory vmvmid. The raw image disk file name can also be derived from vmid. Some more details and the configuration file can be found from Unified Xen DomU configuration file. The set up process of Xen platform can be found Setting up Stable Xen Dom0 with Fedora: Xen 3.4.3 with Xenified Linux Kernel 2.6.32.13 in Fedora 12 in Xen Solutions. Some introduction to Xen management command xm can be found from Create and Manage Virtual Machines on Xen.
在我们的集群中,虚拟机存储在/ lhome / xen /下。 标识为vmid的虚拟机存储在目录vm vmid中。 原始映像磁盘文件名也可以从vmid派生。 可以从Unified Xen DomU配置文件中找到更多详细信息和配置文件 。 Xen平台的设置过程可以在Fedora中使用Fedora设置稳定的Xen Dom0:在Xen Solutions 中的Fedora 12中使用Xenified Linux Kernel 2.6.32.13来安装Xen 3.4.3 。 有关Xen管理命令xm的一些介绍,可以从Xen上的“创建和管理虚拟机”中找到。
This script is called by cron from every one week. We want to backup the virtual machines for every other week. mark_file is used to make the backup command run every other time the script is called. More details please refer to How to Run a cron Job Every Two Weeks / Months / Days. If this script is used manually, please delete the “# check whether bak runned last week” part.
cron从每周开始调用此脚本。 我们希望每两周备份一次虚拟机。 mark_file用于使备份命令每隔一次调用脚本运行一次。 有关更多详细信息,请参阅如何每两周/月/天执行一次cron作业 。 如果手动使用此脚本,请删除“#检查上周是否运行bak”部分。
Two copies of backup is made in the script: one in local directory in another hard disk, another one in hard disk of another machine. The raw image file are compress for save disk space and network usage. In our virtual machines, the raw image file size is 20G, while the compressed one is only about 3G to 4G depends on the usage of the virtual machine’s hard disk space. ssh tunnel is used for data transmission.
脚本中制作了两份备份:一份在另一硬盘的本地目录中,另一份在另一台计算机的硬盘中。 将原始映像文件压缩以节省磁盘空间和网络使用量。 在我们的虚拟机中,原始映像文件的大小为20G,而压缩后的原始文件大小仅为3G至4G,具体取决于虚拟机硬盘空间的使用情况。 ssh隧道用于数据传输。
The backup consist of several steps:
备份包括几个步骤:
1. For every virtual machine:
1.1 Check whether VM is running
1.2 Shutdown the virtual machine
1.3 Copy the image file to local directory in another hard disk
1.4 Start the virtual machine
2. Compress the copied image file in local directory
3. Copy these compressed image file to remote directory
As we use scp to transfer backup files to remote server, we need to Enabling Password-less ssh Login first.
当我们使用scp将备份文件传输到远程服务器时,我们需要首先启用无密码ssh登录 。
This is the script:
这是脚本:
#!/bin/bash
# Author: Zhiqiang Ma (http://www.ericzma.com/)
# Dec. 27, 2010
mark_file=/tmp/xen-bak-marker
log_file=/lhome/xen/bak-111.log
err_log_file=/lhome/xen/bak_err-111.log
xen_dir=/lhome/xen
local_bak_dir=/lhome/xen-bak-tmp
bak_dir=xenbak@backup_host1:/lhome/xenbak
xen_name_list="10.10.10.111"
# check whether bak runned last week
if [ -e $mark_file ] ; then
rm -f $mark_file
else
touch $mark_file
# exit 0
fi
# set std and stderr to log file
mv $log_file $log_file.old
mv $err_log_file $err_log_file.old
exec 2> $err_log_file
exec > $log_file
# check whether the VM is running
# We only backup running VMs
echo "*** Check alive VMs"
xen_name_list_tmp=""
for i in $xen_name_list
do
/usr/sbin/xm list > /tmp/tmp-xen-list
grepinlist=`grep $i" " /tmp/tmp-xen-list`
if [[ $grepinlist == "" ]]
then
echo $i is not alive.
else
echo $i is alive.
xen_name_list_tmp=$xen_name_list_tmp" "$i
fi
done
xen_name_list=$xen_name_list_tmp
echo "Alive VM list:"
for i in $xen_name_list
do
echo $i
done
echo "End alive VM list."
###############################
date
echo "*** Backup starts"
###############################
date
echo "*** Copy VMs to local disk"
for i in $xen_name_list
do
date
echo "Copy $i"
echo "Shutdown $i"
/usr/sbin/xm shutdown $i
sleep 30
echo "Copy to local_bak_dir: $local_bak_dir"
cp $xen_dir/vm-$i/vmdisk0 $local_bak_dir/vmdisk0-$i
cp $xen_dir/vm-$i/vm.run $local_bak_dir/vm.run-$i
date
echo "Create $i"
# with vmmem=1024"
# /usr/sbin/xm create $xen_dir/vm.run vmid=$i vmmem=1024
/usr/sbin/xm create $xen_dir/vm-$i/vm.run
scp $log_file $bak_dir/bak-111.log
cp $log_file $local_bak_dir/bak-111.log
done
####################
date
echo "*** Compress local bak vmdisks"
for i in $xen_name_list
do
date
echo "Compress $i"
tar -z -cf $local_bak_dir/vmdisk0-$i.tar.gz $local_bak_dir/vmdisk0-$i $local_bak_dir/vm.run-$i
rm -f $local_bak_dir/vmdisk0-$i $local_bak_dir/vm.run-$i
done
####################
date
echo "*** Copy local bak vmdisks to remote machines"
for i in $xen_name_list
do
date
echo "Copy to remote: vm$i"
scp $local_bak_dir/vmdisk0-$i.tar.gz $bak_dir/vmdisk0-$i.tar.gz
done
#####################
date
echo "Backup finishes"
scp $log_file $bak_dir/bak-111.log
翻译自: https://www.systutorials.com/automatically-backing-up-xen-file-backed-domu/
xen主机备份还原