Backup Using Batch Files
From Hak5
Sometimes it is useful or even necessary to simply copy existing directories to another hard disk or network drive rather than using more complicated backup methods. Multiple directories can be backed up comparatively easy with a simple click by creating and running a batch file. That file can be executed manually from your desktop, can be added to startup or scheduled for periodic execution as needed.
Batch files have comparatively easy syntax and can have many uses so this method could also be a good learning experience by example. You can simply copy the text below, and paste it into Notepad. Create a new file with either .bat or .cmd extension, rather than as a .txt file.
Here is a working example of a backup script you can modify for your needs:
@echo off :: variables set drive=g:\Backup set backupcmd=xcopy /s /c /d /e /h /i /r /k /y echo ### Backing up My Documents... %backupcmd% "%USERPROFILE%\My Documents" "%drive%\My Documents" echo ### Backing up Favorites... %backupcmd% "%USERPROFILE%\Favorites" "%drive%\Favorites" echo ### Backing up email and address book (Outlook Express)... %backupcmd% "%USERPROFILE%\Application Data\Microsoft\Address Book" "%drive%\Address Book" %backupcmd% "%USERPROFILE%\Local Settings\Application Data\Identities" "%drive%\Outlook Express" echo ### Backing up email and contacts (MS Outlook)... %backupcmd% "%USERPROFILE%\Local Settings\Application Data\Microsoft\Outlook" "%drive%\Outlook" echo ### Backing up the Registry... if not exist "%drive%\Registry" mkdir "%drive%\Registry" if exist "%drive%\Registry\regbackup.reg" del "%drive%\Registry\regbackup.reg" regedit /e "%drive%\Registry\regbackup.reg" :: use below syntax to backup other directories... :: %backupcmd% "...source directory..." "%drive%\...destination dir..." echo Backup Complete! @pause
The above example backs up "My Documents", Favorites, Outlook Express email/address book, (all for the current user) and the Windows Registry. It copies the files to the directory defined in the %drive% variable, or "g:\Backup". If the script is run multiple times, it will only rewrite if the source files are newer. It will create subdirectories as necessary, and it will retain file attributes. It can copy system and hidden files.
In the above file, all lines that begin with "::" are comments. The "set drive=" and "set backupcmd=" near the top define two variables (referenced by %drive% and %backupcmd%), used a number of times throughout the file; the first being the location of the top directory where we want to backup, and the second the actual copy command with all necessary switches. All the "echo " lines in the file simpy output the line of text to the screen, and the lines beginning with %backupcmd% are the actual commands to execute.
Note that most of the folders in the above backup example are subdirectories of the %USERPROFILE%... It is possible to simply backup the entire user profile with My Documents, Favorites, Outlook Express, Outlook, etc. by backing up this one folder. Here is an example (it assumes the above "drive" and "backupcmd" variables are set):
%backupcmd% "%USERPROFILE%" "%drive%\%UserName% - profile"
Contents |
Backing up Other Directories and networked PCs
You can backup other directories by simply creating more alike lines:
%backupcmd% "...source dir..." "%drive%\...destination dir..."
For example, if you'd like to backup "C:\Program Files\Microsoft Office" to our destination "G:\Backup\MS Office" (and retain the directory structure) you'd need to add the following line to the batch file:
%backupcmd% "C:\Program Files\Microsoft Office" "%drive%\MS Office"
Here is another example, backing up the Administrator Profile on a machine on the LAN with computer name "Lianli":
%backupcmd% "\\Lianli\c\Documents and Settings\Administrator" "%drive%\Lianli - admin profile"
Remember, you have to save the batch file with either .bat or .cmd extension, then just double-click to execute it.
Using the Current Date
Sometimes it is useful to create folders with the date incorporated in the folder name. Here is how to set the variable folder to the current date (assuming US system date format):
set folder=%date:~10,4%_%date:~4,2%_%date:~7,2% %backupcmd% "...source dir..." "%drive%\%folder%\...destination dir..."
It is also possible to use the current time in the folder name. The following example with incorporate both the current date and time to the minute, separated by underscores. There is an extra step that cleans up possible spaces in single-digit hours in the system time:
set hour=%time:~0,2% if "%hour:~0,1%"==" " set hour=0%time:~1,1% set folder=%date:~10,4%_%date:~4,2%_%date:~7,2%_%hour%_%time:~3,2% %backupcmd% "...source dir..." "%drive%\%folder%\...destination dir..."
Example - dated directories
In the example below, we first set 3 variables: drive, folder, and backupcmd. The "drive" variable defines the root directory of our backups. The "folder" takes the 2 digit day value from the current date (US date format, taking 2 digits from the date command output, starting at the 7th character), which we will use as a subdirectory. The third variable, "backupcmd" defines our backup command with the appropriate command line switches we want to use.
@echo off :: variables set drive=D:\Backup set folder=%date:~7,2% set backupcmd=xcopy /s /c /d /e /h /i /r /k /y echo ### Backing up directory... %backupcmd% "C:\Program Files\somedirectory" "%drive%\%folder%" echo Backup Complete! @pause
This example will backup the "C:\Program Files\somedirectory" folder to "D:\Backup\[dd]" where [dd] is the current day of the month. After a month, we will have 30ish daily copies of the backup... And, because of the xcopy command line switches chosen, following backups will only overwrite files that are newer, speeding up subsequent backups. Alternatively you can add a line to delete the %folder% directory prior to executing the %backupcmd% if you prefer to start clean (and take longer).
Notes:
- Any batch file can be interrupted with CTRL+C or CTRL+Break if needed.
- Instead of backing up My Documents, Favorites, Outlook and Outlook Express files separately, you can combine it all into one line for the current user: %backupcmd% "%USERPROFILE%" "%drive%\%UserName% - profile" . The only disadvantage being that it would save temporary IE files as well (however the default location of those can be changed).
- The Registry backup in the above example works well only for partial registry restores, it does not save the complete system state. Read this FAQ for more info.
Issues with xcopy between file systems
When performing incremental backups, xcopy compares the timestamp of files in the source directory to the timestamp of existing files in the destination directory to determine whether the file has changed and needs to be re-copied. This doesn't always work as expected when copying between different file systems that record timestamps to differing granularity. For example, NTFS records timestamps to 0.1 seconds, but FAT timetamps are only accurate to within 2 seconds. The result is that if you backup to another computer on your network or NAS drive that uses a filesystem with a different timestamp format, you may end up recopying files that already exist because the timestamps are slightly different. There doesn't appear to be a solution to this using xcopy, but alternative utilites have command line options to allow a "fuzzy" timestamp comparison so that if the source and destination timestamps are within a couple of seconds of each other they are treated as being the same: With xxcopy use the /ff option or with robocopy use /fft. For example, with robocopy, the backupcmd line from the examples above would become:
set backupcmd=robocopy /mir /z /fft
--gointern 07:36, 7 Aug 2006 (PDT)


