Backup Your WordPress Database with MySQL Dump

So you’re the kind of person that likes you have complete control over your WordPress blog? You set up your MySQL databases all on your own? Good on ya! Here’s a little quick script (assuming you’re a little more advanced at proper webhosting/maintenance) that will be sure your database is backed up properly every night. It works without plugins and best of all it’s free.

A few things to consider (and what I’m assuming) before you use this:

1) MySQL is already setup and the databases are in the default location (/var/lib/mysql)

2) There is a folder already setup to write your backups to (it can be in the same location as your databases to make it easier)

3) You must be logged in as root on the server and already have your root/pass setup for the MySQL administration

Here’s what your script will do for you:

1) At 12 midnight everyday, mysqldump will backup all your databases to the assigned folder and append the current date to the filename, then gzip to compress it.

2)Then at 1am, the second part will check the timestamps and delete any backups that are older than 90 days

And here’s the script and how to set it up:

Be sure to log in to your server as root and type the following -

crontab -e

Then insert the following into your crontab script that opens:
MAILTO=root
0 0 * * * mysqldump -A -u root -ppassword | gzip > /var/lib/mysql/db_backups/mysqlbak`/bin/date +\%F`.gz
0 1 * * * find /var/lib/mysql/db_backups/*.gz -mtime +90 | xargs rm -f

Note: Notice there is no space between the -p and the actual password for your root MySQL account. This is intentional. If it was typed as ”-p password” the job would hang since it will be waiting for the user to type it in manually.

Also note that any errors will be logged to /var/logs/messages -hence the MAILTO=root

P.S. – If you aren’t accustomed to the inner workings of linux and the command line terminal (ie: a newbie), try our how-to on the WordPress Backup Plugin.

Share and Enjoy:
  • Twitter
  • Digg
  • StumbleUpon
  • FriendFeed
  • del.icio.us
  • Reddit
  • Technorati
  • email

Leave a Reply

 

Spam Protection by WP-SpamFree

SEO Powered by Platinum SEO from Techblissonline