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 using batch commands.

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

Some lines display as 2 lines but must be typed as one. Test carefully after eash PAUSE command. Only remove the PAUSE commands after the batch program is thoroughly tested. The log file will show which files were checked and which were actually copied.

@echo off
ECHO BackupMyFiles.bat - using Robocopy

REM____TODAY'S DATE____
for /F "delims=/ " %%i in ('date /t') do (set Date=%%i)
REM____LOG FILE LOCATION____
set LOG=F:\Backups\Logs\%Date%.log

REM____USER FOLDERS LOCATION____
set USER=C:\Users\_LOGIN_NAME_

REM____BACKUP LOCATION____
set DEST="F:\Backups"

REM____PROGRAM LOCATION (check)____
set APP=C:\Windows\System32\Robocopy.exe

REM____COPY OPTIONS (one line)____
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____MIRROR OPTIONS____
set MIRROR=/MIR /R:0 /XA:H /MT /NS /NC /XJ /LOG+:%log%

REM____START/APPEND LOG FILE (one line)____
ECHO ##### ROBOCOPY Backup to %DEST% on %Date%  ##### >> %LOG%
ECHO. >> %LOG%

REM____BACKUP USER FOLDERS except AppData____
%APP% %USER% %DEST%\UserData %OPT% /XD AppData
PAUSE

REM____BACKUP OTHER FOLDERS____
REM____Insert commands for your folders here:
REM %APP% "D:\My Files" "%DEST%\My Files" %OPT%
REM %APP% "G:\Photos" %DEST%\Photos %OPT%
PAUSE

REM____MIRROR Start Menu (one line)____
%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