VMware

This Question is Possibly Answered

1 "correct" answer available (10 pts) 2 "helpful" answers available (6 pts)
1 2 Previous Next 23 Replies Last post: Jan 30, 2007 3:47 PM by mittell  

ESX 3.0 Backup Script? posted: Sep 14, 2006 7:47 AM

Click to view yingerc's profile Enthusiast 35 posts since
Jul 5, 2006
Here is my setup....I have 4 blade servers that are connected to an MSA1500 for VM storage. I then have another MSA1500 in another building that is setup. Everything is great, both SANS are visible to the ESX servers. The blades are setup to boot from the SAN, so each blade basically runs ESX 3.0. There is a fiber channel fabric connecting both SANS and connecting the blades to the SAN. Nice setup eh?
:)

What I want to happen is for a backup script to run in the middle of the night and "hot" backup the VM's from one SAN to the other.

Questions:

1) What script can I use to do this in ESX 3.0?
2) Do I have to schedule this script to run on ALL of my ESX machines so that ALL the vm's that are running on several different ESX's backup or can I schedule it on one of the ESX's or the virtual center machine to backup them all up. (without consolidated backup)

3) How do I schedule this script to run?

Thanx in advance!

Re: ESX 3.0 Backup Script?

1. Sep 14, 2006 7:56 AM in response to: yingerc
Click to view mittell's profile Champion 3,096 posts since
Apr 25, 2006
1) a shell script using the vcbVmName and vcbMounter commands.
2)Just one, pointing at the VC server. If you don't have a VC server then each host will need to run the script.
3)Using CRON. Do a google for Cron, it's pretty simple and very flexible on when and how often things run.

I have a script I've put together from bits I've found around the web and playing around myself, if you want it you are welcome to it. It's designed to backup every live VM to an SMB mount but could be easily adapted to do VMFS->VMFS backups.

Re: ESX 3.0 Backup Script?

2. Sep 14, 2006 1:38 PM in response to: mittell
Click to view KaZaN's profile Enthusiast 107 posts since
Apr 15, 2006
Hi Mittel,
I am the next one man who will be happy if U can post or send me Ur backup scripts.. so I can use them as template for my ESX3 backup script while I dont have Consolidated BackUp.

Thanx KaZaN

Re: ESX 3.0 Backup Script?

3. Sep 14, 2006 1:55 PM in response to: KaZaN
Click to view beerorkid's profile Enthusiast 48 posts since
Oct 27, 2004
I would be very interested in it as well.

We were using vmbk with 2.5, worked great with cron. i am having trouble getting it to work with vcbsnapall.

We are pretty small, only 2 hosts, no VMotion, Vcenter, and no 3rd party backup. i just get in there and run a command manualy.

nohup vcbSnapAll -a powerstate:on -r /mnt/backups/backupdsk &

it is going to a samba share.

Re: ESX 3.0 Backup Script?

4. Sep 14, 2006 4:19 PM in response to: KaZaN
Click to view mittell's profile Champion 3,096 posts since
Apr 25, 2006
OK here it is, I hope it's of some use to people. I'm intending on expanding it to support FTP/VMFS at some point and also get it to send emails of logs. :)

I know using the IP address may seem like a strange choice, but I found backing up based on vmname unreliable as some of my VMs do not have the same domain name as my esx host. vcbVmName only returns hostname and IP (and moref/uuid but I wanted vm name for the folder names). Can't go wrong if you use a VMs IP so I used that.

You can run it on one host pointing at your VC server, or on each host pointing at itself to make a host backup it's VMs without needing VC.

[code]#!/bin/bash
#
# chmod 700 this file to prevent non-root access to passwords
#
# schedule from /etc/cron.d as root or from roots crontab
#
#### If passwords contains special characters put them in single quotes
mountdir=/mnt/backup
remotedir=backup
smbserver=smb.domain.com
smbuser=backup
smbpass=password
host=VC.domain.com/esxhost.domain.com
searchtype='powerstate:on'
tempdir=/tmp
user=administrator/root
password=password
#
#
#########################################################################
#start script
#########################################################################

