Google


Get this widget!










How to Backup using Batch Files

Sometimes it is useful or necessary to simply copy existing directories to another hard disk or network drive than using complicated backup methods. Multiple directories can be backed up easily with a simple click by creating a batch file. That file can be executed manually from your desktop, can be added to startup or scheduled for execution as needed.


Batch files basically easy and their syntax have many uses this method could also be use for learning experience by example. Simply copy the text below and paste it into Notepad. Create a new file with with .bat or .cmd extension..

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 /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, and the Windows Registry. It copies the files to the directory defined in the %drive% variable, or “g:\Backup”. If the script is ran 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”

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:

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…”

Hope that helps…


0 Responses to “How to Backup using Batch Files”

  1. No Comments

Leave a Reply







 Subscribe in a reader

Enter your email address:

Delivered by FeedBurner

WidgetBucks - Trend Watch - WidgetBucks.com

UserOnline