Backing up ESXi images using ghettoVCB and FTP


Update: mksbackup can also do this: http://www.magikmon.com/mksbackup/ghettovcb.en.html

ghettoFTP currently doesn’t support backing up VMs to FTP so I’ve written a simple wrapper shell script that ftps the backups that ghettoVCB creates.

You’ll need the ghettoVCB scripts which you can grab from the VMware communities forum here.

The script does the following:

1. Queries the local host for a list of VMs (esxcli vms vm list)

2. For each VM it runs the ghettoVCB shell script

3. At the end of each VM backup it uploads the backup to an FTP server using ESXi’s built-in ftpput command.

ESXi doesn’t have any cron built in which means you’ll either have to run the script manually (a pain!) or schedule it from another box.

I use plink which you can use in a scheduled task to kick off the script via SSH. You’ll obviously need to enable the remote SSH on the ESXi box.

#!/bin/sh
###############################################################################################
# Name: runbackup.sh
# Desc: This backup uploads the gzipped VM files produced by ghettoVCB.sh
# History:
#
# Date          Author          Version         Details
# 28/02/2011    Stuart Jordan   1.0             Script creation
# 01/02/2011    Stuart Jordan   1.1             Amended to upload directories rather than gzips
#                                               Added tidying up of directories post backup
#

ftpUsername="ftpusername"
ftpPassword="ftppassword"
ftpServer="ftpserver"
backupPath="/vmfs/volumes/localvmfsfolder/backups"
scriptPath="/path/to/the/ghettoVCB/script/folder"

esxcli vms vm list  | grep "Display Name:" | awk '{print $3}' | while read currVM
do
        echo $currVM > $scriptPath/curr_backup
        $scriptPath/ghettoVCB.sh -f $scriptPath/curr_backup
        rm -f $scriptPath/curr_backup

        if [ -d "$backupPath/$currVM" ]
        then
                find $backupPath/$currVM -type f |
                while read currBackup
                do
                        echo Processing $currVM
                        filename=$(basename $currBackup)
                        echo File is named $filename
                        filePath=$(dirname $currBackup)
                        echo Uploading file $currBackup to $ftpServer using account $ftpUsername
                        ftpput -u $ftpUsername -p $ftpPassword $ftpServer /VMware/$currVM/$filename $currBackup
                        retval=$?
                        if [ $retval -gt 0 ]
                        then
                                echo Error $retval connecting to FTP server $ftpServer and uploading file $filename
                                else
                                        echo FTP completed OK. Deleting $currBackup file.
                                        rm -f $currBackup
                                fi
                        done

                        find $backupPath/$currVM/* -type d |
                        while read currBackupDirs
                        do
                                if [ -d "$currBackupDirs" ]
                                then
                                        echo Removing directory $currBackupDirs
                                        rmdir $currBackupDirs
                                fi
                        done

                        if [ -d "$backupPath/$currVM" ]
                        then
                                echo Removing $backupPath/$currVM
                                rmdir $backupPath/$currVM
                        fi
                retval=$?
                        if [ $retval -gt 0 ]
                        then
                                echo Error $retval cleaning up directory $filePath. It probably still has some files in there.
                        fi
        else
                echo $backupPath/$currVM is missing
        fi
        echo Completed $currVM
done

Download – rename the file to .sh

Advertisements
Tagged , , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: