How to save money on Azure with WebJobs
This blog is hosted on Azure. It is using WordPress, which stores its data in MySQL database hosted by ClearDb. I blogged about the issues with WordPress database size over a year ago. The issue is inserting transient entries into database that are growing its size and exceeding free 20MB ClearDB limit. As I mentioned in this blog post - you can use plugins that clear database for you, or...you can use web jobs. Azure WebJobs is very neat way to perform custom tasks, such as database maintenance, periodically.
I created a web job that is performing database maintenance for me. Everyday it removes 'transient' entries from wp_options table to keep my MySQL database under 20MB ClearDB limit.
One problem was: how to do MySQL backup without mysqldump? As you know, there is a Virtual Machine underneath every Azure website. And this VM has MySQL, along with mysqldump installed:
Based on this article I created a small Console App called DbMaintenance that performs 3 mentioned tasks:
- Backup database
- Remove transient entries from
- Backup database
Two backups gives me more confidence that I have my database backed up correctly. Especially because I am performing them before and after the operation that potentially can broke things.
You can customize my app by editing
Initialize method. Current paths (for mysqldump and database backup directory) are set for Azure Website. Additionally you have to create directory for backups (
D:\home\db-backups). You can do it from the web app console available on the Azure Portal:
Once this is done, you need to zip DbMaintenance.exe and MySql.Data.dll file, and create a WebJob. On the Azure Portal go to your web app -> Settings -> WebJobs, and add a new WebJob:
You can see the status in Settings -> WebJobs -> YOUR_JOB_NAME:
By clicking on logs column, you can get details about web job:
And console output from each execution:
I configured my WebJob to run everyday. You can also run it on demand, or continuously.
You can find more about Azure WebJobs in this article.