Wednesday, January 22, 2014

How to backup/restore IIS7 configuration

No matter whether your configuration file gets corrupted, you make changes you decide to roll-back, you accidentally foo-bar your .config file or some act of terror occurs, some day you'll want to go back to a prior configuration file.  Thankfully, IIS7 makes this super easy.

Backup/Restore via the command line
Backing up IIS7 configuration is as simple as copying the \windows\system32\inetsrv\config directory (and subdirectories) into a backup directory, so you don't need anything special to do it.  Just include this directory in whatever your OS/content back-up plan is, or write a custom script to do it. 

To help make managing backups easy, we've added a simple cmd-line option to AppCmd.exe that makes management of backup/restore sets easy.  For example, to backup configuration, run the follow command:
> %windir%\system32\inetsrv\appcmd.exe add backup "My Backup Name"
to restore that backup, run this command:
> %windir%\system32\inetsrv\appcmd.exe restore backup "My Backup Name"
to delete a backup, run this command:
> %windir%\system32\inetsrv\appcmd.exe delete backup "My Backup Name"

Pretty easy, eh?  Except for the dirty little secret everyone knows...remembering to do a backup!  Thankfully, IIS7 comes to the rescue here.  Thanks to a feature called IIS7 configuration history, IIS will automatically make history snapshots of ApplicationHost.config each time a change is detected, enabling you to easily restore to a prior version.  By default, IIS checks for a new version every 2 mins, and will keep 10 prior versions of the file.  IIS7 stores these snapshots in the %systemdrive%\inetpub\history folder by default.  You can change any of these settings by editing the section in ApplicationHost.config.  This article explains the feature in great detail.

IMPORTANT NOTE:  This feature only appears in Windows Server 2008 and Vista SP1.  It is not in original release version of Vista , as it was not yet finished when Vista first shipped.  This is one of the thousands of changes that was made to IIS7 after Vista RTM, and is one of many reasons you should install SP1 as soon as possible!

How do you restore a prior snapshot?  Well, you could just go to the \inetpub\history\cfgHistory_NNNNNNNNNN directory and copy the applicationHost.config file into it's proper place: \windows\sytem32\inetsrv\config.  Or you can use the same command as above for restoring a backup to restore a configuration history file. 

To enumerate a list of backups and configuration history files, use the following command:
> %windir%\system32\inetsrv\appcmd.exe list backup

See how the config history files show up just like backups?  Use WinDiff or some other tool to figure out which configuration history you want to restore, then use AppCmd.exe restore backup command to restore it!

No comments:

Post a Comment