Archive for the ‘Batch’ Category.

Simpler version of the 7-Zip backup

I have tested the 7-Zip backup I had constructured that I posted about a while back and I have come to realise that it is a tad bit too complicated. I even went through the process of creating a proxy application to try and get it to be a bit more cleverer but in the end I simplified it down to just 4 parts; the logic, settings and file lists.

Backup.cmd

@ECHO OFF

ECHO 7-Zip backup script
ECHO Written by Alex Boyne-Aitken
ECHO Last update: 07/11/2009
ECHO.

ECHO TRACE: Parse arguments: '%~f1'
IF EXIST %~f1 GOTO labelBegin

ECHO.
ECHO ERROR: Settings file not found!
GOTO labelWaitEnd

:labelBegin

ECHO.
ECHO TRACE: Reading settings
FOR /F "eol=# tokens=1,2 delims==" %%i IN (%~f1) DO (
SET %%i=%%j
ECHO TRACE: %%i = '%%j'
)

SET varTimeStamp=%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%-%TIME:~0,2%-%TIME:~3,2%
SET varTargetBackupSet=%varBackupPath%\%varTimeStamp%-backup.zip

ECHO.
ECHO TRACE: Backup set: '%varTargetBackupSet%'
ECHO TRACE: Command line: '"%varPathToSevenZip%\7z" a -t%varArchiveType% "%varTargetBackupSet%" @"%varInclusionsFile%" -xr@"%varExclusionsFile%"'

ECHO.
ECHO TRACE: Executing backup
"%varPathToSevenZip%\7z" a -t%varArchiveType% "%varTargetBackupSet%" @"%varInclusionsFile%" -xr@"%varExclusionsFile%"

IF /I NOT "%varWaitAtEnd%" == "true" GOTO labelEnd

:labelWaitEnd
PAUSE
:labelEnd

Settings.txt

# The path to the where the 7z.exe executable is
varPathToSevenZip=C:\Program Files\7-Zip

# The path where to store the backup sets
varBackupPath=D:\Backups

# The file path to the inclusions file
varInclusionsFile=D:\Logaan\Documents\Tools\7ZipBackup\Settings\Inclusions.txt

# The file path to the exclusions file
varExclusionsFile=D:\Logaan\Documents\Tools\7ZipBackup\Settings\Exclusions.txt

# Whether to wait at the end of the backup
varWaitAtEnd=true

# Type of backup archive to create
varArchiveType=zip

Exclusions.txt

*.svn

Inclusions.txt

D:\Logaan\Documents
D:\Logaan\Favorites
C:\Users\Logaan\Desktop
D:\Logaan\Saved Games

How to use it

Place the batch file and settings files in a folder somewhere.

Update the setttings file with the correct paths.

Pass the path to the settings file to the backup batch file.

C:> backup D:\Logaan\Documents\Backup\settings.txt

Or create a scheduled task.

Edit Action

Using 7-zip and batch files to perform backups automatically

UPDATE: I have recently overhauled this and created a simpler script in this post: http://www.logaans-site.co.uk/2009/10/17/simpler-version-of-the-7-zip-backup/

First, a little history:

Since the days when I started using XP I had gotten used to the control that NT Backup provided to allow me to backup my files.

About a year ago I moved to Vista and was unhappy with the “All or nothing” approach that the bundled backup software provided. I was also using OneCare at the time and it followed a similar policy.

Since moving to BitDefender I was much happier with the backup software it provided yet the fact that I was not using a well known backup format rubbed me.

So, using this article: http://www.wilshireone.com/article/115/easy-backups-with-7-zip as a guide, I engineered my own backup mechnism using 7-zip.

Now heres the fun. My backup schedule consists of a weekly incremental backup and one monthly full backup. My files are not so important that I need frequent backups, this schedule suits me down to the ground.

My solution consists of 7 parts:

  • Backup.cmd, this is the main script that executes the command
  • Backupset.txt, a list of files and folders that I wish to backup
  • CurrentSet.txt, a path to the current backup file. Used by incremental backups to update
  • FullBackup.cmd, the script to run a full backup
  • FullBackupSettings.ini, the settings used by a full backup
  • IncrementalBackup.cmd, the script to run an incremental backup
  • IncrementalBackupSettings.ini, the settings used by the incremental backup

Now you could remove the separate scripts for the full and incremental backup, the reason I created separate scripts was that I don’t have to change the arguments in the task scheduler. Instead it is all controlled through those scripts.

Backup.cmd

@echo off

echo TRACE: Start

IF EXIST %1 GOTO Begin

echo Settings file does not exist
GOTO End

:Begin

echo TRACE: Read settings
for /f "eol=# tokens=1,2 delims==" %%i in (%1) do SET %%i=%%j

IF "%varBackupType%"=="full" GOTO CreateNewSet

echo TRACE: Load existing set
for /F %%i in (CurrentSet.txt) do set varTargetBackupSet=%%i

IF EXIST "%varTargetBackupSet%" GOTO Execute

echo Backup set does not exist!
GOTO End

:CreateNewSet
echo TRACE: Create new set

set varTargetBackupSet=%varBackupLocation%\%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%-%TIME:~0,2%-%TIME:~3,2%-backup.%varFormat%
echo %varTargetBackupSet% > CurrentSet.txt

