Windows Backups through Scripts

I am a big advocate of making backups.  I use an automated, real time cloud based backup solution for my documents directory.  This is easy, SkyDrive has a free app for that.  I take daily bare-metal recovery snapshots for incremental backups on dedicated hard disks; again, this is functionality that is built into Windows.  I also like to make a monthly complete system backup on an external hard drive that I can take offsite.  This is harder.  My situation is unique—I have almost 3 terabytes of files that I just can’t lose.  I needed a way to quickly plug in an external hard drive, start a program, and have it UPDATE THE CHANGES that had taken place since the last time I had made a backup.  Not being able to find a backup tool that was small, portable, free, and operated the way I wanted it to, I decided to make my own.

The simplest way to accomplish this is through a tool called ROBOCOPY.  “Robust File Copy” is a command-line replication command and was introduced as a standard feature of Windows Vista and is also available in later operating systems.  If operating in Windows XP and it’s not already installed, it’s available through the Windows Resource Kit.

ROBOCOPY has many features available, and I highly encourage you to read about them.  http://en.wikipedia.org/wiki/Robocopy might be a good place to start.  However, for my purposes I use a few switches that you might find useful if you plan to create backup scripts as I have.

Here is an example of a simple backup script, named Backup.bat that I have the Windows Task Scheduler run nightly on one of my workstations:

@ECHO off
ROBOCOPY “C:\Storage\SkyDrive” “\\server\Backups\SkyDrive Mirror” /MIR /r:1 /w:1
ROBOCOPY “C:\Storage\SharePoint\Team Site – Shared Documents” “\\server\Backups\Sharepoint Mirror” /MIR /r:1 /w:1
REM End of script.

A few things to note.  First, UNC paths are supported.  I use the /MIR switch, which performs directory mirroring.  ROBOCOPY is smart enough to detect changes in the source directory, C:\Storage\SkyDrive and update those changes to \\server\Backups\SkyDrive Mirror.  If I add a file or directory, it gets backed up the next time the script runs.  If those files or directories get deleted at the source, those changes are also reflected to the backup.  Files and directories with no changes are skipped.  Given that this a mirroring backup solution, the first time the script runs, the backup process can potentially be lengthy (depending on the amount of data to transfer).  However, subsequent runs might only be seconds long if there are only a few changes to write.  The other switches I use are /R to delineate retry attempts (I have them essentially disabled) and /W to set wait time between retries, in seconds.  My preferred method skips locked or unreadable files, which may or may not work for you.

Simple, right?

Here’s something a little more interesting.  For my monthly offsite backup, I have a large capacity external hard drive with a different script on it that I manually execute.  Since it’s meant to back up so much data, I like to be able to choose what to backup, and when.  So I built an interface.  While it may not be the most elegant script, it does work well and isn’t too hard to customize.  Feel free to play with it.

File:  Offsite_backup.bat

@ECHO off
SETLOCAL
CALL :disclaimer
CALL :menu

REM Thomas Dye 11/30/2012 Offsite backup script
REM Good, you’re looking inside the batch file.
REM This script is customized to the user and will not work for you until modified.
REM Sections for userVariables, userLocations, setVariablesAttended, and setVariablesUnattended should be modified for your use.
REM Begin subroutine modifications below.

:userVariables
REM Predefine any sources for backups that will be listed in the menu.

SET source1name=Hyper-V machines
SET source2name=Home folders
SET source3name=Backup folders
SET source4name=Media folders
SET source5name=Software Library folders
GOTO :eof

:userLocations
REM Predefine file locations.
REM If you have five source names, you should also have five entries here.

IF /I “%source1backup%”==”y” ROBOCOPY “\\server\Hyper-V” “Hyper-V” /MIR /r:1 /w:1
IF /I “%source2backup%”==”y” ROBOCOPY “\\server\Home” “Home” /MIR /r:1 /w:1
IF /I “%source3backup%”==”y” ROBOCOPY “\\server\Backups” “Backups” /MIR /r:1 /w:1
IF /I “%source4backup%”==”y” ROBOCOPY “\\server\Media” “Media” /MIR /r:1 /w:1
IF /I “%source5backup%”==”y” ROBOCOPY “\\server\Softlib” “Softlib” /MIR /r:1 /w:1
GOTO :eof

:setVariablesAttended
REM This section is run when the user wants to choose which directories are backed up.
REM Ensure that your backup source matches the source name.

CALL :userVariables
ECHO(
SET /P source1backup=Do you want to backup %source1name%? (y/n)
SET /P source2backup=Do you want to backup %source2name%? (y/n)
SET /P source3backup=Do you want to backup %source3name%? (y/n)
SET /P source4backup=Do you want to backup %source4name%? (y/n)
SET /P source5backup=Do you want to backup %source5name%? (y/n)
GOTO :eof

:setVariablesUnattended
REM This section is run when the user opts to perform a full backup, capturing all directories.
REM By default, everything is marked “y” to capture in the backup.

SET source1backup=y
SET source2backup=y
SET source3backup=y
SET source4backup=y
SET source5backup=y
GOTO :eof

REM The rest of the file should not need modification.

:disclaimer
ECHO(
ECHO NOTE:  You must manually edit this script to specify backup sources
ECHO before using it!  Use CTRL+C to abort if you have not.
ECHO(
ECHO This script will perform a portable offsite backup of essential
ECHO network files of a source server using ROBOCOPY, now part of the
ECHO Microsoft Windows operating system.
ECHO(
ECHO It should be noted that these are not image backups, just a
ECHO collection of data files and unreadable/locked files will be
ECHO skipped.
ECHO(
ECHO Instructions:
ECHO(
ECHO Place this batch script in the location you would like the backup
ECHO files to be copied.  Ensure that the backup device holding this
ECHO script is plugged into a computer with access to the source server,
ECHO and the script is executed with administrative level privileges.
ECHO(
PAUSE
GOTO :eof

:menu
ECHO(
SET /P fullBackup=Do you want to select which directories to backup? (y/n)
IF /I “%fullBackup%”==”y” GOTO menuYes
IF /I “%fullBackup%”==”n” GOTO menuNo

ECHO(
ECHO A bad option was selected.  Script will terminate.
PAUSE
GOTO :eof

:menuYes
CALL :setVariablesAttended

ECHO(
SET /P beginBackup=Are you ready to begin the backup? (y/n)
IF /I “%beginBackup%”==”y” GOTO backupFiles
IF /I “%beginBackup%”==”n” SET /P abortBackup=Do you wish to abort? (y/n)
IF /I “%abortBackup%”==”y” GOTO :eof
IF /I “%abortBackup%”==”n” GOTO menuYes

ECHO(
ECHO A bad option was selected.  Script will terminate.
PAUSE
GOTO :eof

:menuNo
CALL :setVariablesUnattended
CALL :backupFiles
GOTO :eof

:backupFiles
CALL :userLocations

ECHO(
ECHO The backup script finished.
PAUSE
GOTO :eof

REM End of script.

Advertisements

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