[转]VMware ESX 3.0 VM Backup script by Tooms

转自http://ktcheong.spaces.live.com/blog/cns!72B9F5204F2D5FF2!312.entry

VMware ESX 3.0 VM Backup script by Tooms

I was looking around the internet for a backup script to backup the VM guests on a standalone ESX 3.0 and was not finding any so I start to make my own.

Things I need the script to-do was.

 

  • HOT Backup of all VM guest on the server
  • Backup of VM guests in any power state
  • Backup to a local folder on the server where the remote backup server will pick them from and put it to tapes
  • Auto cleanup of old backup log and VM files (older then 1 day)
  • Execute one script if there was a error and another if there was no error
  • No SAN, work on a standalone ESX server with local storage

and yes I know esxRanger 2.0 for ESX 3.0 can do all this, but that product was not finish and public when I start to make this script, maybe I will change to esxRanger but that will time tell or maybe I will use them both


Please be nice as this is my first Linux bash script and I am just a windows admin ;-) so there is for sure a number of error and smarter ways to do thing, but it works for me.. and if you give me feedback I will fix the script.



Here is the script and how to use it

1. login
login as root on the ESX 3.0 console


2. edit backuptools.conf
First edit the /etc/vmware/backuptools.conf file by running the command "nano /etc/vmware/backuptools.conf"
This is need in order for the vmware backup tools to work...
Out comment the red lines and put in the blue lines


#
# URL for the VC SDK instance to connect to. The format is
# [:port]
#
# VCHOST=myvirtualcenter.company.com
VCHOST=localhost

#
# Username to use for VC SDK authentication
#
# USERNAME=backup
USERNAME=root

#
# Password to user for VC SDK authentication
# WARNING: It is recommended that you don't specify the
# password in the configuration file.
#
# PASSWORD=XXX
PASSWORD=YourPassword

 

note the best thing will be to make a account for backup and not use the root account.

 

3. make a folder for the backup script
make a folder with the command "mkdir /etc/esxbackup"


4. backup script
make the script by using the command "nano
/etc/esxbackup/backup.bash" and copy the script below into it.
change the $TOfolder to be where you like the backup to be put.

###########################################################################
#!/bin/bash
#
# ESX 3.0 Backup script build by [email protected]
#
# 20060905 Minor fixs by tooms
# 20060830 First version by tooms
#
# Change the $TOfolder to be the folder where the backup files will be placed in
#
#


###########################################################################
# Path the vmware apps need.
PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"


###########################################################################
# the base Path where the backup will be placed
# Change this path to match your setup
TOfolder=/vmfs/volumes/storage1/backup


############################################################################
# make the sub folder name ready from date/time
DTfolder=$(date '+%Y%m%d-%H%M')


############################################################################
# making the base folders ready if not exist
if test ! -x "$TOfolder" ; then
  mkdir -p $TOfolder
fi


############################################################################
# make a log file ready
rm -f $TOfolder/backup.log
Backuplog=$TOfolder/backup.log
echo " " >>$Backuplog
echo "The Date/time is $(date '+%Y%m%d-%H%M%S')" >>$Backuplog
echo " " >>$Backuplog


############################################################################
# making the sub-folders ready if there not exist
if test ! -x "$TOfolder/$DTfolder" ; then
  echo "Make folder: $TOfolder/$DTfolder" >>$Backuplog
  mkdir -p $TOfolder/$DTfolder
fi
if test ! -x "$TOfolder/$DTfolder/vm" ; then
  echo "Make folder: $TOfolder/$DTfolder/vm" >>$Backuplog
  mkdir -p $TOfolder/$DTfolder/vm
fi
if test ! -x "$TOfolder/$DTfolder/log" ; then
  echo "Make folder: $TOfolder/$DTfolder/log" >>$Backuplog
  mkdir -p $TOfolder/$DTfolder/log
fi


############################################################################
# move backup log to the new subfolder
mv $TOfolder/backup.log $TOfolder/$DTfolder/log/
Backuplog=$TOfolder/$DTfolder/log/backup.log
echo " " >>$Backuplog
echo "Move backup log to $Backuplog" >>$Backuplog


############################################################################
# build content log of $TOfolder before backup and cleanup
echo " " >>$Backuplog
echo "Build content_before.log of $TOfolder before backup and cleanup" >>$Backuplog
ls -R -la $TOfolder >>$TOfolder/$DTfolder/log/content_before.log