:Execute
echo TRACE: Execute backup
"%var7zipPath%\7z" %varMode% -t%varFormat% "%varTargetBackupSet%" @"%varFileList%"

:End
echo TRACE: Finished
pause

The backup script loads the settings passed as argument 1, does some checking, then calls 7-zip to begin backing up. The settings files define how the files are added and where to etc.

BackupSet.txt

D:\Guild Wars\Screens
D:\Logaan\Documents
D:\Logaan\Favorites
C:\Users\Logaan\Desktop
D:\Logaan\Saved Games
C:\Users\Logaan\AppData\Local\2DBoy
C:\Users\Logaan\AppData\Local\Ascaron Entertainment
C:\Users\Logaan\AppData\Local\id Software
C:\Users\Logaan\AppData\Local\Ironclad Games
C:\Users\Logaan\AppData\Local\Rockstar Games
C:\Users\Logaan\AppData\Roaming\EditPlus 3
C:\Users\Logaan\AppData\Roaming\FileZilla
C:\Users\Logaan\AppData\Roaming\Free Download Manager
C:\Users\Logaan\AppData\Roaming\Xfire

This is the list file passed to 7-zip. The only downside is that you cannot list the same folder or file name twice in this file. The work around would be to invoke multiple backup scripts and then add the duplicate folders and nested zips.

CurrentSet.txt

D:\2009-04-05-17-16-backup.zip

This contains the backup file created in the last full backup. Incremental backups then read it in and use it to update files.

FullBackup.cmd

@echo off
Backup D:\Logaan\Documents\Tools\7ZipBackup\FullBackupSettings.ini

Simples, it just calls the main backup script with the correct settings.

FullBackupSettings.ini

# File path to the 7-zip executables
var7zipPath=C:\Program Files\7-Zip

# Backup format
varFormat=zip

# Target location for the backup
varBackupLocation=D:

# List file
varFileList=D:\Logaan\Documents\Tools\7ZipBackup\BackupSet.txt

# Mode
varMode=a

# Type
varBackupType=full

The settings file is read in and used in the main backup script. Note the mode and backup type, this is what separates the two types of backup.

IncrementalBackup.cmd

@echo off
Backup D:\Logaan\Documents\Tools\7ZipBackup\IncrementalBackupSettings.ini

Similar to the full version, except its providing the increment backup settings

IncrementalBackupSettings.ini

# File path to the 7-zip executables
var7zipPath=C:\Program Files\7-Zip

# Backup format
varFormat=zip

# Target location for the backup
varBackupLocation=D:

# List file
varFileList=D:\Logaan\Documents\Tools\7ZipBackup\BackupSet.txt

# Mode
varMode=u

# Type
varBackupType=incremental

Similar to the full settings, but with the zip mode set to update and its backup type defined as incremental.

Once that has all been put in place, all I do is create two tasks in the windows task scheduler.

You could go one step further and have the script copy the backups to another disk, I just do this myself when I feel like.

Update

After a little testing I found a flaw in the technique that I was using. Essentially what is happening is that new files are being added to the old archive and any deleted files were being left. I want to keep any files that I deleted but I do not want them muddying up the actual latest image.

Instead, what I did was use the -u switch to stop updates to the base archive being made and adding new files to a new archive, which makes it incremental in its true sense.

Backup.cmd

@echo off

echo TRACE: Start

IF EXIST %1 GOTO Begin

echo Settings file does not exist
GOTO End

:Begin

echo TRACE: Read settings
for /f "eol=# tokens=1,2 delims==" %%i in (%1) do SET %%i=%%j

set varNewBackupSet=%varBackupLocation%\%DATE:~-4%-%DATE:~3,2%-%DATE:~0,2%-%TIME:~0,2%-%TIME:~3,2%-backup.%varFormat%

IF "%varBackupType%"=="full" GOTO CreateNewSet

echo TRACE: Load existing set
for /F %%i in (CurrentSet.txt) do set varOldBackupSet=%%i

IF EXIST "%varOldBackupSet%" GOTO ExecuteIncremental

echo Backup set does not exist!
GOTO End

:CreateNewSet
echo TRACE: Create new set
echo %varNewBackupSet% > CurrentSet.txt

:ExecuteFull
echo TRACE: Execute Full backup
"%var7zipPath%\7z" a -t%varFormat% "%varNewBackupSet%" @"%varFileList%"
GOTO End

:ExecuteIncremental
echo TRACE: Execute Incremental backup
"%var7zipPath%\7z" u -u- -u!"%varNewBackupSet%" -t%varFormat% "%varOldBackupSet%" @"%varFileList%"

:End
echo TRACE: Finished
pause

In fact, I have gone one step further and I have built a command line tool that wraps 7-zip and provides a nicer mechanism for configuring the backups. It then executes 7-zip. Once I have tested it a little I will post the source code.

Techinque for loading batch file parameters/settings

This technique allows you to define all of your parameters in a single file and not have to pass them all in on the command line.

This command reads in all of the tokens from the settings file and sets a series of environment variables

for /f "eol=# tokens=1,2 delims==" %%i in (settings.ini) do SET %%i=%%j

The settings file looks like this

# Comment
FirstVariableName=A value

# Comment
AnotherVariableName=A value

Just repeat those blocks. You can then do some validation on the enviroment variables if needs be.

I learnt this technique a little while ago, but I cannot remember where, so I cannot give credit where it is due.