Skip to content

Email WordPress Database Backups via a Cron Job

Posted in Server, and Ubuntu

I email WordPress Database backups to myself as a way of permanent storage. I find this approach easier, free of headaches and setting up is pretty straightforward and easy.

Other options might be to rSync the database files to a remote server, or to your Google Drive or Dropbox or even if you’re rich, a dedicated server which holds backups somewhere stationed in a black site somewhere in the world.

To send backups to your Google Drive or Dropbox or any other cloud storage for that matter requires a bit of overhead to setup.

In this article, I share how to send your database backups as a mere email to yourself, enabling simple archiving.

Redundancy Matters

You might be confused why one needs to backup the WordPress database since the server is already secure. Well, the server ain’t secure.

Consider every day of your Server as the last. No matter how secured you think your system is, having a redundant option for your WordPress installation is crucial.

Keep a backup of your WordPress database on your server and email them as well to somewhere else for a double layer redundancy of your databases.

Here we go!

This is the final file for backing up the WordPress installation on the server’s databases. Now, this script will be called by a cron task which we discuss next. The snippet below was adapted from here.

The above bash script, in English means:

Find all the MySQL databases, back the databases up to a folder named with the current date, then email all the backed up databases to an address

Like seriously? Yeah, seriously, it is that simple.

The echo part is what does the fun part we’re talking about, emailing each backup file to an address. If you have 3 databases, 3 emails will be sent one after the other.

Since the above script is meant to be run at certain time intervals, how about we introduce the cron task responsible for the job!

The above cron job will run every fortnight (2 weeks). For a deeper explanation of how the above cron job works, see this Stack Overflow discussion.

How to add the cron job is history, just sudo crontab -e and enter the above expression in relation to where your wordpressBackup file is located.

Conclusion

This is a simple way to keep your WordPress backed up outside of your server instance. Although securing your server so no one gets in to cause havoc, having a backup plan should that happen is essential, and emailing backups to yourself is a sure way to handle that.

I hope you enjoyed this tip. Hope to see you in the next one!