############################################################################
# Delete content from the $TOfolder there is older then 1 day
echo " " >>$Backuplog
echo "Delete content from the $TOfolder there is older then 1 day" >>$Backuplog
find $TOfolder/* -type d -maxdepth 0 -mtime +1 -exec rm -d -r -f -v {} >>$Backuplog /;


############################################################################
# Delete old vcbSnapAll logfiles from /var/log/vmware/ there is older then 1 day
echo " " >>$Backuplog
echo "Delete old vcbSnapAll logfiles from /var/log/vmware/ there is older then 5 min." >>$Backuplog
find /var/log/vmware/vcbSnapAll-* -type d -maxdepth 0 -mtime +1 -exec rm -d -r -f -v {} >>$Backuplog /;


############################################################################
# make the VM export/backup
echo " " >>$Backuplog
echo "Laver export af alle VM" >>$Backuplog
vcbSnapAll -a any -r $TOfolder/$DTfolder/vm/ -L 6 >>$Backuplog


############################################################################
# Getting the exitcode from vcbSnapAll
vcbSnapAll_exitcode=$(expr $?)
echo " " >>$Backuplog
echo "The exitcode is $vcbSnapAll_exitcode" >>$Backuplog


############################################################################
# copy the vcbSnapAll logfiles to the logfolder
echo " " >>$Backuplog
echo "copy the vcbSnapAll logfiles to $TOfolder/$DTfolder/log/" >>$Backuplog
cp -r -v /var/log/vmware/vcbSnapAll-* $TOfolder/$DTfolder/log/ >>$Backuplog


############################################################################
# build content log of $TOfolder after backup and cleanup
echo " " >>$Backuplog
echo "Build content_after.log of $TOfolder after backup and cleanup" >>$Backuplog
ls -R -la $TOfolder >>$TOfolder/$DTfolder/log/content_after.log


############################################################################
# write end time in logfile
echo " " >>$Backuplog
echo "The Date/time is $(date '+%Y%m%d-%H%M%S')" >>$Backuplog


############################################################################
# Checking for errors
echo " " >>$Backuplog
if [ $vcbSnapAll_exitcode = 0 ]; then
  echo "Error: none" >>$Backuplog
  echo " " >>$Backuplog
  #run script if all okey !
else
  echo "Error: got exitcode $vcbSnapAll_exitcode" >>$Backuplog
  echo " " >>$Backuplog
  #run script if there is a error !
fi



5. Make it a script
run this command "chmod 777 /etc/esxbackup/backup.bash" to make it a script there can be run from the commandline


6. When to run the backup
edit the crontap fil with the command "nano /etc/crontap" to run the script at 21:00 every evening
Add the line mark with blue

SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/

# run-parts
01 * * * * root run-parts /etc/cron.hourly
02 4 * * * root run-parts /etc/cron.daily
22 4 * * 0 root run-parts /etc/cron.weekly
42 4 1 * * root run-parts /etc/cron.monthly
00 21 * * * root /etc/esxbackup/backup.bash
 


7. Try the script
Try the script by running the command "/etc/esxbackup/backup.bash"


8. Look at the backup log
Look at the backup log with the command "nano /vmfs/volumes/storage1/backup/20060905-1800/log/backup.log"
note that the path will offcource not be the same when you use it...


9. Backup to tape
Now setup your remote backup server to backup any thing in the /vmfs/volumes/storage1/backup folder, I use Backup Exec 10D on a windows server with the backup exec 9.1 linux agent installed in the ESX 3.0 server COS.
That works very well with backup speed at around 1500mb/min over a 1gbit network with a LTO3 24 tape autoloader from Dell on the windows server.



And yes before you ask how to restore a VM from the backup folder......
First turn off the VM there need to be restore and do like this

[root@ESX3 root]# vcbRestore -s /vmfs/volumes/storage1/backup/20060905-1800/vm/vm01/ -b overwrite
[2006-09-06 18:30:00.140 'App' 3076448384 info] Current working directory: /root
[2006-09-06 18:30:00.157 'BaseLibs' 11344816 warning] [Vmdb_Unset] Unsetting unknown path: /vmomi/

Converting "/vmfs/volumes/storage2//VM_vm01/vm01.vmdk" (VMFS (flat)):
0%=====================50%=====================100%
**************************************************

[root@ESX3 root]#
 

 

 

Now turn on the VM that is just restore and verify it is working.


hope this help you on the way and works on your box, let me know in the forum if you have any smart things there need to be add to the backup script and I will then update it from time to time...


not bad from a windows admin with only a very little linux skills ;-)

 

 

你可能感兴趣的:(vmware,windows,server,command,delete,Build)