backup_vmware.sh
This is the complete backup script to backup a vmware servers images to another remote server.
backup_vmware.sh
—
text/x-sh,
3Kb
File contents
#!/bin/bash
# Project: Vmware Backup Script
# Version: 2
# Date Started: 2006-11-04
# Written By: Emily Brantley, Scott Glaser, and Steven Schwartz
# Copyright (c) 2006 by Scott Glaser. This material may be distributed only subject to the terms and
# conditions set forth in the Open Publication License, v1.0 or later. Distribution of substantively
# modified versions of this document is prohibited without the explicit permission of the copyright
# holder. Distribution of the work or derivative of the work in any standard (paper) book form is
# prohibited unless prior permission is obtained from the copyright holder.(the latest version is
# presently available at http://www.opencontent.org/openpub/).
#
# For more information contact: sonar_guy<AT>c-ccom.com
#
# NOTES: This script runs in a loop, backing up all the images of machines runnng inside $VM_DIR
# machines to be backed up must be registered. To register an image as root execute the command
# vmware-cmd -s register "/disk2/Virtural_Machines/mail01.theglaserfamily.org/mail01.theglaserfamily.org.vmx"
# replace mail01.theglaserfamily.org.vmx with you image file.
# This script also requires that you vmware-config-tools running inside the guest for the script
# to execute properly.
VM_DIR=/disk2/Virtural_Machines/ # Path to virtural machines, this will vary based on you installation.
VM_IMAGE=mail01.theglaserfamily.org # Name/Directory of the virtural machine image
BU_PATH=/disk2/backups # Path to the backup directory
TMP_LOC=$BU_PATH/tmp/ # Path to make tmp copies this directory must exist prior to running script
SSHID=sglaser # SSHID ??
USER=sglaser # Username of the account to use for SSH
HOST=kermit # Host name of the machine to be copied to
# needs one parameter (dir of image to back up)
# Copy routine will determine the state of the node, based on that it will either copy or not copy the data.
function copy_image()
{
STATE=$(vmware-cmd ${VM_DIR}${1}/${1}.vmx getstate)
if [ "$STATE" = "getstate() = on" ]
then
echo "$1 is running; stopping..."
MESSAGE=$(vmware-cmd ${VM_DIR}${1}/${1}.vmx suspend)
if [ "$MESSAGE" != "suspend() = 1" ]
then
echo "$1 could not be suspended; it won't be backed up."
return 1
fi
else
echo "$1 is not running; it won't be backed up."
return 1
fi
# copy VM to temorary dir to be backed up, after copy, restart vm.
echo "Copying $1 files to temp directory..."
MESSAGE1=$(date +%T)
echo $MESSAGE1
cp -Rp ${VM_DIR}${1} $TMP_LOC
MESSAGE2=$(date +%T)
echo $MESSAGE2
echo "Backup complete, restarting $1"
vmware-cmd ${VM_DIR}${1}/${1}.vmx start
}
# Backup routine will tar the copied image to the backup location, then remove temp data.
function backup_image()
{
echo "Backing up $1..."
tar cvzf $BU_PATH/$1-`date +%F`.tar.gz $1/*
echo "Backup of $1 Completed"
rm -rf $1
echo "Temp file removed for $1"
}
# SCP routine will move the backup images to the host desired.
function scp_image()
{
echo "Moving image $1 to $HOST..."
scp -i $SSHID *.gz $USER@$HOST/home/$USER/
echo "Move of files backup images Completed"
echo "Removing local tar files..."
rm -rf *.gz
echo "Local tar files deleted."
}
cd $VM_DIR
for dir in *; do copy_image $dir; done
cd $TMP_LOC
for dir in *; do backup_image $dir; done
cd $BU_PATH
do scp_image; done

