New Year’s Resolution: Automate WordPress / WPMU Backups. Check.

January 7, 2010  | 
9 Comments

filesThis is a great New Year’s resolution for any WordPress developer, blogger, WPMU or BuddyPress community manager, and anybody else responsible for a WordPress-related site. It’s much easier to accomplish than dropping that 10lbs of Christmas fudge you gained over the holidays. Yes, I know that setting up Cron jobs are boring, but I urge you to take 5 minutes to do this today and you will thank yourself later. You cannot always depend on your host for backups. I’m sorry to report that I’ve heard numerous horror stories recently of people losing everything because of a mishap on their hosting provider’s part. No amount of apologizing on their part will bring back your database. Here are some quick instructions for automating the backups for your blogs:

How to Automate Blog Database Backups

1. Login to cPanel or Plesk or whatever other GUI you use and locate Cron jobs.

2. Select Advanced (Unix Style)

3. Set up the cron task to backup your blog every day at a certain time each day, as shown in the screenshot below:

cron

This one is set to backup at 5pm every day. The asterisks can be interpreted as “each”, ie. each day, each month, etc.

Enter the command line but replace with your own information:

mysqldump -udbusername -pdbpassword db_name | gzip > /home/cpanelusername/yourfolder/mysql_backup.gz

Check your wp-config.php file to find your database, username and password for the database if you’ve forgotten it and then enter those values in the appropriate places, along with the path to the directory where you want the file to dumped.
Here’s an example:

mysqldump -usunshineblog -psunshine1234 sunshineblog | gzip > /home/happyuser/sunshine/mysql_backup.gz

If you don’t add a date stamp, the backup will continually overwrite the last copy saved and you will always only have that one backup stored. This is great for not taking up loads of space. However, if that file ends up being corrupt for some reason, you will want multiple backups. Here’s how you add the date stamp to the file name:

mysqldump -udbusername -pdbpassword db_name | gzip > /home/cpanelusername/yourfolder/mysql-$(date +%Y-%m-%d-%H.%M.%S).gz

If you want to email the backup to another location, all you have to do is preceed the command with
MAILTO=yourbackupemailaddress (There is a space after your email address).
This will ensure that you have it stored somewhere else apart from your server.

How to Automate File Backups

Part two of this process is to back up your files automatically as well. For most blogs, the only folder you really need to worry about is the /wp-content directory where your plugins and theme customizations are stored. Depending one how often you are altering these files, you may not need to back them up more than once per week.

1. Create a file called file-backup.sh and store it in your root folder.

2. Add this to the file:

#/bin/bash
/bin/tar czPf /home/cpanelusername/etc/files/wp-content-weekly.tar.gz /home/cpanelusername/public_html/wp-content/

3. Schedule a weekly cron job for this backup:

/home/cpanelusername/file-backup.sh

Now you will have weekly backups of your /wp-content folder sent to your /etc/files directory. You can of course back up the files for your entire installation if you’d rather do that instead. It will continually overwrite the last backup, which I think is preferable, unless you’d rather add the date stamp:

#/bin/bash
/bin/tar czPf /home/cpanelusername/etc/files/wp-content-weekly-`date "+%Y%m%d%H%M"`.tar.gz /home/cpanelusername/public_html/wp-content/

Thanks go out to Franky Branckaute for his helpful post on this subject and instructions for scheduling the cron jobs. I hope you take a few minutes to set up automated backups right now so that none of your data is lost in the new year! If you’re having trouble setting up cron jobs, I’m sure your hosting provider will be glad to assist you. There are some WordPress plugins available to help you perform regular backups if you’re having issues setting these up, but implementing your own cron jobs offers the most flexibility as far as when, how often, file type, folder location, etc. You’re better off setting these up yourself.

Featured Plugin - Every great SEO tweak you need, in one snazzy bundle

Fully integrated with the SEOMoz API, complete with automatic links, sitemaps and SEO optimization of your WordPress setup - this is the only plugin you need to help you rank your site number 1 on Google - nothing else compares.
Find out more

Featured Plugin - Add bottom corner (or anywhere else) chat to your site