#### Mount SMB backup folder ####
if test ! -e "$mountdir" ; then
mkdir $mountdir
fi
mount -t smbfs -o username=$smbuser,password=$smbpass //$smbserver/$remotedir $mountdir
if [ $? -ne 0 ]; then
echo "SMB MOUNT UNSUCCESSFUL"
exit
fi

#### Determine what VMs are on this host ####
for i in `/usr/sbin/vcbVmName -h $host -u $user -p $password -s powerstate:on | grep ipaddr: | awk -F: '{print $2}'`
do echo $i >> $tempdir/hostVMs.txt
done

#### Put VM IP addresses in excludedVMs.txt to stop them being backed up
if test ! -e "$tempdir/excludedVMs.txt" ; then
touch $tempdir/excludedVMs.txt
fi
sort -f -o $tempdir/excludedVMs.txt $tempdir/excludedVMs.txt
sort -f -o $tempdir/hostVMs.txt $tempdir/hostVMs.txt
comm -2 -3 $tempdir/hostVMs.txt $tempdir/excludedVMs.txt > $tempdir/backupVMs.txt


#### Backup non-exluded VMs ####
cat $tempdir/backupVMs.txt |
while read vmip
do vmname=`nslookup $vmip | grep name | awk -F'= ' '{print $2}' | awk -F. '{print $1}'`
echo Backing up VM....
echo $vmname #print what host we're working on
echo $vmip
if test -e $mountdir/$vmname-old; then
rm -rf $mountdir/$vmname-old #remove old backup
fi
if test -e $mountdir/$vmname; then
mv $mountdir/$vmname $mountdir/$vmname-old #archive previous backup
fi
/usr/sbin/vcbMounter -h $host -u $user -p $password -a ipaddr:$vmip -r $mountdir/$vmname > $mountdir/$vmname.log
if [ $? -ne 0 ]; then
echo "Backup FAILED for $vmname with the following errors:" >> $mountdir/errors.log
fi
cat $mountdir/$vmname.log | grep Error >> $mountdir/errors.log
done

#### Clean up temporary files ####
rm -f $tempdir/hostVMs.txt[/code]

Re: ESX 3.0 Backup Script?

5. Sep 14, 2006 3:05 PM in response to: mittell
Click to view KaZaN's profile Enthusiast 107 posts since
Apr 15, 2006
Thank You Man, I will try this asap...
KaZaN

Re: ESX 3.0 Backup Script?

6. Sep 14, 2006 5:01 PM in response to: yingerc
Click to view mpozar's profile Enthusiast 102 posts since
Oct 2, 2003
Here is a SCRIPT provided by TAS that attempts to do what vmbk.pl was doing with ESX 2.5.x.:

http://www.techarchsolutions.com/downloads.asp

Have FUN!

Michael Pozar

Re: ESX 3.0 Backup Script?

7. Nov 3, 2006 1:19 PM in response to: mittell
Click to view BigJon's profile Novice 35 posts since
Nov 8, 2004
mittell, I am having a few probs, any idea what is going on?

Also, in the /tmp directory, is the hostVMs.txt supposed to have thier name, ip address? What info is supposed to be in there?

The error I get is:

Backing up VM....

mv: cannot copy a directory, `/mnt/backup/', into itself, `/mnt/backup/-old'

I was running this script manually, to see if it works first.

Thanks for your help!!

Re: ESX 3.0 Backup Script?

8. Nov 3, 2006 3:29 PM in response to: BigJon
Click to view mittell's profile Champion 3,096 posts since
Apr 25, 2006
What user are you running the script as? What username and password have you used in the script?

hostVMs.txt should contain a list of IPs of VMs on the ESX host or VC server that are powered on. The error you're getting implies that hostVMs.txt is empty, so null is returned for $vmname and the script tries to do "mv /mnt/backup/ /mnt/backup/-old #archive previous backup" which isn't allowed so throws an error.

Try running the following and see if you get a list of IPs returned to the console. You need to run all of this as root, $user is root or administrator for esxhost or vc server respectively.

[code]/usr/sbin/vcbVmName -h $host -u $user -p $password -s powerstate:on | grep ipaddr: | awk -F: '{print $2}'[/code]

Also check out this thread I've revised the script slightly.

http://www.vmware.com/community/thread.jspa?threadID=58570

