Backup script for Robocopy and external hard disk drives


If you have data that you value on external disk drives you have a couple of options for backup.  One is to Raid the drives and allow the Raid architecture chosen to take care of the backup.  Another method is to ‘roll your own’ backup strategy.  This does bring with it all of the pitfalls of not running the backups, not verifying that the backups are working, or letting the backups get out of date.

With these caveats noted, one benefit is the ability to incrementally add storage when it is needed at low cost – rather than having to matching it to the Raid architecture or the enclosure you have selected.  Bare internal HDDs are the cheapest way to buy storage – and simple USB docks can be purchased for these for £20 ($30) or less.

What size the partition?

Partitioning a source drive can help to fit backups on to target backup drives.  For source drives a size of 465 GB will allow drives to be partitioned with minimal lost space (e.g. 3 TB will have six 465 GB partitions plus change.)  It also means you do not need to synchronize the size of your source and backup drives.  The partition is the unit of backup – six 465 GB partitions can be spread over three 1 TB backup drives, or two 2 TB drives leaving 1 TB spare.  465 GB is also a reasonable size to recover (as a secondary data recovery route.)  Backup drives do not need to be partitioned and can be a single large volume.  If you are recovering a backup drive something has gone seriously wrong with your backup strategy.

How to backup?

The follow script provides a method and is provided as-is, try and test it yourself – I accept no responsiblity for any data loss.

It works on a data disk with the label “Disk1.Partition1” and a folder “Data”.  It expects to write to a disk with label “BackupDisk.1” and into a folder “Mirrors\Disk1.Partition1\Data”.

The two sections that use WMIC map from the label you provide to the path where the drive is installed (if it is plugged in and switched on.)  Removing the problem of drives getting mapped to different drive letters between backup sessions.

If the source and target (backup) drives are found, then it constructs the source and target full path names and echos these to the output.

Backup is done in two phases to avoid disk size issues.  If data has been both added and removed from the source, but the folder walking encounters the added data first – then a single step use of robocopy may attempt to mirror more data than the backup drive has space for.

The /PURGE line will remove all data in the backup folder that does not currently exist in the source folder.  Freeing the space for added data.

The /XJ parameter skips NTFS junction points.

The second robocopy invocation with the /MIR performs the backup – copying all new or changed data from the source to the target.  This process can be interrupted and resumed (by re-running it) without loosing (much) progress.

The script is coded to be executed inside a batch file.  If you place batch file inside the folder you are backup up (e.g. as backMeUp.bat), then the backup process becomes a matter or connecting the source and backup disks to a system, then double-clicking the backMeUp.bat file.

For more on robocopy, Google it.

A final disclaimer in case you missed the one above: this script is provided as-is, try and test it yourself – I accept no responsiblity for any data loss.

@echo off

setlocal enabledelayedexpansion

:: Change these four lines to match your source and backup locations
@set sourceLabel=Disk1.Partition1
@set sourceSubPath=Data
@set targetLabel=BackupDisk.1
@set targetSubPath=Mirrors\Disk1.Partition1\Data

FOR /F "skip=1 tokens=1 delims= " %%A IN ('WMIC volume WHERE Label^="%sourceLabel%" Get DriveLetter^,Label /Format:table') DO (
       IF %%A GTR 0 (
	Set sourceDrive=%%A
       )
    )

FOR /F "skip=1 tokens=1 delims= " %%A IN ('WMIC volume WHERE Label^="%targetLabel%" Get DriveLetter^,Label /Format:table') DO (
       IF %%A GTR 0 (
	Set targetDrive=%%A
       )
    )

if "%sourceDrive%"=="" (
  echo Cannot find the SOURCE drive "%sourceLabel%" - please insert and retry
)

if "%targetDrive%"=="" (
  echo Cannot find the BACKUP drive "%targetLabel%" - please insert and retry
)

if not "%sourceDrive%"=="" (
 if not "%targetDrive%"=="" (

  set newsourcePath=%sourceDrive%\%sourceSubPath%
  set newtargetPath=%targetDrive%\%targetSubPath%

  @echo source Path is !newsourcePath!
  @echo target Path is !newtargetPath!

  echo on

  robocopy !newsourcePath! !newtargetPath! /PURGE /E /NOCOPY /XJ

  robocopy !newsourcePath! !newtargetPath! /MIR /XJ

  pause

  GOTO:EOF 

) )

@echo on

pause
Advertisements

One Response to Backup script for Robocopy and external hard disk drives

  1. sjhunt says:

    @echo off

    setlocal enabledelayedexpansion

    @set sourceLabel=
    @set sourceSubPath=
    @set targetLabel=
    @set targetSubPath=

    :: E.g. sourceLabel=A.1
    :: E.g. sourceSubPath=ZXM\TestFolder
    :: E.g. targetLabel=BackupDisk
    :: E.g. targetSubPath=A.1.Backup

    FOR /F “skip=1 tokens=1 delims= ” %%A IN (‘WMIC volume WHERE Label^=”%sourceLabel%” Get DriveLetter^,Label /Format:table’) DO (
    IF %%A GTR 0 (
    Set sourceDrive=%%A
    )
    )

    FOR /F “skip=1 tokens=1 delims= ” %%A IN (‘WMIC volume WHERE Label^=”%targetLabel%” Get DriveLetter^,Label /Format:table’) DO (
    IF %%A GTR 0 (
    Set targetDrive=%%A
    )
    )

    if “%sourceDrive%”==”” (
    echo Cannot find the SOURCE drive “%sourceLabel%” – please insert and retry
    )

    if “%targetDrive%”==”” (
    echo Cannot find the BACKUP drive “%targetLabel%” – please insert and retry
    )

    if not “%sourceDrive%”==”” (
    if not “%targetDrive%”==”” (

    set newsourcePath=%sourceDrive%\%sourceSubPath%
    set newtargetPath=%targetDrive%\%targetSubPath%

    @echo source Path is !newsourcePath!
    @echo target Path is !newtargetPath!

    pause

    echo on

    robocopy !newsourcePath! !newtargetPath! /PURGE /E /NOCOPY /XJ

    robocopy !newsourcePath! !newtargetPath! /MIR /XJ

    pause

    GOTO:EOF

    ) )

    @echo on

    pause

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s

%d bloggers like this: