XenServer 虚拟机备份脚本

简介:

 最近忙着弄虚拟机,什么数据都需要备份,即使虚拟机也不例外,下面是我自己写的备份脚本根据实际情况进行使用,:)

 

首先做一个NFS服务器,此服务器需要大容量磁盘,然后再需要备份的虚拟服务器主机上挂在共享目录

然后就可以实施脚本做成计划任务做备份

 ###################################################################################################

####                       创建所有虚拟机的快照并导出为模板删除快照                            ####

###################################################################################################

#全部  测试OK

#!/bin/bash

 

#Version: v.2

#Date: 2012.07.20

#Auther:andylhz

#Propose:Create All VM's snapshot on XenServer and export snapshot to template and delete the snapshot.

#Script name: A_vm_export_shapshot_to_template.sh, Example:./A_vm_export_shapshot_to_template.sh

 

 

 

 

 

##Get System Date

DT=$(date +%Y%m%d%H%M%S)

 

##Seting backup dir

backup_dir=/root/vmbk

 

##Get VM uuid list

 

echo "VM snapshot is createing...."

 

   for vmname in `xe vm-list params=name-label |awk '{print $5}' |grep -v ^C |sed '/^$/d'` ;

     

     do echo `xe vm-snapshot vm=$vmname    new-name-label=$vmname`  >> $backup_dir/snapshot_uuid ;            

 

     if [ $? -eq 0 ]; then

 

      echo  "VM snapshot $vmname is created....OK!"

    else

      echo  "VM snapshot $vmname is created....FAILSE!"

      exit 1

    fi

 

    done

 

##Export snapshots as a template

 

#Get snapshot uuid

xe snapshot-list params=uuid,name-label|sed '/^$/d' |awk '{print $4,$5}' |sed 's/: //g'|sed -e 'N; s/\n/ /g' >$backup_dir/vmname_sanpuid

 

echo "Templates are exporting please waite....."

 

   for snap_uuid in `cat $backup_dir/vmname_snapuid|awk '{print $1}'`;

     

     do xe snapshot-export-to-template  snapshot-uuid=$snap_uuid   filename=$backup_dir/$snap_uuid$DT.xva ;

      

   if [ $? -eq 0 ]; then

 

      echo  "VM template $snap_uuid$DT is created....OK!"

   else

      echo  "VM template $snap_uuid$DT is created....FAILSE!"

      

      exit 1

   fi

         

   done

   

 

##Compressed export file.

 

cd  $backup_dir

 

echo "VM templates are being compressed...."

 

gzip  *.xva

 

 if [ $? -eq 0 ]; then

 

      echo  "VM template is compressed....OK!"

 else

      echo  "VM template is compressed....FAILSE!"

      

      exit 1

 fi

 

 

##Delete snapshot

 

echo "Deleteing snapshot....."

 

      for snap_uuid in `cat $backup_dir/snapshot_uuid`;

     

         do xe snapshot-uninstall    snapshot-uuid=$snap_uuid force=true;

         

 if [ $? -eq 0 ]; then

 

      echo   "Snapshot $snap_uuid deleted.....OK!"

 else

      echo   "Snapshot $snap_uuid deleted.....FAILSE!"

      

      exit 1

 fi

                 

      done

 

mv snapshot_uuid   snapshot_uuid$DT

mv vmname_sanpuid vmname_sanpuid$DT

 

 

 

###################################################################################################

####                           对某个虚拟机做快照并导出为模板                                  ####

###################################################################################################

#静默方式 测试OK!

 

#!/bin/bash

 

#Version: v.1

#Date: 2012.07.20

#Auther:andylhz

#Propose:Create one VM's snapshot on XenServer and export snapshot to template and delete the snapshot.

#Script name: Q_vm_export_shapshot_to_template.sh, Example:Q_vm_export_shapshot_to_template.sh  snapshot_name

 

 

 

 

#Dfine varable

 

#Get System Date

DT=$(date +%Y%m%d%H%M%S)

 

#Seting backup dir

backup_dir=/root/vmbk

 

#VM name you want to export, parameter is shapshot name!

 

v_host=$1 

 

##Create vm snapshot 

 

echo "VM snapshot is createing...."

 

echo `xe vm-snapshot vm=$v_host    new-name-label=$v_host$DT`  > $backup_dir/snapshot_uuid

    [ -n  $backup_dir/snapshot_uuid ]

    if [ $? -eq 1 ]; then

 

      echo  "VM snapshot $v_host$DT is created....OK!"

    else

      echo  "VM snapshot $v_host$DT created....FAILSE !"

      exit 1

    fi

 

 

sn_uuid=`cat $backup_dir/snapshot_uuid`

 

echo "Convert snapshot to template and export"

 

echo "Template exporting....."

 

xe snapshot-export-to-template  snapshot-uuid=$sn_uuid   filename=$backup_dir/$v_host$DT.xva

 

if [ $? -eq 0 ]; then

      echo  "Template $v_host$DT Exported...OK!"

    else

      echo "Template $v_host$DT Exported...FAILSE!"

      exit 1

    fi

 

 

cd  $backup_dir

 

echo "VM templates are being compressed...."

 

gzip  $v_host*.xva

 

 if [ $? -eq 0 ]; then

 

      echo  "VM template $v_host*.xva is compressed....OK!"

 else

      echo  "VM template $v_host*.xva is compressed....FAILSE!"

      

      exit 1

 fi

 

 

echo "Deleteing snapshot:"$sn_uuid

 

xe snapshot-uninstall snapshot-uuid=$sn_uuid  force=true

 

if [ $? -eq 0 ]; then

 

      echo   "Snapshot $sn_uuid deleted.....OK!"

 else

      echo   "Snapshot $sn_uuid deleted.....FAILSE!"

      

      exit 1

 fi

 

 

 

###################################################################################################

####                             解压缩模板并导入创建虚拟机                                    ####

###################################################################################################

#!/bin/bash

 

#Version v.1

#Date: 2012.07.20

#Auther:andylhz

#Porpose: Install VM from GZIP template.

#Script name: gzip_vm_install_from_template.sh, Example:gzip_vm_install_from_template.sh  abc_template.gz

 

 

 

 

 

#Import VM template and  install

 

#read  import

 

gunzip $1

 

#gunzip import file

 

importfile=`echo $1 |sed  s/\.gz$//g`

 

#importfile=`echo $import |sed  s/\.gz$//g`

 

xe vm-import filename=$importfile

 

    if [ $? -eq 0 ]; then

 

            echo  "VM Template $importfile import success....OK!"

       else

            echo  "VM Template $importfile import....FAILSE!"

            exit 1

    fi

 

 

 

##Create VM from template

 

echo "Installing VM:"${importfile}clone

 

xe vm-install template=$importfile new-name-label=${importfile}clone  

 

 if [ $? -eq 0 ]; then

 

            echo  "Installing VM:"${importfile}clone....OK!"

       else

            echo  "Installing VM:"${importfile}clone....FAILSE!"

            exit 1

    fi

 

#Starting VM

echo "Starting VM:"${importfile}clone

 

xe vm-start vm=${importfile}clone

 

 if [ $? -eq 0 ]; then

 

            echo "Starting VM:"${importfile}clone....OK!"

       else

            echo "Starting VM:"${importfile}clone....FAILSE!"

            exit 1

    fi

 

 

echo "All options is OK"

 

 

 

###################################################################################################

####                               根据导入模板并创建虚拟机                                    ####

###################################################################################################

 

静默+参数模式:

 

#!/bin/bash

 

#Version v.2

#Date: 2012.07.20

#Auther: andylhz

#Porpose: Install VM from template.

#Script name: Q_vm_install_from_template.sh, Example: Q_vm_install_from_template.sh  template_name

 

 

 

 

##Creater VM from imported template

 

install_tp=$1

 

echo  "Use VM template is:"$1

 

echo  "The VM is installing....." 

 

xe vm-install template=$install_tp new-name-label=${install_tp}clone

 

echo "Starting VM:"${install_tp}clone

 

xe vm-start vm=${install_tp}clone

 

echo "Starting VM:"${install_tp}clone "OK!"

 

echo "All options is OK!"

 

 

###################################################################################################

 

备份中的备份文件夹是挂在远端的NFS 服务器到本地xen服务器上。

NFS配置在这里不做介绍


     本文转自andylhz 51CTO博客,原文链接:http://blog.51cto.com/andylhz2009/945537,如需转载请自行联系原作者


相关文章
|
6天前
|
分布式计算 大数据 Hadoop
【经验分享】用Linux脚本管理虚拟机下的大数据服务
【经验分享】用Linux脚本管理虚拟机下的大数据服务
17 1
|
6天前
|
SQL 关系型数据库 MySQL
【简单无脑】自动化脚本一键安装虚拟机下的MySQL服务
该文章提供了在虚拟机上安装MySQL服务的简化方法,特别是针对新手。作者提供了一个自动化脚本`install_mysql.sh`,使得安装过程更简单。用户需要下载`install.rpm`资源,将其放在指定目录下,然后创建并编辑脚本文件,将提供的代码粘贴进去,通过`chmod u+x`授权,最后运行脚本`./install_mysql.sh [rpm文件路径]`来安装MySQL。文章还附有相关图片说明。
42 1
【简单无脑】自动化脚本一键安装虚拟机下的MySQL服务
|
6天前
|
存储 弹性计算 运维
调整虚拟机内存参数的shell 脚本
【4月更文挑战第29天】
21 0
|
6天前
|
存储 弹性计算 运维
调整虚拟机内存参数的 shell 脚本
【4月更文挑战第29天】
22 2
|
6天前
|
弹性计算 运维 Shell
使用脚本开启关闭虚拟机
【4月更文挑战第29天】
7 2
|
9月前
|
虚拟化
怎么备份VMware虚拟磁盘文件或移植到其他虚拟机
怎么备份VMware虚拟磁盘文件或移植到其他虚拟机
73 0
|
Linux 虚拟化
使用脚本快速初始化VMware下Linux虚拟机新增数据盘
使用脚本快速初始化VMware下Linux虚拟机新增数据盘
123 0
使用脚本快速初始化VMware下Linux虚拟机新增数据盘
|
存储 算法 IDE
Java虚拟机(JVM)面试题备份2
Java虚拟机(JVM)面试题备份2
49 0
Java虚拟机(JVM)面试题备份2
|
存储 算法 IDE
Java虚拟机(JVM)面试题备份
Java虚拟机(JVM)面试题备份
84 0
Java虚拟机(JVM)面试题备份
|
Ubuntu 编译器 Shell
Hi3516开发笔记(三):Hi3516虚拟机基础环境搭建之交叉编译环境境搭建以及开机启动脚本分析
Hi3516开发笔记(三):Hi3516虚拟机基础环境搭建之交叉编译环境境搭建以及开机启动脚本分析
Hi3516开发笔记(三):Hi3516虚拟机基础环境搭建之交叉编译环境境搭建以及开机启动脚本分析