Powershell scripts for MSSQL or MongoDB backup to Azure & AWS

Powershell scripts for MSSQL or MongoDB backup to Azure & AWS

In this post, I’d like to present the two simple Powershell scripts, that will let you create a secured (password protected zip archive) backup of the MSSQL or MongoDB databases and upload it either to the Azure or AWS cloud. By using such solution, you may actually save a few bucks instead of using some external, paid services like e.g. CherrySafe that do the same thing.


The prerequisites:

Of course, it’s up to you whether you prefer the Azure or AWS or even storing the backup on your local drive – the scripts presented below can work with any backup scenario. On top of that, the backup data will be put into the password protected zip archive before sending it to the cloud storage.

Let’s start with the MongoDB backup:

At the beginning you can see a bunch of variables that have to be set in the first place.
Then the mongodump fires up, next the zip archive is being created, and finally, the backup is being sent to the S3 and Azure storage respectively. Before the script finishes, it does remove the temporary backup from the local drive by calling Remove-Item.

And here’s the MSSQL backup script:

Pretty similar to the previous one, except that it makes use of the MSSQL backup services based on the SQL Server DAC.

I use such scripts in some of my projects, for example in the Warden for which I need the MongoDB backup in order to save the Web Panel database.
To make it work, you may schedule a new task by using the built-in Task Scheduler, and select a new action of type: Start a program. In the Program/script section set the: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and finally pass the path to the backup script in the Add arguments textbox e.g. C:\Tasks\mongodb-backup.ps1.

Surely, when the database grows to some big numbers, probably some other backup strategies would be more efficient, but unless you have tens of gigabytes of the data, these two scripts presented above should do the trick.

Leave A Comment

Your email address will not be published. Required fields are marked *