Quickly setting auto backup of all of my embedded databases in Windows Server

Posted Comment

I recently migrated all of my hosted applications (including this site mustak.im) to a Virtual Private Server (VPS). Even though it's relatively easy to get something like Azure App Service and deploy ASP.NET Applications there but I find managing my own Private App and Build Server more appealing.

I needed to have a reliable backup since disaster can happen anytime (as I'm managing the server on my own) but I did not want to pay for commercial solutions. I'm not making money out of my sites (these are my dev works, experiments in progress...) so I have access to any free software. However, I wanted to roll out something really simple, fast and light on my own - without installing a beast of a backup software.

The Batch file

The windows batch file I've used does the following

  • Stop IIS (since some NoSQL Db's might be exclusively locked)
  • Find and compress all NoSQL and Embedded database to an archive with the name Backup-{Date}-{Time}.7z
  • Create the archive in a local folder that is synced with Mega.nz
  • Delete backups older than 7 days from the same folder
  • Start IIS

I'm relying on MEGAsync app to sync these backups with the cloud. It could be dropbox or google drive but I choose MEGA because of their generous free 50GB storage space allowance.

@echo off
echo Stopping IIS

net stop WAS /y

echo Creating Backup

"C:\Program Files\7-Zip\7z" -r a -t7z "C:\Users\me\Documents\MEGAsync\NoSQLDB\Backup-%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%-%time:~0,2%%time:~3,2%.7z" ^
C:\inetpub\wwwroot\*.ldb ^
C:\inetpub\wwwroot\*.sdf ^
C:\inetpub\wwwroot\*.db ^

echo Starting IIS
net start W3SVC

@echo Removing older backups

for /f "skip=7 eol=: delims=" %%F in ('dir /b /o-d /a-d C:\Users\me\Documents\MEGAsync\NoSQLDB\*.7z') do @del "C:\Users\me\Documents\MEGAsync\NoSQLDB\%%F"

Setting up Scheduled Task

I have created a basic schedule task using Windows Task scheduler to execute the batch file daily at a specific time. I made sure this task is run in an elevated permission (as shutting down the server requires this) and this one is run regardless of the user is logged in or not.

Task scheduler

...but the task was running really slow!

Normally scheduled tasks takes longer to complete because windows runs them on a low priority mode. That is why it was taking way longer to finish this task. As an workaround I have force this task to be run with normal priority. Unfortunately this is something you can not do in the schedule task's properties page. What you have to do is:

  • Export the task as XML file,
  • Change the Priority attribute inside the XML file from 7 to 4 (that means it will run as normal priority)
  • Import the task from the XML file and delete the original one

Here is the fragment of the XML where I had to make this modification

Exported Task Schedule

As you can see, I save the Backup.bat script itself in the MEGAsync directory, just in case!

What's New Facebook & Twitter

LexisNexis Software Engineer II

Team Rapture Team Member, since 2011