IIS - Automated Remote Website Backups with MSDeploy and Dropbox on Windows Server 2012 R2

Jason Watmore
Wednesday 08 July 2015

No website hosting provider wants or expects their web server to crash and burn, and chances are (if it's configured properly and secure) that it never will, but if it does you want to know that you've got a disaster recovery plan in place that works and you can get all your websites back online quickly.

I recently setup a new web server running Windows 2012 R2 to replace an older Windows 2008 server for a law firm marketing client in Sydney, and decided to go with a combination of PowerShell, MSDeploy and Dropbox to automatically create daily backups of all websites in IIS (including files, application pools and IIS configuration) and save them remotely.

There are other options for automating backups of web servers, web hosting companies in Sydney, Melbourne, Brisbane and around Australia usually offer full server backups that can be restored to a new machine in the event of a hardware failure. The reason I chose this option for our Sydney web development customer is that it also works in the case that there's a software failure on the server that would be included with any full image backup, the other benefit is that with backups created with MSDeploy it's simple to upgrade to a new server running the latest version of Windows and IIS.

STEPS TO SET IT ALL UP:

Install MSDeploy

MSDeploy can be installed using the Microsoft Web Platform Installer or as a standalone install from http://www.iis.net/downloads/microsoft/web-deploy.

Once installed the MSDeploy command line tool is located in C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe (for Web Deploy 3.5).

Install Dropbox

Download and install Dropbox from their website (https://www.dropbox.com/downloading)

Run Dropbox as a Windows Service

Dropbox by default runs only when you log into your machine, so in order to get it to run all the time in the background you need to run it as a Windows Service.

  • Update Dropbox Preferences
    • Choose preferences and uncheck “Show desktop notifications” and “Start Dropbox on system startup”
    • Exit Dropbox by clicking exit in the context menu that shows when right clicking icon in task bar
  • Create Dropbox Windows Service
    • Install Windows 2003 Resource Kit Tools - https://www.microsoft.com/en-au/download/details.aspx?id=17657 (Ignore error about the software having known issues)
    • Open a command window as an Administrator
    • Execute the following command:
      sc create Dropbox binPath= "C:\Program Files (x86)\Windows Resource Kits\Tools\srvany.exe" DisplayName= "Dropbox Service"
  • Set Dropbox Service to run under the Administrator account
  • Set Startup Type to Automatic
  • Update the Windows registry to point the new windows service to the Dropbox executable
    • Start > Run > regedit
    • Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Dropbox
    • Create a new key “Parameters”
    • Add a new string value “Application”, (type REG_SZ). Set the value to the path to the dropbox.exe binary (e.g. C:\Users\[YOUR USERNAME]\AppData\Roaming\Dropbox\bin\Dropbox.exe /home

Disable Dropbox Auto Updates

Dropbox has an auto update feature that can stop it from working when running as a service, unfortunately there isn't any setting within dropbox to disable the auto updates but I've found that if you delete the 2 scheduled tasks starting with "DropboxUpdateTask" it does the trick.

  • Open the Windows Task Scheduler
  • Select the Task Scheduler Library
  • Delete the 2 tasks that have names starting with "DropboxUpdateTask"

Create a PowerShell Script to Generate Backups with MSDeploy and save to Dropbox folder

Save the following PowerShell script to a file named BackupSites.ps1, don't forget to set the $backupPath variable.

The script backs up all running IIS websites and deletes old backups, you can set the days to keep backups with the $daysToKeep variable.

# location of msdeploy command line tool
$msdeploy = 'C:\Program Files\IIS\Microsoft Web Deploy V3\msdeploy.exe'

# location of folder to save backups (make sure it's in your dropbox folder!!)
$backupPath = '[ENTER BACKUP DROPBOX FOLDER PATH HERE]'

# number of days to keep backups before deleting them
$daysToKeep = 60

# delete old backups
Get-ChildItem $backupPath -Recurse | Where {$_.LastWriteTime -lt (Get-Date).AddDays(-$daysToKeep)} | Remove-Item -Force

# create new backups
foreach($website in Get-Website | Where-Object State -eq 'Started') {
New-Item -ItemType Directory -Force -Path ($backupPath + $website.Name)
& $msdeploy @('-verb:sync', '-enableLink:AppPoolExtension', ('-source:appHostConfig="' + $website.Name + '"'), ('-dest:package=' + $backupPath + $website.Name + '\' + $website.Name + '-' + (Get-Date -Format yyyyMMddHHmm) + '.zip')) }

Add a Scheduled Task to Run the Backup Script Daily

Open Windows Task Scheduler and create a new task with the following options:

  • General
    • BackupSites
    • Set user to an Administrator
    • Run whether user is logged on or not
  • Triggers
    • Settings: Daily at 3am (or any time you like) starting the following day. Recur every 1 day.
  • Actions
    • Action: Start a program
    • Program/script: PowerShell
    • Add arguments (optional): .\BackupSites.ps1
    • Start in (optional): [FOLDER CONTAINING BACKUP SCRIPT (BackupSites.ps1)]

That's it! You should be able to run the scheduled task and see the backups appear in your dropbox folder :)

 

Web Development Sydney and Law Firm Marketing

Point Blank Development provide cutting edge web development services to clients in Sydney and around Australia, for more information see our web development service page and check out some of the websites we've built.