Backup or Copy Windows Server 2008 Configuration

No Comments

In Windows Server 2008 there is a backup tool called Windows Server Backup which however creates huge backup files. I prefer to use a simple command/batch file that copies everything I want to backup, zips it and then uploads it to Amazon S3 (aka “the cloud”)

I have not been able to find a simple way to copy all of my Windows Server 2008 settings. Theses are the bits and pieces I have found:

Copy IIS7 Configuration

To save IIS 7 (Internet Information Services) configuration, copy the config directory %SYSTEMROOT%\System32\inetsrv\config, example:

xcopy %SYSTEMROOT%\System32\inetsrv\config c:\backup\iisconfig\ /S/E

Windows Advanced Firewall Export Configuration

To export all the current configurations of your firewall to a file:

netsh advfirewall export c:\backup\advfirewall.wfw

Source: David Davis

Export Windows Registry

To export the Registry, use regedit.exe command line options. Note that export is done in background so you need to allow some time before you are able to access the exported file.

regedit /e c:\backup\regbackup.reg

Source: Speedguide

Network Settings

netsh dump > c:\backup\network-settings-netsh-dump.txt

Source: Chris Sanders

 

Please note that this method cannot and should not to be compared to a full backup method such as Windows Server Backup.

This method will not allow you to do a full recovery. It will mean installing Windows Server and all software from scratch and then recover the data you have backed up. Meaning an unrecoverable hard drive crash would most likely mean more downtime/recovery time.

The reason I like this method is because it takes less space which allows me to use S3 to save my backups. That means I can fully automate it (scheduled batch script and no need to change any media) and it is off-site (meaning if there is a fire where my server is, my backup is still safe). And I prefer the ease of just getting my files from a zip file if I need to check or recover anything. Besides, it’s dirt cheap, I pay Amazon S3 a few dollars for a month worth of daily 1GB backups.

Automate update of web.config with Microsoft Powershell

1 Comment

I have an ASP.NET web site project that I use for 5-10 web sites. Each has its own directory, setup in IIS and MySQL database, ie they work as independent web sites, only the code base is the same. For updates I have a setup with one Windows command file to copy out any file changes to the web site directories and another command file that updates the database with any database structure changes.

So far I have made any updates to the web.config files manually but today I finally figured out how to automate this too.

The Windows command tool Powershell has been around for a couple of years but I have not used it before. It was installed with Windows Update some time ago so it should be installed on your machine. To use it simply run powershell.exe from a command prompt. To exit, close the window or type exit.
Powershell can among other things run Powershell script files (file extension .ps1)

This script will show you how to add to and edit multiple web.config files (or any xml file)

# Source: http://blog.tjitjing.com

# Array of files to make changes to, add as many as you like
$filesarray = @("c:\temp\web.config","c:\anotherfile.xml") 

# Go thru all files
foreach ($filename in $filesarray)
{
	# Get file + cast as xml
	$xml = [xml](get-content $filename)
	
	# Backup file before making changes
	$backup = $filename + "-" + (get-date).tostring("yyyy-MM-dd-hh_mm_s")
	$xml.Save($backup)
	
	$root = $xml.get_DocumentElement();
	
	# Add a new node
	$node = $xml.createElement("mynewnode")
	$root."system.web".appendChild($node)
	$subnode = $xml.createElement("add")
	$node.AppendChild($subnode)
	$attribute = $xml.CreateAttribute("url")
	$attribute.set_Value("http://blog.tjitjing.com")
	$subnode.SetAttributeNode($attribute )
	
	# Change existing node
	$root.connectionStrings.add.connectionString = "My new connection string"
	
	# Save
	$xml.Save($filename)	 
}

There are two caveats I ran in to when running my script:

1) You need to always give the path of the file, even if it is in your current directory. To run a file from current directory you need to wite ./

C:\Temp>powershell.exe ./test.ps1

Or from within Powershell

PS C:\Temp>./test.ps1

2) Powershells execution policy is by default set to Restricted. To avoid getting a message similar to “File C:\temp\test.ps1 cannot be loaded because the execution of scripts is disabled on this system.”, you need to run the command Set-ExecutionPolicy RemoteSigned from the powershell prompt.

Some useful links that helped me understand this and get it working:
Powershell on Microsoft Technet
Andy Schneider’s Blog about Windows PowerShell
Blog post by Dave Donaldson