What it does:

At midnight every night, the core WordPress tables are backed up, compressed and emailed to you. The backed up file is then deleted from the server.

For the script to work, you must have MUTT installed on your server. Ask your host about this.
Known hosts on which this works: A Small Orange, EMax hosting and Site5. If you know of any others, please let me know ?

Why it does this:

The only backup script I could find that did what I wanted was automysql. Problem was that this backed up the entire database and I wanted to exclude tables involved with stats and spam. There may well be a way to modify automysql but I could not find it. So I put this together.


This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
(See the download).


I am not responsible if this does anything wrong. Before you install code you should be either familiar with what it does and how, or you should ask someone you trust - with a cron job asking your host is a "Good Thing".

Cron Job ?

http://en.wikipedia.org/wiki/Cron explains. If you are unsure about Cron, or you need to know if you can run Cron jobs, talk to your host - only they can answer.

+ + + + +


This ONLY backs up those tables which were created when you installed WP. If keeping all your stats, all your anti-spam settings, your user-online tables, your mostaccessed tables etc etc etc is important, just go use automysql - it is more fully featured. I'm working on the principle that if something catastrophic did happen then all you really want is your posts, comments and links.

The script

In a nutshell it says this: "Get this database, save a copy, compress it, mail it, delete the copy."

#Set the 4 variables
#Replace what is AFTER the = with the information from your wp-config.php file
#That's your information on the right okay ?




#Keep the " around your address

mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME > backup.sql
gzip backup.sql
DATE=`date +%Y%m%d` ; mv backup.sql.gz $DBNAME-backup-$DATE.sql.gz
echo 'Blog Name:Your mySQL Backup is attached' | mutt -a $DBNAME-backup-$DATE.sql.gz $EMAIL -s "MySQL Backup"
rm $DBNAME-backup-$DATE.sql.gz
All you need to do is tell it which database and where to send it. Easy :)

See the 'Blog Name:' bit ? Put something there to identify it for you. Useful if you have multiple cron running.

You can download the 2.0 script here: http://www.romanticrobot.net/wordpress/cron/cronjob2.sh

Setting up the script

Complete the details

The script is configured for the default 'table_prefix' of wp_
If you changed that prefix at the time of installation, you will need to change it in the script.

Then complete your email details. Remember that you could be getting emails that are over 1MB in size and increasing, and that these will arrive frequently. Be sure that your email account can accommodate these. (Finally ! A use for GMail !)

Uploading the file
You could put this cronjob file in any directory on your website BUT DO NOT.
If you put this in your main directory or a sub-directory (which could be www / htdocs / public_html / mainwebsite_html) it is readable in a browser. Which means I can download it and find out how to access your database. So unless you want to take that risk, DO NOT UPLOAD THIS INTO YOUR MAIN DIRECTORY.

You need to upload the file above your main directory. That way it is not accessible from a browser.
The 'etc' directory is one place for this to go. I'll use my site as an example:
When I login by ftp, I see these:

cron image

Click the indicated directory and upload your cron file into it.

Change the file permissions to 711

Now you need to get the PATH for your file.
Login to your blog, click Options, then Miscellaneous. Look at what is in the box for this Destination directory:. It should look something like this
The information you need is everything before the public_html.
Remember - on your host it might be www or htdocs or mainwebsite_html or something very similar.

Your path will therefore be /home/name/etc/cronjob.sh
If you unsure at all, ask your host for the PATH to the directory where you uploaded the file.

Setting the cron job
Your host should offer a way of accessing and setting cron jobs. The screenshots here are for CPanel.

cron image


cron image

Click Standard

cron image

That line of information you just copied ? Paste that where shown.

Now check the other boxes - I get a backup every night at midnight so if you want the same, make the same options highlighted. (If you are considering running the cron job more frequently than once every 24 hours, you must read the Q & A section at the bottom of this page).

Click the button beneath that says "Save Crontab"

That's it - all done !

Q & A


These pages are independent of http://wordpress.org
All design, content & images © Mark 2004-2015. All rights reserved.