Recently I have been brainstorming about having an automated way of backing up IIS configs to a shared folder. Just imagine having your web servers going down and having to re-add all the website settings and bindings for hundreds of websites. I know for a fact that I would need a few strong drinks.
Let’s just have a cocktail and make a PowerShell script to do it for you instead.
I will post the script below. But for now, let’s break it down.
First we need to set the day of the week if you are going to be making daily backups.
$DayOfWeek = Get-Date -UFormat %A
Now we need to set the variables for the folders.
$ConfigFolder = "C:\Windows\System32\inetsrv\backup" $ShareFolder = "\\YOURSHARE\IISBackup\$DayofWeek" $ConfigLog = "C:\Users\LOCALUSER\Documents\Logs\IISConfig.log"
Let’s set the variables for email.
$EmailFrom = "backup@yourorg.com" $EmailTo = "webadmin@yourorg.com" $EmailBody = "Robocopy backup completed. See attached log files for more details." $EmailSubject = "YOUR ORG - IIS Config Robocopy Summary" $SMTPServer = "127.0.0.1" $SMTPPort = "25"
Now we need to define the actual backup of the IIS Configs.
Backup-WebConfiguration -Name BackupTest
Move it over to your share.
Robocopy $ConfigFolder $ShareFolder /MIR /FFT /R:3 /W:10 /Z /LOG:$ConfigLog /NP
IIS doesn’t allow multiple names or even overwriting the configs. Understandable since you may need a weeks worth of backups. But we need to delete the old logs.
Remove-Item -path C:\Windows\System32\inetsrv\backup\BackupTest -recurse
Now is the real helpful part. Since you don’t want to check the servers everyday to see if the backups were successful, you will need an email.
Send-MailMessage -Attachments $ConfigLog -SmtpServer $SMTPServer -Subject $EmailSubject -Body $EmailBody -From $EmailFrom -To $EmailTo
That’s the script. It’s pretty simple and straight to the point. Any system running Windows 10 or later or any Windows Server OS will allow this script to be run.
It’s best to run this script with task scheduler. Task scheduler will even keep a nice history list if it fails or not.
Below is all the code combined and allows you to save and run.
# IIS Configuration Manager Backup Utility # Define & Strip Day Name $DayOfWeek = Get-Date -UFormat %A # Set IIS Folders $ConfigFolder = "C:\Windows\System32\inetsrv\backup" $ShareFolder = "\\YOURSHARE\IISBackup\$DayofWeek" $ConfigLog = "C:\Users\LOCALUSER\Documents\Logs\IISConfig.log" # Set Email Parameters $EmailFrom = "backup@yourorg.com" $EmailTo = "webadmin@yourorg.com" $EmailBody = "Robocopy backup completed. See attached log files for more details." $EmailSubject = "YOUR ORG - IIS Config Robocopy Summary" $SMTPServer = "127.0.0.1" $SMTPPort = "25" # Backing up the IIS Config Backup-WebConfiguration -Name BackupTest # Move the folders to your share Robocopy $ConfigFolder $ShareFolder /MIR /FFT /R:3 /W:10 /Z /LOG:$ConfigLog /NP # Delete Local Files After Move Remove-Item -path C:\Windows\System32\inetsrv\backup\BackupTest -recurse # Email Notifications Send-MailMessage -Attachments $ConfigLog -SmtpServer $SMTPServer -Subject $EmailSubject -Body $EmailBody -From $EmailFrom -To $EmailTo