This 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:
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.





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.
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.
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.
I meant, of course, \% instead of %
@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 :)
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.
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
Pingback: wp-popular.com » Blog Archive » Automate WordPress / WPMU Backups. Check. – WordPress MU and BuddyPress plugins, themes, support, tips and how to’s
Pingback: Make Regular Custom Backups of Your WordPress Site for FREE with the XCloner Plugin