Re: ESX 3.0 Backup Script?

9. Nov 6, 2006 5:14 AM in response to: mittell
Click to view BigJon's profile Novice 35 posts since
Nov 8, 2004
This is what I get when run as root:

rstate:on | grep ipaddr: | awk -F: '{print $2}'
VMware Consolidated Backup -- VM Locator Utility, Version 1.0
(C) 2005, VMware, Inc. All rights reserved.
Usage:
vcbVmName -h <url> -u <username> -p <password>
-s <searchSpec> [-c <cachefile>] [<verbosity>]

<url> := <hostname>[":"<port>]
<username> := <string>
<password> := <string>
<searchSpec> := <Type>":"<Qualifier>
<Type> := "Any"|"IpAddr"|"MoRef"|"Uuid"
<Qualifier> := <string>
<cachefile> := <filename>
<verbosity> := -L (0-6)

Example:
Use the following command line to locate a VM with the host name
"buvm01.eng.vmware.com" on the ESX server "esx17.vmware.com"
using the user name "sdk" and the password "foobar":

vcbVmName -h esx17.vmware.com -u sdk -p foobar -s ipaddr:buvm01.eng.vmware.com

Re: ESX 3.0 Backup Script?

10. Nov 6, 2006 5:58 PM in response to: BigJon
Click to view mittell's profile Champion 3,096 posts since
Apr 25, 2006
Right, well that means that vcbVmName is being passed incorrect parameters. Can you tell me what you've put in the variables at the top of the script? (not including your passwords or real IPs of course!)

Re: ESX 3.0 Backup Script?

11. Nov 9, 2006 1:27 PM in response to: mittell
Click to view BigJon's profile Novice 35 posts since
Nov 8, 2004
I don't put anything in there. I just cut and pasted what was there. I apologize for my stupidity, I am still tring to learn scripting in a linux enviroment.
The orginal script you have posted, I did fill in that information. Is that what you were asking about?

Message was edited by:
BigJon

Re: ESX 3.0 Backup Script?

12. Nov 10, 2006 12:05 AM in response to: BigJon
Click to view mittell's profile Champion 3,096 posts since
Apr 25, 2006
You need to fill in the variables at the top of the script with information appropriate for your environment. e.g. root as the user if you're pointing the script at a single ESX host, or administrator if you're pointing the script at your VC server.

You need to change most of them, but you can leave the /mnt/backup and /tmp paths as they are.

e.g.

mountdir=/mnt/backup #you can leave this
remotedir=backup #this needs to be your SMB mount name
smbserver=smb.domain.com #this needs to be your SMB server FQDN
smbuser=backup #this needs to be a user on your SMB server with access to your backup share
smbpass=password #above users password
host=VC.domain.com/esxhost.domain.com #the FQDN of your ESX host or VC server
searchtype='powerstate:on' #leave this to backup powered on VMs
tempdir=/tmp #you can leave this
user=administrator/root #administrator for VC, root for an ESX host
password=password #above users password

Re: ESX 3.0 Backup Script?

13. Nov 10, 2006 12:54 AM in response to: mpozar
Click to view daikyu's profile Expert 610 posts since
Sep 23, 2004
nice, thanks

Re: ESX 3.0 Backup Script?

14. Nov 13, 2006 10:26 AM in response to: mittell
Click to view BigJon's profile Novice 35 posts since
Nov 8, 2004
So do I add this line to the backup script?

/usr/sbin/vcbVmName -h $host -u $user -p $password -s powerstate:on | grep ipaddr: | awk -F: '{print $2}'


Like I said, I am not a linux scripting guru, and I am trying to understand how this works. Thanks

VMware Developer

SDKs, APIs, Videos, Learn and much more in the Developer community.

Learn More

Developer Sample Code

Increase your developer productivity with VMware API sample code.

Learn More

VMworld Sessions & Labs

Online access to the latest VMworld Sessions & Labs and online services.

Learn more

Purchase PSO Credits Online

Purchase credits to redeem training and consulting services online.

Buy Now

Community Hardware Software

View reported configurations or report your own.

Learn More

VMware vSphere

Come witness the next giant leap in virtualization.

Register Today

Communities