Using a Cron job to keep things safe
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 ?
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.
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.
License:
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).
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).
Disclaimer:
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".
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.
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.
+ + + + +
NOTE
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.
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."
This is the 1.5.2 version
This is the 2.0 version
See the 'Blog Name:' bit ? Put something there to identify it for you. Useful if you have multiple cron running.
You can download the 1.5.2 script here: http://www.tamba2.org.uk/wordpress/cron/cronjob.sh
You can download the 2.0 script here: http://www.tamba2.org.uk/wordpress/cron/cronjob2.sh
In a nutshell it says this: "Get this database, save a copy, compress it, mail it, delete the copy."
This is the 1.5.2 version
#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 ?
DBNAME=DB_NAME
DBPASS=DB_PASSWORD
DBUSER=DB_USER
#Keep the " around your address
EMAIL="you@your_email.com"
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME wp_categories wp_comments wp_linkcategories wp_links wp_options wp_post2cat wp_postmeta wp_posts wp_users > 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
This is the 2.0 version
#Set the 4 variablesAll you need to do is tell it which database and where to send it. Easy :)
#Replace what is AFTER the = with the information from your wp-config.php file
#That's your information on the right okay ?
DBNAME=DB_NAME
DBPASS=DB_PASSWORD
DBUSER=DB_USER
#Keep the " around your address
EMAIL="you@your_email.com"
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME wp_categories wp_comments wp_linkcategories wp_links wp_options wp_post2cat wp_postmeta wp_posts wp_usermeta wp_users > 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
See the 'Blog Name:' bit ? Put something there to identify it for you. Useful if you have multiple cron running.
You can download the 1.5.2 script here: http://www.tamba2.org.uk/wordpress/cron/cronjob.sh
You can download the 2.0 script here: http://www.tamba2.org.uk/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 that these will arrive frequently. Be sure that your email account can accommodate these. (Finally ! A use for GMail !)
Compressing the whole database
You may want to backup the whole database. I do not recommend that you do this if you are running any program that writes logs to the database - there is no point in backing up logs and it creates more work for the server. Logs such as these can get large very quickly. However, if you are running such code as Vanilla or Scuttle, saving the whole db is a good idea. In this case, you need to make a change.
Make this:
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 that these will arrive frequently. Be sure that your email account can accommodate these. (Finally ! A use for GMail !)
Compressing the whole database
You may want to backup the whole database. I do not recommend that you do this if you are running any program that writes logs to the database - there is no point in backing up logs and it creates more work for the server. Logs such as these can get large very quickly. However, if you are running such code as Vanilla or Scuttle, saving the whole db is a good idea. In this case, you need to make a change.
Make this:
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME wp_categories wp_comments wp_linkcategories wp_links wp_options wp_post2cat wp_postmeta wp_posts wp_users > backup.sqlInto this:
mysqldump --opt -u $DBUSER -p$DBPASS $DBNAME > backup.sql
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:
Change the file permissions to 711
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:
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
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.
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
/home/name/public_html/blog/wp-images/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.
Your host should offer a way of accessing and setting cron jobs. The screenshots here are for CPanel.
Click.
Click Standard
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
- I'm still not sure about cron jobs. Will you do it for me ?
- No. Talk to your host, post in their support forums.
- How often can I run this ?
- Ask your host. I can't see why you would want to run it more than once a day.....
- I've stayed up til midnight and nothing has happened !!
- Heh, I did this too. The script kicks in when it gets to midnight on the server. Look in your WordPress options for the Times in the weblog should differ by: and work out when it should arrive
- Can I test it sooner ?
- Yes - just alter it to work "Every 15 minutes" and "Every hour". It will then send one fairly quickly. Remember to change it back !
- Can I leave the copy on the server too ?
- Yes. Remove the line rm backup-$DATE.sql.gz
Be sure to delete old ones regularly - this is a very fast way to fill server space !
- Yes. Remove the line rm backup-$DATE.sql.gz
- How do I backup more than one database ?
- You would need to run one cron job per database BUT the script would also need to take account of this and name the saved files appropriately. If you need this, I suggest you look at automysql for your needs.
- It does not work.
- The script tries 2 methods to send the email. If you do not get a backup file, and instead you get an error message, ask your host for advice. I am unable to advise on error messages. I have tested this script many times on my database and have had no untoward effects. Before looking for any help anywhere, check that you have done everything here.
I know... you are thinking how wonderful this is and how you can rest safely knowing that your data is safely backed up. Well while you feel that way, you could always send a small contribution as a gesture of your appreciation ... once costs for the site are met, it will probably go to running costs for me so I can continue to to produce useful nuggets such as this page. Thank you if you do, and either way - keep those backups safe !
评论