Quick Links

When you have a bunch of end user computers on a network, it is important to be sure files stored locally on the respective computers are backed up in the event of hard drive failure. Maintaining backup programs, configurations and, possibly, support costs on each machine can be a real pain, so as an alternative we have a simple solution: a script which, when run, mirrors local data to a common network location.

How it Works

Both the setup and mirror process are very simple and goes like this:

  1. Create and share a folder on your network where you would like the user files stored. Users will need read and write access to this folder.
  2. Place the backup script in the network share.
  3. Schedule a task on each user’s computer to run the backup script from the network share.
  4. The script mirrors the documents stored on the local machine to the network share.

The script utilizes the Microsoft RoboCopy tool and automatically creates the “/Computer Name/User Name” folder structure inside the network folder so user documents will not overwrite each other.

The Script

@ECHO OFF
    

TITLE Local Documents Backup

ECHO Local Documents Backup

ECHO Written by: Jason Faulkner

ECHO SysadminGeek.com

ECHO.

ECHO.

SETLOCAL EnableExtensions

REM RoboCopy.exe must be present on the client machine in a folder specified in the PATH variable.

REM For Windows Vista and later have this tool included, but Windows XP and earlier should download

REM this tool from Microsoft and place it in the Windows folder of their machine.

REM Root folder where backup files should be stored.

REM To use the directory containing this script, use: %~dp0

SET BackupDir=%~dp0

REM Message to users:

ECHO.

ECHO Running Local Documents Backup.

ECHO.

ECHO Do not close this window, it will close automatically when finished.

ECHO You can safely minimize this window and continue working.

ECHO.

ECHO.

REM Destination= Specified Backup FolderComputer NameWindows User Name

REM Create required folders if they do not exist

SET BackupDir=%BackupDir%%ComputerName%

SET BackupDir=%BackupDir:\=%

IF NOT EXIST "%BackupDir%" MKDIR "%BackupDir%"

SET BackupDir=%BackupDir%%UserName%

IF NOT EXIST "%BackupDir%" MKDIR "%BackupDir%"

SET LogFile="%BackupDir%%ComputerName%-%UserName%_BackupLog.txt"

ECHO %ComputerName% Backup Starting > %LogFile%

ECHO. >> %LogFile%

ECHO.

REM Call BackupDirectory with a relative folder name in the %UserProfile% directory

REM To view available directories, run this from the command prompt:

REM DIR %UserProfile%

REM Add more directories as needed (i.e. "Downloads", "Favorites", etc.)

ECHO Backing up Documents...

REM Windows Vista / 7

CALL :BackupDirectory "Documents" >> %LogFile%

REM Windows 2000 / XP

CALL :BackupDirectory "My Documents" >> %LogFile%

ECHO Finished

ECHO.

ECHO Backing up Desktop...

CALL :BackupDirectory "Desktop" >> %LogFile%

ECHO Finished

ECHO.

ECHO.

ECHO.

GOTO End

ENDLOCAL

:BackupDirectory

ECHO.

REM Only backup this directory if it exists on the client machine

SET Source="%UserProfile%%~1"

IF NOT EXIST %Source% GOTO End

SET Dest="%BackupDir%%~1"

IF NOT EXIST %Dest% MKDIR %Dest%

REM Run the mirror copy:

RoboCopy %Source% %Dest% /V /S /E /COPY:DAT /PURGE /MIR /NP /R:1 /W:30

ECHO.

ECHO.

GOTO End

:End

 

Scheduling the Backup Script on User Machines

Once the network share and batch script are in place, setting up a simple Scheduled Task is all you have to do on each client machine. This Scheduled Task which runs the script only requires a few special options which we will point out here.

Because the script’s source and destination folders for the mirror process are driven by the Windows User Account, you will need to make sure the Scheduled Task runs under the respective user’s Windows login.

If you have multiple users who utilize the same machine, you will need to set up a Scheduled Task for each user so the script. In situations like this, you may want to select the option to only have the script run when the user is logged in.

Schedule the task to run whenever appropriate. You might want to consider having the process repeat several times throughout the day so changes are mirrored with the server often.

image

The program/script is the batch script stored on the network.

image

Since the backup can only run if a network connection is available, you can configure this option. This is optional because if the connection is not available the task will not run anyway because it cannot find the target program/script. Additionally, if you have the Scheduled Task set to run at a time when the computer may not be in use and/or asleep, select the option to wake the computer in order to run the task.

image

Configure advanced options as needed. One option of interest is “Run task as soon as possible after a scheduled start is missed” which will ensure backup intervals are not completely skipped in the event the machine is turned off.

image

The Result

As we mentioned earlier, the first time the mirror process runs the folder structure is created and all documents from the configured source folders are copied to the network. Needless to say, the copy can take a while depending on the amount of data. Subsequent executions of the Schedule Task will complete much quicker as files from the local machine are added, updated and deleted as needed to keep the mirrored structure.

image

Download BackupFiles Script from SysadminGeek.com

Download Windows Server 2003 Toolkit (which includes RoboCopy.exe) from Microsoft