[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

7.2

Problems with vmware backup script in ubuntu

Asked by ITDharam in VMware, Ubuntu

Tags: backup, crontab, resume, script, vmware

Hi, I'm using the following script to attempt to backup VMs.  VMs are running on VMWare Server running on Ubuntu.

If I run this script manually, it works.  

If I run it in crontab, it suspends the machine, backs up the machine, but doesn't resume it.

Your help is appreciated.
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
#! /bin/bash
 
##
##  You will need to set appropriate values for VM_HOME, BACKUP_DEST, and
##  VMWARE_CMD.  If you want to see verbose output DEBUG should be true,
##  else false.  DAYS_TO_KEEP_TAR is how long before the script removes an old
##  archive.
##
 
VM_HOME=/home/vmware/
BACKUP_DEST=/media/backup
VMWARE_CMD=/usr/bin/vmware-cmd
DEBUG=true
DAYS_TO_KEEP_TAR=6
 
## include any VMs in this array that you do NOT want backed up.  Use
## the directory name.  For example:  If your vm is $VM_HOME/web0 then put
## 'web0' in the list.  Separate with spaces.
EXEMPTION_ARRAY=( vm1 web0 mail1 )
 
## Do not modify anything below this line
VM_WAS_RUNNING=false
VM_EXEMPT=false
 
cd $VM_HOME
 
##
## Create the backup directories if they do not exist
##
function doCheckDirectories
{
	## if the archives directory does not  exist, create it
	if [ ! -d $BACKUP_DEST/archives ]; then
 
		if [ "$DEBUG" = "true" ]; then
			echo $BACKUP_DEST/archives does not exist, creating.
		fi
 
		mkdir $BACKUP_DEST/archives
	fi
 
	## if the directories directory does not exist, create it
	if [ ! -d $BACKUP_DEST/directories ]; then
 
		if [ "$DEBUG" = "true" ]; then
			echo $BACKUP_DEST/directories does not exist, creating.
		fi
 
		mkdir $BACKUP_DEST/directories
 
	fi
}
 
##
## If this VM is in our exempt array, set VM_EXEMPT to skip entirely.
##
function doCheckExempt
{
	# iterate throught the array, if we get a match, set
	# VM_EXEMPT to true
	for check_vm in ${EXEMPTION_ARRAY[@]}; do
 
		if [ "$check_vm" = "$directoryName" ]; then
 
			if [ "$DEBUG" = "true" ]; then
				echo $vm is on the exception list, skipping.
			fi
 
			VM_EXEMPT=true
		fi
	done
}
 
##
## Set the displayName from the vmx file
##
function setDisplayName
{
	# get the line from the file
        displayLine=`grep displayName $1`
 
	# get the quoted name from it
	quotedName=${displayLine#*=}
 
	# and get rid of the quotes and spaces
	displayName=${quotedName//[\" ]/} #" <- for syntax highlighting
 
	if [ "$DEBUG" = "true" ]; then
		echo $vm displayName is $displayName
	fi
}
 
##
## Set the directoryName from the path
##
function setDirectoryName
{
	# remove the leading /
	removeFront=${vm#*/}
 
	# remove traling / and filename
	removeBack=${removeFront%/*}
 
	# remove the rest of the path
	directoryName=${removeBack##*/}
 
	if [ "$DEBUG" = "true" ]; then
		echo $vm directoryName is $directoryName
	fi
}
 
##
## suspend a VM if its running, skip it if not
##
function suspendVM
{
	# get the current VM state
	vmstate=`$VMWARE_CMD -q $vm getstate`
 
	if [ "$DEBUG" = "true" ]; then
		echo $displayName state is currently: $vmstate
	fi
 
	# if its running, suspend it, otherwise, move on.
	if [ "$vmstate" = "on" ]; then
 
		if [ "$DEBUG" = "true" ]; then
			echo suspending $displayName . . .
		fi
 
		# make sure it actually suspends
		if [ `$VMWARE_CMD -q $vm suspend hard` == 1 ]; then
 
			# track if it was running, so I can restart or not
			VM_WAS_RUNNING=true
 
			if [ "$DEBUG" = "true" ]; then
				echo $displayName Suspended
			fi
 
			return
		else
			echo $vm DID NOT SUSPEND!! Exiting Program.
			exit 0
		fi
	else
		if [ "$DEBUG" = "true" ]; then
			echo $displayName wasnt suspended, not suspending
		fi
	fi
}
 
##
## backup the VM
##
function doBackup
{
	# delete any previous data for this VM
	if [ "$DEBUG" = "true" ]; then
		echo Removing old backup of $displayName
		rm -rfv $BACKUP_DEST/directories/$directoryName
	else
		rm -rf $BACKUP_DEST/directories/$directoryName
	fi
 
	# copy all data to the directories tree
	if [ "$DEBUG" = "true" ]; then
		echo Backing up $displayName to $BACKUP_DEST/directories/$directoryName
		cp -arv $directoryName $BACKUP_DEST/directories/$directoryName
	else
		cp -ar $directoryName $BACKUP_DEST/directories/$directoryName
	fi
}
 
##
## Resume the VM if we it was running in the first place
##
function resumeVM
{
	if [ "$VM_WAS_RUNNING" = "true" ]; then
 
		# reset for next VM
		VM_WAS_RUNNING=false
 
		if [ "$DEBUG" = "true" ]; then
			echo Resuming $displayName . . .
		fi
 
		if [ `$VMWARE_CMD -q $vm start` == 1 ]; then
 
			if [ "$DEBUG" = "true" ]; then
				echo $displayName restarted
			fi
		else
			echo $vm FAILED TO RESUME!! Exiting Program.
			exit 0
		fi
 
	else
		if [ "$DEBUG" = "true" ]; then
			echo $displayName wasnt running in the first place, not resuming.
		fi
	fi
}
 
##
## tgz up the directory for a more compressed and mobile backup.
##
function doTar
{
 
	fileName=backup_$displayName-`/bin/date +%G%m%d`.tgz	
 
	if [ "$DEBUG" = "true" ]; then
		echo taring up $displayName to $fileName
		tar -cvPpszf $BACKUP_DEST/archives/$fileName $BACKUP_DEST/directories/$directoryName
	else
		tar -cpPszf $BACKUP_DEST/archives/$fileName $BACKUP_DEST/directories/$directoryName
	fi 
 
}
 
##
## Clean up any tars that are older than DAYS_TO_KEEP_TAR
##
function doCleanTar
{
	if [ "$DEBUG" = "true" ]; then
		echo Cleaning up tars older than $DAYS_TO_KEEP_TAR
		find $BACKUP_DEST/archives -name "backup_$displayName*.tgz" -ctime +$DAYS_TO_KEEP_TAR -exec rm -vf {} \;
	else
		find $BACKUP_DEST/archives -name "backup_$displayName*.tgz" -ctime +$DAYS_TO_KEEP_TAR -exec rm -vf {} \;
	fi
}
 
##
## Main Loop, iterate through the VMs and handle them apprpriately
##
 
# make sure we have the appropriate directories for backups
doCheckDirectories
 
for vm in $(find $VM_HOME  -name '*.vmx'); do
	if [ "$DEBUG" = "true" ]; then
		echo ooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
		echo Current VM is $vm
	fi
 
		# get my display and directory names, for use in tar name, etc.
		setDisplayName $vm
		setDirectoryName $vm
 
		# check to see if current vm should be exempted
		doCheckExempt
 
		# only back up if it is not on the exempt list
		if [ "$VM_EXEMPT" = "false" ]; then
 
			# suspend my VM if its running
			suspendVM
 
			# actually do the directory backup
			doBackup
 
			# resume the VM if it was running to begin with
			resumeVM
 
			# tar it up
			doTar
 
			# and finally, clean up my old tars
			doCleanTar
		fi
 
		# reset for next vm
		VM_EXEMPT=false
done
[+][-]12/18/07 12:25 PM, ID: 20495112Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]12/19/07 06:39 PM, ID: 20504550Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zones: VMware, Ubuntu
Tags: backup, crontab, resume, script, vmware
Sign Up Now!
Solution Provided By: ITDharam
Participating Experts: 2
Solution Grade: B
 
[+][-]01/07/08 06:47 PM, ID: 20605561Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091021-EE-VQP-81 / EE_QW_2_20070628