The destination directory could be anything - another folder on the same PC, a removable disk, a mapped share or even a straight UNC path e.g. \\server\share - flexibility is the key for this script, once the basic variables are right and you've decided to use robocopy or xcopy then off you go.
So to the script:
Open notepad and put this info in - note where things are comments and what variables you'll have to change:
Script start:
echo on
REM Set up some time variables
for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%l-%%k-%%j
for /f "Tokens=1" %%i in ('time /t') do set tm=-%%i
set tm=%tm::=-%
set dtt=%dt%%tm%
REM set up variables for log files, source and destination - change this variable
set log="C:\Users\owner\Documents\Scripts\Logs\%dt%.log"
REM local stuff to be backed up - change this variable
set src="c:\documents"
REM remote location to put backups - change this variable
set dest="I:\backups\server"
REM now for the actual work - change switches as required - explanation of switches is below.
robocopy %src% %dest% /E /Z /MIR /R:1 /LOG:%log%
REM I'd like to know how it went (this file can be big if there are a lot of files copied)
echo Backup Logs attached | blat - -subject "Sync Log Report for %dt%" -to "me@mydomain.com" -attach %log% -f user@domain.com
Use blat to send the email - grab it from www.blat.net (great program!) It sends an email with a header that will look like this:
Sync Log Report for 2012-02-10and an attachment of your log file. You can add different things to this - for example I'll often use a [servername] tag after the date.
The robocopy switches used are:
- /E = copy sub-directories, including empty ones
- /Z = copy files in restartable mode (in case the network drops out or something similar)
- /MIR = MIRror a directory tree (which is /E plus /PURGE)
- /R:1 = number of retries on failed copies. It's best to set this - by default it's 1 million (!)
I run this from the Windows Scheduler and have a mirrored copy of data files each night. It's quite a useful little tool. If you'd like to use xcopy instead there are a few things to consider:
- the src and dest variables need to have a trailing backslash and a wildcard
- set src="c:\documents\*"
- set dest="i:\backups\server\*"
- and the command to insert would be:
- xcopy %src% %dest% /C /D /E /H /Y > %log%
- where the switches are:
- /C = continue copying even if there are errors
- /D = copies files whose source is newer
- /E = copies directories and sub-directories (even if empty)
- /H = copies hidden and system files
- /Y = suppresses prompting to overwrite files
- the > redirects xcopy's output to the %log% variable we configured earlier in the script, and then blat will email the resulting file out.
If you find this useful in anyway, please let me know in the comments.