Last active
October 26, 2015 19:55
-
-
Save phin3has/6686eef831e3db8dd3ea to your computer and use it in GitHub Desktop.
Backs up Linux Hosted VMware Workstation Machines
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Backs up VM workstations, will suspend if you provide a name (see line 57) | |
# Written by @awhitehatter, 10-26-15 | |
LOGFILE="/var/log/vmbak.log" | |
BACKUPD="/path/to/backup" | |
BACKDIR="$(date +"%d-%m-%y")-bak" | |
# Backup function | |
function backup { | |
#Test for any running VMs | |
VMTEST=`vmrun list|grep 0` | |
if [ -z "$VMTEST" ]; then | |
echo "$(date) -- Must be a VM running. Terminating Job" >> $LOGFILE | |
echo "$(date) -- To auto suspend VM, add VM to VMSTAT function" >> $LOGFILE | |
exit 1 | |
fi | |
# Now run the backup Job | |
echo "$(date) -- Backing up VMs" >> $LOGFILE | |
rsync -a /home/username/vmware/ $BACKPATH >> $LOGFILE 2>&1 | |
echo "$(date) -- Backup Complete, now compressing backups" >> $LOGFILE | |
tar cvzf $BACKPATH-bak.tar.bz $BACKPATH --remove-files >> $LOGFILE 2>&1 | |
echo "$(date) -- Backup Job Completed" | |
} | |
# VM status function | |
function vmstat { | |
VMRUNSTATUS=`vmrun list|grep -i $1` | |
if [ -z $VMRUNSTATUS ] | |
then | |
echo "$(date) -- $1 is not running" >> $LOGFILE | |
else | |
vmrun suspend $VMRUNSTATUS | |
echo "$(date) -- Ran vmrun suspend on $VMRUNSTATUS" >> $LOGFILE | |
fi | |
} | |
# Check to see if the back up drive is mounted | |
if [ -d $BACKUPD ] | |
then | |
echo "$(date) -- Backup Drive mounted, beginning backup" >> $LOGFILE | |
else | |
echo "$(date) -- Backup Drive not found, terminating job" >> $LOGFILE | |
exit 1 | |
fi | |
# remove old directories | |
find $BACKUPD -mtime +14 -exec rm -rf {} \; | |
# set up location for backup job | |
mkdir $BACKUPD/$BACKDIR >> $LOGFILE 2>&1 | |
BACKPATH="$BACKUPD/$BACKDIR" | |
# run vmstat to pause running vms | |
# Give vmstat the name of your VM | |
vmstat "windows_vm" | |
# run backup function | |
backup | |
# backup the log file | |
cat $LOGFILE > $BACKUPD/vm_backup.log | |
exit 0 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment