sábado, 18 de junio de 2011

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

The last option we need in order to have a [almost] complete backup tool is the full restore.

As part of the backup process there is a file with the list of all files per directory. We are going to use those files to restore all the newest files from the backup sets.

It might be the case that we need to restore the newest files but given an older list file (maybe because we delete some information we need to restore), in that case we set this variable.

set sourceFilesDir=<path where source LST files will be taken in a restore process>

if the variable is not set, the script looks for the newest backup set to be used as base for the LST files.

rem get newest lst files
if NOT exist "%sourceFilesDir%" (
FOR /F "usebackq delims==" %%i IN (`dir /oen/b/ad %backupDir%`) do (
set sourceFilesDir=%backupDir%\%%i
)
)

Of course, all this is after setting main variables.

Next thing is to retrieve all backup sets by date in descending order.

FOR /F "usebackq delims==" %%i IN (`dir /oe-n/b/ad %backupDir%`) do (

Most of the process is similar to previous restore scripts, with slight changes because we need to retrieve the file name in order to get it’s proper LST file and create the correct directory.

For example:

if /I "!fileName:~-10,5!"==".part" set process=0
...

if /I "!fileName:~-10,6!"==".part1" (
set process=1
set trimLen=10
)

We will later use those variables.

If the scripct receives a parameter, it means we need only to restore one directory, in that case, if the RAR file match the given parameter, we proceed with the process, otherwise we skip the file. The way to skip the file is using the process variable.

if NOT "%restoreOnly%"=="" (
if "!process!"=="1" (
set process=0

if "%restoreOnly%.rar"=="!fileName!" set process=1
if "%restoreOnly%.part1.rar"=="!fileName!" set process=1
if "%restoreOnly%.part01.rar"=="!fileName!" set process=1
if "%restoreOnly%.part001.rar"=="!fileName!" set process=1
if "%restoreOnly%.part0001.rar"=="!fileName!" set process=1
if "%restoreOnly%.part00001.rar"=="!fileName!" set process=1
)
)

If the file is suitable to be processed, we look for the proper LST file, then create an intermediary file, that’s because the original LST includes the Drive letter and we don’t need it to the restore.

rem decode original LST file
if /I "!trimLen!"=="4" set listFile=!fileName:~0,-4!.lst
if /I "!trimLen!"=="10" set listFile=!fileName:~0,-10!.lst
if /I "!trimLen!"=="11" set listFile=!fileName:~0,-11!.lst
if /I "!trimLen!"=="12" set listFile=!fileName:~0,-12!.lst
if /I "!trimLen!"=="13" set listFile=!fileName:~0,-13!.lst
if /I "!trimLen!"=="14" set listFile=!fileName:~0,-14!.lst

rem create the list of files to recover
if exist "%restoreDir%\RestoreFiles.lst" del "%restoreDir%\RestoreFiles.lst"
FOR /F "eol=; tokens=1 delims==" %%x in (%sourceFilesDir%\!listFile!) do (
set stringLine=%%x
@echo !stringLine:~3!>> "%restoreDir%\RestoreFiles.lst"
)

Now use the already set variables to extract all the required files.

"%rarPath%\rar.exe" x -o- -n@"%restoreDir%\RestoreFiles.lst" "%backupDir%\%%i\%%x" * %restoreDir%\ >> %restoreDir%\Restore.log

The used parameters are:

x = extract
-o- = do not overwrite
-n@<file> = extract only thise files listed in the given file
"%backupDir%\%%i\%%x" = RAR file
* = extract all (view -n@<file>)
%restoreDir%\ = targe dir

The usage is:
D:\>FullRestore.cmd

If we need only to recover one directory we use:
D:\>FullRestore.cmd temp

Where temp is the directory.

No hay comentarios: