Quantcast
Channel: VMware Communities : Popular Discussions - VMware Server 1
Viewing all articles
Browse latest Browse all 74849

Shell Script for Backup on RedHat

$
0
0

I threw this code together to help with my backup of VMs.

 

I hope some others may benefit from it. Any suggestions to make it better are gladly accepted!

 

#!/bin/bash

 

#Set Date into number only format

DATE=`date +%m%d%y`

#Define logfile path

VMLOG=/your/vmlogdir/vmsuspend$DATE.log

 

#Log Formatting

 

echo "----


" > $VMLOG

echo "----


" | tee -a $VMLOG

 

#Delete log files in excess of 7 days

find /yourvm/dir -type f -mtime +7 -print | xargs /bin/rm -f

#Delete previous days tarballs

find /yourbackupdir -type f -mtime +0 -print | xargs /bin/rm -f

 

#Loop for catching all VMs

for vm in $(vmware-cmd -l)

do

 

#Define path of current VM

currvmpath=`dirname $vm`

#Define image name of current VM

currvmname=`basename $vm`

 

#Echo name of current VM to logfile

echo "$vm" | tee -a $VMLOG

#Set begin time in seconds(used for timing)

begintime=`date +%s`

echo "Suspend began:........`date`" | tee -a $VMLOG

#Suspend statement

vmware-cmd "$vm" 'suspend' soft | tee -a $VMLOG

echo "Suspend ended:........`date`" | tee -a $VMLOG

echo "Tar began:............`date`" | tee -a $VMLOG

#Tar with compression statement

tar -czf /yourbackupdir/"$currvmname""$DATE".tgz $currvmpath

echo "Tar ended:............`date`" | tee -a $VMLOG

echo "Restart began:........`date`" | tee -a $VMLOG

#Start VM statement

vmware-cmd "$vm" 'start' soft | tee -a $VMLOG

echo "Restart ended:........`date`" | tee -a $VMLOG

#Set end time in seconds(used for timing)

endtime=`date +%s`

#Calculate how long the process took in seconds

elapsed=$(expr "$endtime" - "$begintime")

echo "Time to complete:....."$elapsed" seconds" | tee -a $VMLOG

echo "**************************************************" | tee -a $VMLOG

 

done

 

#Log formating

echo "----


" | tee -a $VMLOG

echo "----


" | tee -a $VMLOG

 

#Statement to mail log file to appropriate users

mail -s VM Preperation Routine you@yourdomain.com< /your/vmlogdir/$VMLOG

/code

 

 

 

This script suspends each registered VM one at a time. While each machine is suspended it creates a compressed tar ball of the VM directory. Then it restarts the machine after the archive is complete.

 

 

 

 

JonZ


Viewing all articles
Browse latest Browse all 74849

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>