PDQ Library:  Personal backups using ROBOCOPY (Win7)

You must already know how to use batch commands to use this page. I accept no responsibility for any accidents you have, as a bad command can quickly delete important files.

For daily backups of personal files, I use a batch program (command files). For important files, I use the free Dropbox file service to make a copy in the cloud as well as making them available on all my devices. (My network drive's backup software creates constant overhead on my network every time there is a change in my files.) A Windows-7 program called ROBOCOPY is designed to copy files from a specified source folders and overwrite them in the destination folder if the time stamp or file size is different. If you put Robocopy commands in a Batch Program, you can run it whenver you wish, or create a 'task' using the Windows Task Scheduler to run it at the same time every day.

Creating your backup program:
The template below needs to be edited and should be tested carefully before you run it to backup to a drive with valuable data! Start by copying the text below into a text editor. Save it with a name like BackupMyFiles.bat. Modify the commands for your system. For example, change _LOGIN_NAME_ to match your login name. Edit lines that start with Set to match paths in your hard drives. Surround all paths that contains a space with quotes. If your destination is a network drive use the format \\NASdrive\Sarah\Backups - mapped drive letters will not work.

Using a dated Log File:
The log file name contains today's date so each day has a unique log file. All entries logged for today are appended to that file (because the first line sent to the log file uses a double >>). Log files list all file comparisons, even those that do not result in a copy. It also contains a summary of each Robocopy command. The log can be very long - the reason for sending screen output to a log file!

Testing:
Before you run your Batch program for the first time, add the /L option to the options list (OPT=). This will simply send operations to the log file without actually doing anything. Search for lines with "100%" which follow every file copied. Search for "ERROR" to find error messages. If that looks good, remove the /L option.

Robocopy is complicated to configure properly, and a single mistake can delete files in an instant. The "PAUSE" commands will stop the program after each copy operation so you can check the log file and also make sure files are being copied to the correct folder. Once the program is running perfectly, you may remove the pause commands.

Template

@echo off
ECHO BackupMyFiles.bat - using Robocopy

REM_____Today's Date (yy-mm-dd); Log file location
for /F "delims=/ " %%i in ('date /t') do (set Date=%%i)
set LOG=F:\Backups\Logs\%Date%.log

REM_____Location of User folders
set USER=C:\Users\_LOGIN_NAME_

REM_____Location for Backup folders
set DEST=F:\Backups
REM_____set DEST=\\NETWORK_DRIVE\SHARE_NAME\Backups
REM_____Location of program
set APP=C:\Windows\System32\Robocopy.exe

REM_____Options to copy files
set OPT=/COPY:DT /E /FFT /FP /R:0 /XA:H /MT /NC /NP /NS /XA:H /XJ /S /W:3 /XF *.tmp /XD old /LOG+:%log%

REM_____Options to mirror folder
set MIRROR=/MIR /R:0 /XA:H /MT /NS /NC /XJ /LOG+:%log%

REM_____Start (or append to existing) dated log file
ECHO ROBOCOPY.BAT ______Backup folders to external drive______ %Date%   >> %LOG%
ECHO. >> %LOG%

REM_____Backup all User Folders except AppData
%APP% %USER% %DEST%\UserData %OPT% /XD AppData
PAUSE

REM_____Backup other User Folders
%APP% "G:\Saved Manuals" %DEST%\Save %OPT%
%APP% G:\Downloads %DEST%\Downloads %OPT%
PAUSE

REM_____MIRROR User Start Menus
%APP% "%USER%\AppData\Roaming\Microsoft\Windows\Start Menu" "%DEST%\Start Menu" %MIRROR%
PAUSE

ECHO. >> %LOG%
ECHO ================================================ >> %LOG%
ECHO. >> %LOG%

ECHO.
ECHO Done.
PAUSE
EXIT

ROBOCOPY SWITCHES

Open a command prompt (Start > Run > "cmd") and type the following command to view all Robocopy documentation (in pages):

robocopy /? | more

Here are some Robocopy switches with their explanations.
/COPY:DT Don't copy file Attributes (prevents ERROR 5)
/E copy subfolders incl empty ones
/FFT assume FAT File Times (2-second granularity)
/FPFull path in log
/L List only. Don't copy, timestamp or delete files.
/LOG+:{file}Append to log file
/M copy only files with the Archive attribute; reset it. **
/MIRMirror mode, delete destination files not present in source
/MT[:n]multi-threaded copies with n threads (default 8).
/NCdon't log file classes.
/NDLNo directory list – do not log directory names.
/NFLNo file list – don't log file names.
/NPNo progress – don't display % copied.
/NSdon't log file sizes
/PURGEDeletes files no longer in the source location.
/R:0zero Retries on failed copies
/S copy subfolders but not empty ones
/V Produce verbose output, showing skipped files.
/X Report all extra files, not just those selected.
/XA:HDo not copy HIDDEN files
/XD dirspecExcludes folders. (BAK* skips folders starting with "BAK"
/XJDon't follow junctions

** The option /M should save time by only backing up new or updated files in the source folder rather than comparing all the files in the source to the destination folders across the network. I have not tested this theory, but I use this option in my own backup script.

To learn more about Batch Programs, open the Windows Help System (Start > Help or F1 key) and type this: What is batch command. Select any items about command prompt.

Hint: If your Start Menu does not contain a Run command, right click on the taskbar (bottom of screen) and select Properties. Click the Start Menu tab, then Customize. Select the item to display the Run command.

TOP back