Sometimes you need a quick and simple way to backup a user directory to a network drive/NAS share or external drive.

Here’s a batch file that will backup using Robocopy the subdirectories of your user directory to the location of your choice.

Note that this is more like a folder mirroring script than a backup as the source and destination will be kept in sync each time you run it. Robocopy deletes files in the destination if they’ve been deleted from the source so that the folders remain in sync.

The script creates log files in the destination folder and can email the logs to you if an error occurs during the backup. Emailing requires an additional command line emailer, such as GBmailer.

This has been used for many years without issues but as with anything you find on a random site on the Internet, take time to understand what this script does, and use it at your own risk.

@echo off
setlocal

:: www.techexplorer.co.uk
::
:: A batch file to copy (mirror) the contents of the current user's home
:: directory to a network drive/NAS share/mounted external disk. This is
:: intended for use where you need an identical copy of the user directory
:: which can be kept in sync rather than a more traditional backup.
::
:: The first run will be a full backup, subsequent runs will incrementally 
:: mirror the contents of the destination to match the source (meaning that
:: files deleted from the source, will also be deleted from the backup
:: destination).
::
:: Edit this path to match your backup location, ensuring you set appropriate
:: permissions if multiple users back up to sub-directories of the same share
set BackupPath=\\server\share\%username%\backup
set LogRoot=%BackupPath%\_BackupLogs
::
:: For email notifications, ensure you download GBmailer and update the path
:: below with the correct location. If the email application specified below
:: is not found, the email sending line will not run so you'll need to check
:: the log files manually.
:: https://sourceforge.net/projects/gbmailer/files/
::
:: Alternatively, you can edit the script and parameters to work with any
:: other command line email application
::
set Mailer="c:\scripts\gbmail\gbmail.exe"
set Mailserver=mailserver.yourdomain.co.uk
set EmailTo=user@yourdomain.co.uk
set EmailFrom=%computername%@yourdomain.co.uk
::
:: ############################################################################

Title Backing up - please don't close this window

:: ensure the directory exists by attempting to create it
md %BackupPath% >nul 2>&1
md %LogRoot% >nul 2>&1

:: set Log file location and filename
set Log=%LogRoot%\BackupToDisk.Log
set LogSummary=%LogRoot%\BackupToDisk_summary.Log

:: clear any existing Log entries
echo.>%Log%
echo.>%LogSummary%

:: Do the backup
:: Add all folders within the user directory here to ensure they get backed up
FOR %%I IN (Documents,Pictures,Music,Downloads,Desktop,Favorites,Links,Searches,Videos) DO (
call :routine %%I
)


:: check robocopy Log files for error messages
echo.>>%LogSummary%
findstr /c:"ERROR " %Log% >>%LogSummary%
if errorlevel 1 (
	echo.>>%LogSummary%
	echo.>>%LogSummary%
	echo Finished>>%LogSummary%
     echo No errors found>>%LogSummary%
) else if errorlevel 0 (
	echo.>>%LogSummary%
	echo.>>%LogSummary%
     echo WARNING: errors found in log file>>%LogSummary%
     echo.>>%LogSummary%
	REM send an email if the email application exists
     if exist %Mailer% (%Mailer% -to %EmailTo% -file %LogSummary% -h %Mailserver% -from %EmailFrom% -s "Error during backup on %COMPUTERNAME%")
)


:: the backup routine is repeated for each folder specified above
:routine
if [%1]==[] goto :eof

set BackupSource="C:\Users\%username%\%1"
set BackupDestination="%BackupPath%\%1"


echo.
echo  Backing up %1 to disk
:: Some temporary files are exluded here
robocopy %BackupSource% %BackupDestination% /MIR /NP /DCOPY:T /R:1 /W:5 /XJD /XF ntuser.dat* UsrClass.dat* *.tmp ~wri *.jar *.$$$ *.par *.cdx *.VSP ~*.xlsx ~*.docx /XD "%userprofile%\AppData\Local\Temporary Internet Files" "%userprofile%\AppData\Local\Google" /Log+:%Log%

if errorlevel 16 (
     echo backup %BackupSource% to disk>>%LogSummary%
     echo ERROR: ROBOCOPY terminates with ***FATAL ERROR***>>%LogSummary%
     echo.>>%LogSummary%
) else if errorlevel 8 (
     echo backup %BackupSource% to disk>>%LogSummary%
     echo ERROR: ROBOCOPY terminates with **FAILED COPIES**>>%LogSummary%
     echo.>>%LogSummary%
) else if errorlevel 4 (
     echo backup %BackupSource% to disk>>%LogSummary%
     echo ERROR: ROBOCOPY terminates with    *MISMATCHES*>>%LogSummary%
     echo.>>%LogSummary%
) else if errorlevel 1 (
     echo backup %BackupSource% to disk completed successfully>>%LogSummary%
     echo.>>%LogSummary%
) else if errorlevel 0 (
     echo backup %BackupSource% to disk completed successfully - no changes>>%LogSummary%
     echo.>>%LogSummary%
)

attrib /S /D -H -S %BackupDestination%

echo.>>%LogSummary%
echo.>>%LogSummary%

endlocal

goto :eof