martes, 14 de junio de 2011

Backup.cmd - Simple scripts for Backup in Windows using MSDOS

For the easiest use and development, the backup should take all directories of the disk and then try to backup their contents. Depending of the backup type, it will backup all information or only those files updated since last backup.

In Windows is easy to know which files were updated, it just take us to review the file’s attributes and find if it has the Archive file attribute active. For example, the attributes for the file autoexec.bat

C:\>attrib autoexec.bat
A C:\autoexec.bat

Once we have backed up a file we will need to reset the Archive attribute, but, as I mention earlier, the WinRAR tool help us in the process. If we set the proper attributes to WinRAR, it will compress only files ready to archive and at the end it will also reset that attribute.

At the end, the backup script needs to set the proper parameters and then call WinRAR to do the job. Part of the required setting is to store each backup set in a separate directory to avoid overwriting the previous one.

Taking a look to the script we have that, after setting the variables, we check the parameters to determine whether it is a full o incremental type of backup.

if NOT "%1"=="" set full=1

Also, we need to create a proper backup dir according with the current date

set backupExt=bkp
set backupRoot=%backupDrive%Backup00.%backupExt%
set backupDir=%backupRoot%\%date:~6,4%%date:~3,2%%date:~0,2%.%backupExt%

if NOT exist "%backupRoot%" md "%backupRoot%"
if NOT exist "%backupDir%" md "%backupDir%"

backupDir will hold all backup files.

To do the process we need to get all directories, the dir command gives us that information. We just need to use it in a FOR loop

FOR /F "usebackq delims==" %%i IN (`dir /b/ad-h-s`) do (

But if we are processing the same drive where the backups are stored we need to avoid creating a backup of the backup files. This validation in an easy task because the main backup directory has a fixes name.

set localFile=%%i
set localextension=!localFile:~9!

if Not "!localextension!" == "%backupExt%" (

Remember the configuration file? Well, we need to validate if the current directory is not in the exceptions. This is made comparing the current directory against the values in the exceptions variable; again, this is made with a loop, extracting the comma separated values from the variable. As we need to have a limit in the search, we can evaluate as much as 100 exceptions.

rem validate is not an exception
set localExceptions=
set nextExceptions=%exceptions%
rem up to 100 exceptions
set maxExceptions=100
FOR /L %%e IN (0,1,!maxExceptions!) DO (
set localExceptions=!nextExceptions!
for /f "tokens=1,* delims=, " %%a in ("!localExceptions!") do (
set currentException=%%a
set nextExceptions=%%b
)
if /I "%%i"=="!currentException!" set processDirectory=0
if NOT "!nextExceptions!"=="" set maxExceptions=1
)

The script use the processDirectory variable as a switch to know if the directory will be processed or not.

Once we decide to process a directory, we create a list of files of it, this fill will be used later in a restore process.

dir /oen/b/s/a-d "%%i" > "%backupDir%\%%i.lst"

At the beginning of the script we set a variable to know if it was a full backup, now we will use it. An IF statement using the variable full help us to set the proper parameters for WinRAR.

if "%full%"=="0" (
"%rarPath%\rar.exe" a -ac -ao -r -m0 -v%maxRARSize%k "%backupDir%\%%i.rar" "%%i" > "%backupDir%\%%i.log"
) else (
"%rarPath%\rar.exe" a -ac -r -m0 -v%maxRARSize%k "%backupDir%\%%i.rar" "%%i" > "%backupDir%\%%i.log"
)

Basically the main logic is to select which directories to backup and to set the backup destination directory. Al the other job is made by WinRAR.

The WinRAR parameters used are:

C:\Program Files\WinRAR>rar

RAR 3.51 Copyright (c) 1993-2005 Alexander Roshal 7 Oct 2005
Shareware version Type RAR -? for help

Usage: rar <command> -<switch 1> -<switch N> <archive> <files...>
<@listfiles...> <path_to_extract\>

<Commands>
a Add files to archive

<Switches>
ac Clear Archive attribute after compression or extraction
ao Add files with Archive attribute set
r Recurse subdirectories
m<0..5> Set compression level (0-store...3-default...5-maximal)
v[k,b] Create volumes with size=*1000 [*1024, *1]

The compression used os only to store the files. This makes our backup faster but it use more space. Changing that switch will save space but with slower backups.

The way to use the script will be as follows:

For a full backup:
D:\>Backup.cmd full

For a incremental backup:
D:\>Backup.cmd

With the default configuration, you will get the next directory(for the date of Jun 6th, 2011):
D:\Backup00.bkp\20110616.bkp

Within that directory you will find LST files containing the list of all files per directory, LOG files containing the operation results per directory and the RAR files containing the backup files.

No hay comentarios: