Personal tools
You are here: Home Team Members Scott Glaser AKA Sonar_Guy VMware Backup Scripts vmware_snapshot_all.sh

vmware_snapshot_all.sh

by scott_glaser last modified Jan 30, 2007 10:54 AM

This script is meant to be utilizied to snapshot all vm images on a server that are currently running. It will shutdown the image, create the snapshot, then restart each image.

vmware_snapshot_all.sh — text/x-sh, 2Kb

File contents

#!/bin/bash
# Project:      Vmware Snapshot Script
# Version:      1
# 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.

ROOT_UID=0                              # Only users with $UID 0 have root privileges.
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

# script runs in a loop, snapshots are made of all the images that are currently active inside $VM_DIR
# snapshots will not be conducted on images that are not active
# for machines to be backed up they must be registered and have vmware-tools installed and running on the guest

# needs one parameter (dir of image to back up)
# Snapshot routine will determine the state of the node, based on that it will either snapshot or not snapshot the data.
function snapshot_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 stop)
                if [ "$MESSAGE" != "stop() = 1" ]
                then
                        echo "$1 could not be stopped; 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 snapshoted up, after snapshot, restart vm.
        echo "Creating snapshot of $1..."
#        tar cvzf $BU_PATH/$VM_IMAGE-`date +%F`.tar.gz $1/*
	vmrun snapshot $VM_DIR$1/$1.vmx
        echo "Snapshot complete, restarting $1"
        vmware-cmd ${VM_DIR}${1}/${1}.vmx start
}

cd $VM_DIR
for dir in *; do snapshot_image $dir; done
Document Actions
Log in


Forgot your password?
New user?