Robocopy - simple little backup script
June 27, 2009I’ve been asked in the past to help out people (read family/friends, generally “love jobs”) with backing up stuff. Often there isn’t very much that needs to be backed up - what’s prompted this little script is a family member who just needs to backup about 4GB of stuff from a couple of folders.
Robocopy is something I’ve used in the past, and is a quick, easily scriptable little utility for moving stuff around in Windows. I’ve used it for all sorts of things, server migrations and stuff, for a number of reasons:
- it’s quick
- it’s simple
- it only copies the delta, and leaves stuff it’s already copied alone - this comes in particularly useful for when moving large quantities of files which are in use - you can “pre-stage” most of the stuff, and then it only copies what you need once you do the “final” copy.
Without further ado, here’s the script. It won’t be the best bit of scripting you’ve ever seen, but:
@echo off
REM Robocopy script
REM
REM This sets the main path for the backup. This is set a folder called backup in the current folde (ie. the USB key or wherever you're running this from)
set dest=%cd%\backup\
REM This sets the global options for the robocopy backups
set options=/E /ZB /COPYALL /MIR /R:3 /W:3 /V
REM each different folder you want to backup, and what folder you want it to be called in the destination.
REM comment out any that you don't need, and make sure the corresponding robocopy statement is also commented out.
REM ##########
set path=C:\Somefolder
set destfolder=Somefolder
robocopy %path% "%dest%%destfolder%" %options%
echo.
echo.
if errorlevel 16 echo ***FATAL ERROR*** Make sure all programs are closed and retry & echo. & echo. & pause & goto end
if errorlevel 8 echo **FAILED COPIES** Make sure all programs are closed and retry & echo. & echo. & pause & goto end
REM ##########
REM For each folder you want to backup, copy below
REM ##########
set path=C:\anotherfolder
set destfolder=anotherfolder
robocopy %path% "%dest%%destfolder%" %options%
echo.
echo.
if errorlevel 16 echo ***FATAL ERROR*** Make sure all programs are closed and retry & echo. & echo. & pause & goto end
if errorlevel 8 echo **FAILED COPIES** Make sure all programs are closed and retry & echo. & echo. & pause & goto end
REM ##########
:end
Basically, it sets as much of the “constant” stuff as it can for reuse with variables, and you just copy those chunks of code as required. There is basic error handling - if you run the batch and something goes wrong, it’ll stop and tell you.
Robocopy is available in the Windows resource kits. For more info, looky here. For download, go here.
Just make sure Robocopy.exe is on your USB key you’re running the script from, or in c:/windows/system32 (or somewhere else in your PATH variable)
I’m not going to document the whole thing - this is much for my reference as anyone else’s. Do some reading if you need to - the doco for Robocopy is very good, and there’s heaps of info out there.
