码迷,mamicode.com
首页 > 其他好文 > 详细

XenServer 虚拟机备份脚本

时间:2016-08-16 14:43:33      阅读:656      评论:0      收藏:0      [点我收藏+]

标签:

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

首先做一个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配置在这里不做介绍

XenServer 虚拟机备份脚本

标签:

原文地址:http://www.cnblogs.com/supper-hjx/p/5776224.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!