How to automate snapshots of Windows machines hosted with Amazon EC2 Web Services

IFM supplies engineering services for $NZ180+GST per hour. If you require assistance with designing or engineering an Amazon solution - hire us!

There are lot of scripts for automating the snapshot'ing of machines hosted at Amazon Web Services, but many of them require Linux, or for you to install an additional scripting language. Suppose you just want something you can dump on a Windows host and get going?

I came across this solution by Sehmer, but it didn't seem to work correctly. So I re-wrote it, and turned it back into a single "DOS" batch file. It is intended that you run it each day. It will take keep a rolling set of backups over 7 days. It does this by snapshot'ing every volume that is attached to every machine in the defined region, and deleting any snapshots that the script has created from the same day in the prior week. Because it does every machine in the region you only need to run this on one machine to backup every machine. Also note that it doesn't have to run on a machine actually located at Amazon - you can run this from your workstation as well.

To use this download the zip file, and extract it to your C:\ drive. You also need to install the Java 7 runtime. I have only used the 64 bit runtime on 64 bit machines - if you install the 32 bit runtime on a 64 bit machine you'll need to change the JRE path in the batch file. After extracting the zip file you should end up with a C:\AWS directory. Inside of this are the EC2 API tools installed and ready to go, a working directory used to store some state, and a script called "awsSnapBackup.cmd".

Edit the awsSnapBackup.cmd file, and make sure you configure your AWS Access and Secret keys. Also change the EC2_URL to match the region you are using (it is setup for Sydney availability zone 2 by default).

Then give it a test run by double clicking on it, and if all goes well, use the Windows task Scheduler to schedule it to run every day. I normally run it under the SYSTEM account, with no special privileges.

Below is a copy of the batch file stored in the downloadable zip file.

@echo off

:: This script snapshots all volumes that area attached to all EC2 instances in the region
::
:: This script is based on http://sehmer.blogspot.co.nz/2013/02/update-amazon-ec2-daily-snapshot.html
:: by Sehmer.

:: Set login details
set AWS_ACCESS_KEY=xxx
set AWS_SECRET_KEY=xxx

:: Set Region
set EC2_URL=https://ec2.ap-southeast-2.amazonaws.com

:: Java home directory just under the bin for java.exe
set JAVA_HOME="C:\Program Files\Java\jre7"

:: Path for AWS home directory
set AWS_HOME=C:\AWS

:: Path to EC2 API and subfolder of bin
set EC2_HOME=%AWS_HOME%\ec2-api-tools-1.6.7.3
set PATH=%PATH%;%EC2_HOME%\bin

:: Set today's day of week parameter
for /F "tokens=1 delims=/ " %%a in ('DATE/T') do set dateDOW=%%a

:: First delete all snapshots from this day last week

:: Create a file with all scheduled snapshots
ec2-describe-snapshots|find /i "%dateDOW%: Weekly Backup for">%AWS_HOME%\Working\SnapshotsDOW.txt

:: Delete snapshots for older backups
for /F "tokens=2" %%i in (%AWS_HOME%\Working\SnapshotsDOW.txt) do cmd /c ec2-delete-snapshot %%i

:: Now create a snapshot for every attached volume to every instance

:: Create a file with all attached volumes
ec2-describe-volumes|find /i "attached">%AWS_HOME%\Working\ActiveVolumes.txt

:: Create a file with all instances
ec2-describe-instances|find /i "TAG"|find /i "Name">%AWS_HOME%\Working\InstanceNames.txt

:: Create snapshots of all attached volumes
for /F "tokens=2,3" %%d IN (%AWS_HOME%\Working\ActiveVolumes.txt) do for /F "tokens=3,5*" %%a IN (%AWS_HOME%\Working\InstanceNames.txt) do if %%a EQU %%e ec2-create-snapshot %%d -d "%dateDOW%: Weekly Backup for %%b (VolID:%%d InstID:%%e)"

 

 

Cisco Systems Partner - Premier Certified