No javascript required, no third part chat engine, just fully featured chat right in your own database on your own WP sites - couldn't be easier.
Find out more

Featured Plugin - Easily integrate your WordPress site with Facebook

Would you like to add Facebook comments, registration, 'Like' buttons and autoposting to your WP site? Well, The Ultimate Facebook plugin has got that all covered!
Find out more

Featured Plugin - Open an Online Store with MarketPress

Out of all the WordPress ecommerce plugins available, this has got to be the winner - easy to configure, powerful functionality, multiple gateways and more. A simply brilliant plugin!
Find out more

Featured Plugin - Send beautiful html email newsletters, from WordPress!

Now there's no need to pay for a third party service to sign up, manage and send beautiful email newsletters to your subscriber base - this plugin has got the lot.
Find out more

Featured Plugin - Start Your Own Powerful Membership Site

If you're thinking about starting a paid, or just private, membership site then this is truly the plugin you've been looking for. Easy to use, massively configurable and ready to go out of the box!
Find out more

Featured Plugin - Turn any WordPress page into a fully featured wiki!

To get a wiki up and running you used to need to install Mediawiki and toil away for days configuring it... not any more! This plugin gives you *all* the functionality you want from a wiki, in WordPress!!!
Find out more

Featured Plugin - Start your own Quora / StackOverflow / Yahoo Q&A site

It's now incredibly easy to start your own Q&A site using nothing more than WordPress - The Q&A plugin simply and brilliantly transforms any site, or page, into a perfect support or Q&A environment.
Find out more

Featured Plugin - WordPress + Google Maps = Perfect

Simply insert google maps into posts, sidebars and pages - show directions, streetview, provide image overlays and do it all from a simple button and comprehensive widget.
Find out more

9 Responses to New Year’s Resolution: Automate WordPress / WPMU Backups. Check.

  1. Excellent post, I am still shocked that backing up isn’t built in to WordPress – this is something that is so important, and I bet at least 99% of the WordPress blogs out there aren’t backed up.
    I do want to add, you really NEED to make sure those backup files go somewhere other than your web server. You mentioned mailing them to you and that would be great, you could even just set up a dedicated gmail account to receive the backups.
    If you only have backups on your server, that’s not going to do you much good when (not if, when) your server crashes or your hosting company shuts down your account.

  2. Thanks for this. I found it very useful.
    A few points for anyone not using cPanel who is having difficulty with this
    - I had to use /usr/bin/mysqldump
    - I had to include the -h switch for hostname as well as -u and -p
    - I had to escape the %s with backslashes (/% instead of %) to get the datestamp to work.

    Hope that might help someone else to get it working. This will save me shedloads of time on client sites.

  3. I rsync files to Amazon S3 nightly, with a report sent to my email. Also daily db exports go to S3 and kept for a week.

  4. I meant, of course, \% instead of %

  5. @Aaron – I’m sure you’ve thought of this, but files sent to Amazon S3 are accessible to anybody, so they need to be encrypted unless you want the world to see them :)

  6. I use Auto MySql BackUp http://sourceforge.net/projects/automysqlbackup/ It’s a cool shell script that uses mysqldump. It has some great features like backup a single database, multiple databases, or all the databases on the server; each database is saved in a separate file that can be compressed (with gzip or bzip2); it will rotate the backups and not keep them filling your hard drive (as normal in the daily backup you will have only the last 7 days of backups, the weekly if enabled will have one for each week, etc.).

    @aaron how do you use s3 with rsync? I never seen that feature in rsync.

  7. I am new to WordPress MU so please excuse the very basic nature of my question/s. (We were forced into installing WordPress MU ourselves as we are a small school within China and many of the other blogging options we have used have been progressively blocked.)
    We have WordPress MU installed on a Windows server – not Unix. Will this backup procedure still apply?
    Thanks

  8. Pingback: wp-popular.com » Blog Archive » Automate WordPress / WPMU Backups. Check. – WordPress MU and BuddyPress plugins, themes, support, tips and how to’s

  9. Pingback: Make Regular Custom Backups of Your WordPress Site for FREE with the XCloner Plugin

Click on a tab to select how you'd like to leave your comment

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting