ServerUbuntu

Email WordPress Database Backups via a Cron Job

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.

#!/bin/bash

USER="user"
PASS="password"
HOST="localhost"

TODAY="$(date +"%Y-%m-%d")"
DEST="/home/user/backup"
MDB="$DEST/databases/$TODAY"

if [ ! -d $MDB ]
then
    mkdir -p $MDB > /dev/null 2>&1 && echo "Directory $MDB created." ||  echo "Error: Failed to create $MDB directory."
else
    echo "Error: $MDB directory exits!"
fi

now="$(date +"%Y-%m-%d_%H-%M-%S")"

file="myFile"

dbs="$(mysql -u $USER -h $HOST -p$PASS -Bse 'show databases')"
# for loop to backup all databases
for db in $dbs
do
    file="$MDB/$db.$now.sql.gz"
    mysqldump -u $USER -h $HOST -p$PASS $db | gzip -9 > $file
    echo "Backup $file.....DONE"
    echo "Database Backup of $file" | mutt -a "$file" -s "Database Backup File Attached" -- nkansahrexford@gmail.com
    echo "Emailing $file.... DONE"
done

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.

    echo "Database Backup of $file" | mutt -a "$file" -s "Database Backup File Attached" -- youremail@address.com

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!

0 6 * * 1 expr `date +\%s` / 604800 \% 2 >/dev/null || /home/user/scripts/wordpressBackup >> /var/log/wp-backup.log

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!

Related Articles

Back to top button