Switch to full style
User discussion and information resource forum for scripting of TeraByte products using script lanugages such as TBScript, VBScript, Batch Files, and more.
Forum rules
Post a reply

Incremental Rotate based on count

Sun Oct 25, 2015 4:55 pm

I modified the "Sample Backup Batch Script 2" to create a similar that instead of differential backups it will create incremental backups.

edit: I updated the code. If the batch finds in the directory the last incremental image it will continue normally. If it does not find it, instead of giving an error it will create a new full image.

[code]@echo off

:: Copyright (C) 2011-2012, TeraByte Unlimited. All rights reserved.
:: version 1.2

:: This batch file maintains multiple iterations of a particular backup. Each
:: time the batch file is run, a backup will be created. The filename will
:: include the date and time. For example, the first time the batch file is run,
:: <file name>_2010-04-18-2315__FULL.tbi will be created. On the next run,
:: <file name>_2010-04-19-2315__FULL.tbi will be created. (Assuming it's run the
:: next day at the same time.) Once the number of backups reaches the number
:: specified, the oldest backup (or backup set) is deleted.
::
::
:: There are several settings you need to specify below.
:: Each one is preceded by the "set" command.

:: ---------------------------------------------------------------------------

:: Specify the folder where backup files will be saved. In all cases, omit the
:: trailing backslash, even for root directories (e.g. C: or D:). Folder
:: path can include spaces.
::
:: A UNC path can also be specified for this variable. NOTE: Make sure the share
:: is accessible or IFWRotate and/or Image for Windows may not function as expected.
:: If necessary, use the TBILogin variable below to specify the network share
:: login details. If IFWRotate cannot access the path it will be unable to count the
:: previous image backups, which will result in only Full images being created and
:: no images being deleted. In this case, it may be necessary to add credentials
:: for the share to the user account running IFWRotate.
::
:: Example: set TBIBase=D:\My Backups
::
:: Example: set TBIBase=\\server\backups\mybackups

set TBIBase=



:: Specify the desired backup filename below. In all cases, omit
:: the file extension. Filename can include spaces. This is the "base"
:: filename used for creating the Full and Incremental filenames.
::
:: Backup images will have the same "base" name as the specified TBIName value
:: with the date, time, and "__FULL" appended (for Full images) or "_INCR_", date,
:: and time appended (for Incremental images).
::
:: Created Full image filename example: WorkComputer_2010-04-20-1754__FULL.tbi
::
:: Created Incremental image filename example: WorkComputer_2010-04-20-1754_INCR_2010-04-21-1754.tbi
::
:: Example: set TBIName=WorkComputer

set TBIName=



:: Specify the parameters that should be used by IFW below. This is
:: where you specify the source drive and partition for the backup. Do not
:: include the "/f" option. It will be included automatically.
::
:: Note: If you need to use the Image for Windows "/login" option to access a network share,
:: use the TBILogin script variable below instead of specifying /login here.
::
:: Example: set TBIParms=/b /d:w0@0x1

set TBIParms=



:: Specify the path to Image for Windows (IMAGEW.EXE).
:: Note: Set this path only if not using the default installation path.
:: Do not include a trailing backslash. Do not include any spaces before or after the equals sign.
:: Omit quotation marks and the trailing backslash.
::
:: Example: set TBIPath=C:\Program Files (x86)\TeraByte Unlimited\Image for Windows\V2

set TBIPath=



:: Specify the maximum number of Full image files to be saved. The oldest
:: Full image file (along with any associated Incremental images) will be
:: deleted when this value is exceeded. Number must be between 2 and 99.
::
:: Note: If the number of existing backup sets (Fulls or Fulls + Incrementals) exceed
:: the number specified, the older sets will be deleted when a new Full is created.
:: This may result in multiple sets being deleted. For example, if you had a previous
:: maximum of 7 and changed it to 3, the oldest sets will be deleted to bring
:: the count down to the new maximum of 3 when a new Full is created.
::
:: Example: Set TBIMaxFullCnt=4

set TBIMaxFullCnt=4



:: Specify the maximum number of Incremental image backups to be created for
:: each Full backup. A setting of 0 (zero) will create only Full image backups.
:: If you want to create Incremental backups, set the value to 1 or higher (max. 99).
::
:: Example: set TBIMaxINCRCnt=6

set TBIMaxINCRCnt=6



:: Specify any parameters needed when creating a Incremental backup. Don't
:: include the "/b", "/base", or "/f" options as those are automatically
:: included. For example, you may want to verify the image after it's created.
::
:: Note: If you need to use the Image for Windows "/login" option to access a network share,
:: use the TBILogin script variable below instead of specifying /login here.
::
:: Example: set TBIIncParms=/v
::

set TBIIncParms=



:: Specify the network login details (if needed to access a network share). Do not set
:: if network login is not required.
::
:: If you include the login details here, don't include them in TBIParms or TBIIncParms.
:: See the Image for Windows manual for details using the /login parameter.
::
:: Note: When this variable is used the script must be run with administrator privileges.
::
:: Example: set TBILogin=/login:"\\server\share*username*password"
:: Example: set TBILogin=/login:"\\BackupServer\Win7Backups*JohnSmith*mypassword"

set TBILogin=



:: ====================================================
:: Command line options for this script file
:: ====================================================
::
:: /f Forces a new Full image to be created. Rotation rules for the
:: maximum number of Full images will still be enforced.
::
:: /d Forces a new Incremental image to be created. Using this option
:: will create a Incremental image even if it will exceed the maximum
:: number specified.
::
:: Note: The Incremental image will be based on the newest Full image.
:: If no Full image exists, a Full image will be created instead.
::
::

:: ###########################################################################
:: #
:: # NO CHANGES SHOULD BE NEEDED PAST THIS POINT
:: #
:: ###########################################################################

:: Check for Windows 2000 or later

if not "%OS%"=="Windows_NT" goto :NoWin

:: ---------------------------------------------------------------------------

:: Init vars

setlocal enableextensions enabledelayedexpansion

if "%INCRotate%"=="" for /f "usebackq tokens=2,*" %%G in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\TeraByte Unlimited\Image\ExternVars" 2^> nul ^| find "INCRotate"`) do set INCRotate=%%H
set Error=X
set Number1=X
set Number2=X
set TBICmd=X
set TBIOldest=X
set TBINewest=X

if %TBIMaxFullCnt% LSS 2 set /a TBIMaxFullCnt=2
if %TBIMaxFullCnt% GTR 99 set /a TBIMaxFullCnt=99
if %TBIMaxINCRCnt% LSS 0 set /a TBIMaxINCRCnt=0
if %TBIMaxINCRCnt% GTR 99 set /a TBIMaxINCRCnt=99

if not defined TBIBase echo No TBIBase parameter supplied. Please edit: %0 & goto :End
if not defined TBIName echo No TBIName parameter supplied. Please edit: %0 & goto :End
if not defined TBIParms echo No TBIParms parameter supplied. Please edit: %0 & goto :End
if not defined TBIMaxFullCnt echo No TBIMaxFullCnt parameter supplied. Please edit: %0 & goto :End
if not defined TBIMaxINCRCnt echo No TBIMaxINCRCnt parameter supplied. Please edit: %0 & goto :End

:: ---------------------------------------------------------------------------

if "%TBIPath:~0,1%"==" " (set TBIPath=)
if "%TBIPath%"=="" if "%PROCESSOR_ARCHITECTURE%"=="AMD64" set TBIPath=C:\Program Files (x86)\TeraByte Unlimited\Image for Windows\V2
if "%TBIPath%"=="" set TBIPath=C:\Program Files\TeraByte Unlimited\Image for Windows\V2
if not exist "%TBIPath%\imagew.exe" echo Unable to find 'imagew.exe' in the following path: & echo %TBIPath% & echo. & echo Please edit %0 and check TBIPath value. & goto :End

:: ---------------------------------------------------------------------------

:: Log into network share

if "%TBILogin:~0,1%"==" " (set TBILogin=)
if defined TBILogin start "" /min /wait "%TBIPath%\imagew.exe" %TBILogin% /log:0 /quit

:: Get Full image file counts

set /a Number1=0
if exist "%TBIBase%\%TBIName%_*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%_*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1) & (if "!TBINewest!"=="X" set TBINewest=%%a)
set TBIFullCnt=%Number1%
echo.
echo Number of Full images found in set: %TBIFullCnt%
if "%TBINewest%"=="X" set TBINewest=(none)
if "%TBIOldest%"=="X" set TBIOldest=(none)
echo Oldest Full image set found: %TBIOldest%
echo Newest Full image set found: %TBINewest%

:: Get INCR image file counts for newest set

set TBINewest=%TBINewest:~0,-10%
set /a Number1=0
if exist "%TBIBase%\%TBINewest%_INCR*.tbi" for /f %%a in ('dir "%TBIBase%\%TBINewest%_INCR*.tbi" /o-d /b /p /-p /w /-w') do (set /a Number1=!Number1!+1)
set TBIINCRCnt=%Number1%
echo.
echo Number of Incremental images found in newest Full set: %TBIINCRCnt% of %TBIMaxINCRCnt%

:: Check which type of backup (Full or INCR) needs to be created

if /i "%1"=="/f" goto :CreateFull
if %TBIFullCnt%==0 goto :CreateFull
if /i "%1"=="/d" goto :CreateINCR
if %TBIINCRCnt% LSS %TBIMaxINCRCnt% goto :CreateINCR
goto :CreateFull

:CreateINCR

echo.
echo Create Incremental Image
if not exist "%INCRotate%.tbi" goto:CreateFull
echo -- Running command: "%TBIPath%\imagew.exe" /b %TBIIncParms% /base:"$~INCRotate$" /f:"%TBIBase%\%TBINewest%_INCR_$~YYYY$-$~MM$-$~DD$-$~HHMM$" /hash /savename:INCRotate
start "" /wait "%TBIPath%\imagew.exe" /b %TBIIncParms% /base:"$~INCRotate$" /f:"%TBIBase%\%TBINewest%_INCR_$~YYYY$-$~MM$-$~DD$-$~HHMM$" /hash /savename:INCRotate
goto :PostBkup

:CreateFull

if %TBIFullCnt% LSS %TBIMaxFullCnt% goto :CreateFullImage
echo.
echo Number of Full images will exceed %TBIMaxFullCnt%.
echo Deleting oldest Full image set: %TBIOldest%
del "%TBIBase%\%TBIOldest%"
set TBIOldest=%TBIOldest:~0,-10%
if exist "%TBIBase%\%TBIOldest%_INCR*.tbi" del "%TBIBase%\%TBIOldest%_INCR*.tbi"
if exist "%TBIBase%\%TBIOldest%_INCR*.#*" del "%TBIBase%\%TBIOldest%_INCR*.#*"
if exist "%TBIBase%\%TBIOldest%__FULL.#*" del "%TBIBase%\%TBIOldest%__FULL.#*"
:: Check if additional old backup sets need deleted
set /a Number1=0
if exist "%TBIBase%\%TBIName%_*__FULL.tbi" for /f "delims=" %%a in ('dir "%TBIBase%\%TBIName%_*__FULL.tbi" /o-d /b /p /-p /w /-w') do (set TBIOldest=%%a) & (set /a Number1=!Number1!+1)
set TBIFullCnt=%Number1%
if %TBIFullCnt% LSS %TBIMaxFullCnt% goto :CreateFullImage
goto :CreateFull


:CreateFullImage

echo.
echo Create Full Image
echo -- Running command: "%TBIPath%\imagew.exe" %TBIParms% /f:"%TBIBase%\%TBIName%_$~YYYY$-$~MM$-$~DD$-$~HHMM$__FULL" /hash /savename:INCRotate
start "" /wait "%TBIPath%\imagew.exe" %TBIParms% /f:"%TBIBase%\%TBIName%_$~YYYY$-$~MM$-$~DD$-$~HHMM$__FULL" /hash /savename:INCRotate
goto :PostBkup


:: ---------------------------------------------------------------------------

:PostBkup

set Error=%ERRORLEVEL%

echo.
echo #########################################
if not "%Error%"=="0" echo # The following error was reported: %Error%
if "%Error%"=="0" echo # No errors were reported.
echo #########################################
echo.

goto :End


:: ---------------------------------------------------------------------------

:NoWin

echo.
echo Windows 2000 or later is required for this script.
echo.
goto :End


:: ---------------------------------------------------------------------------

:End

set Error=
set Number1=
set Number2=
set TBIFullCnt=
set TBIINCRCnt=
set TBICmd=
set TBIOldest=
set TBINewest=

endlocal

set TBIBase=
set TBIName=
set TBIParms=
set TBIPath=
set TBIMaxFullCnt=
set TBIMaxINCRCnt=
set TBIIncParms=
set TBILogin=

:: End of batch script
[/code]
Last edited by Panagiotis on Tue Oct 27, 2015 8:44 am, edited 2 times in total.

Re: Incremental Rotate based on count

Sun Oct 25, 2015 5:03 pm

Although the script works great I need a way to make that batch file to get from the registry the saved directory/name (=last created full or incremental) from
[HKEY_LOCAL_MACHINE\SOFTWARE\TeraByte Unlimited\Image\ExternVars]
"INCRotate"=

and check if the file exists.
if exist "$~INCRotate$" go to :CreateINCR
if not exist "$~INCRotate$" go to :CreateFullImage

Re: Incremental Rotate based on count

Sun Oct 25, 2015 7:12 pm

This is a sample of pulling the variable. The ...if "%INCRotate%"=="" for... line should be all on one line.

@echo off
setlocal enableextensions enabledelayedexpansion
cls

(set INCRotate=)
if "%INCRotate%"=="" for /f "usebackq tokens=2,*" %%G in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\TeraByte Unlimited\Image\ExternVars" 2^> nul ^| find "INCRotate"`) do set INCRotate=%%H

if "%INCRotate%"=="" echo Not found.
if not "%INCRotate%"=="" echo Found: %INCRotate%

Re: Incremental Rotate based on count

Mon Oct 26, 2015 12:09 am

In that batch file, why not using the environment variables %ProgramFile% and %ProgramFile(x86)% ?

Re: Incremental Rotate based on count

Tue Oct 27, 2015 8:46 am

Thank you very much. I updated the script and it works great now.

TeraByte Support(PP) wrote:
> This is a sample of pulling the variable. The ...if
> "%INCRotate%"=="" for... line should be all on one line.
>
> @echo off
> setlocal enableextensions enabledelayedexpansion
> cls
>
> (set INCRotate=)
> if "%INCRotate%"=="" for /f "usebackq tokens=2,*" %%G
> in (`reg query "HKEY_LOCAL_MACHINE\SOFTWARE\TeraByte
> Unlimited\Image\ExternVars" 2^> nul ^| find "INCRotate"`) do set
> INCRotate=%%H
>
> if "%INCRotate%"=="" echo Not found.
> if not "%INCRotate%"=="" echo Found: %INCRotate%

Re: Incremental Rotate based on count

Tue Oct 27, 2015 8:58 am

I wanted to keep the modifications of the original differential batch to a minimum. My guess is that Terabyte wanted to include support for non default installations or for those users that use IFW as a portable app.
But, you can use global environment variables if you want.

Eric wrote:
> In that batch file, why not using the environment variables %ProgramFile%
> and %ProgramFile(x86)% ?

Re: Incremental Rotate based on count

Wed Mar 09, 2016 2:14 pm

Will this work for a linux script via cron? What changes might I need to make in order to do so?
Post a reply