Thursday, April 26, 2012

How to take backups within InnoSetup

One of the important requirements for any software installer is to be able to take a backup of the system before new code is updated. Once code is updated, there must be a rollback (or uninstall procedure) as well. The installer must be able to place the code backup in a place where it can easily pick up code from when uninstalled. For software patches where several patches can be installed successively, this can get fairly complex since we only want to rollback specific code which is associated with the patch.

Taking code backups and doing a rollback is not an inbuilt feature in InnoSetup. However, with some customization, this can be easily achieved.

To backup code, a line very similar to code installation can be created when the installer is first created. For example:

Source: {app}\\*; DestDir: "{#InstallHomeDir}\UnInstall\{#MyAppVersion}\Websites\"; Components: Core; Flags: {#UninstallFlags}; Check:TakeBackup;
This line is similar to a code install line, except it copies code from the application folders on  the target machine to a pre-determined backup location. So this line only fires at run-time when code is being installed. For each "Source" line in the installer for code installation, there will be a matching line for backup similar to the one above. Note that a version number is included inside the DesDir to handle multiple patch scenarios. A Check is also used to turn backups on or off. 

Once the backup is done, at un-install time, you can fire the following command from within Inno Setup:

Exec('xcopy', ExpandConstant('"{#InstallHomeDir}\UnInstall\{#MyAppVersion}\Websites" "{app}" /f /c /v /e /r /h /y'), '',SW_SHOW, ewWaitUntilTerminated, ResultCode)
This will copy ALL your folders you have backed up to the {app} folder.

No comments: