跳至主要内容

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 ?

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.

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).

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".

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.

+ + + + +

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.

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
#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 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_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
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 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:
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
Into 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:

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
/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.

cron image

Click.

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
  • 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 !
  • 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 !

评论

此博客中的热门博文

How to Design a Store Front Sign

In this Article we will talk about designing a store front for a business. What color or size should your Letters & designs be? What to put on your sign and why?, then be ready to Get in touch with sign people and get your best deal. Large lettering with out a front sign could save you time and money Do your products have a special shape or color to emulate on your sign? Kodak is always in yellow and black, Coca-Cola white on red, Etc. Muffler places, have a sign in the shape of a muffler. your phone number on the store front some place, but not on the identification sign, (door, or window) Your Product & logos can be arranged on the front sign, or window. Not on the Pole sign, Store hours & phone number could fit in your design and by using the product color, it could add product recognition & enhance your design. Suppliers may provide plenty of advert...

music forum (zt)

24. moonlight - ≡ Sound Of Nature ≡ -     15. Tone Quebec Network 17.Ukoo http://www.hispeed.com.cn/Forum/LoadForum.asp?Foru m / a> http://bbs.dd81.com/index.php 30. Jazz pawnshop http://www.sogua.com/ 6. Lyrics 吾爱 http://www.inkui.com Reading of the singer in mind 2. Hyun tone http://d.sogou.com/ http://www.beihai365.com/bbs/forumdisplay.php?f/a> 20. Left Bank • Teana http://bbs.flamesky.com/ 8. Sogou sogou- 25.U Shadow 吾爱 sound waves 4. Need 14.HDCD http://www.commus.com/ 29, Buddhist music, do not know the name of the http://www.itpub.net/forum79.html http://bbs.kugoo.com/index.asp http://bbs.uying.com/176/Index.aspx http://board.verycd.com/ http://board.verycd.com/f8.html 13.MP3 barrage http://bbs.51lrc.com/index.asp?board/a> A pleasant song - "Dancing circulation" 19.Verycd of http://bbs.breezecn.com/ http://www.mp4cn.com/2008/index.html Sound of Music Forum, http://www.jazzsky.com/lb/cgi-bin/leobbs.cgi http://www.luopo.co...

找到一个可以替代ghs.google.com的地址

用Goole Blogger的自定义域名功能需要用到 ghs.google.com 做CNAME,或者用ping ghs.google.com 得出的IP做一个A记录.但是,伟大的GFW已经把 ghs.google.com 和部分IP屏蔽在大陆之外.这样的壮举使得我们的自定义域名不能正常访问,或者让你的网页背景.图片被滤掉,精心做成的模板变得惨白不堪. 那么,我们可不可以找一个替代 ghs.google.com 的地址呢?当然能.因为Google是强大的.它有许多有用的IP地址让你去探寻. 对 ghs.google.com 分析研究后发现, ghs.google.com 这台服务器并没有提供实质性的服务,而只是选择访问者访问最快的服务器(Google全球性公司,很多地区都有服务器).我们只要找到其他服务器的IP地址,并且这个IP地址没被屏蔽,那么就成了. 寻找这样的IP地址需要用到tracert命令.即在CMD模式下输入:tracert ghs.google.com ,在已经屏蔽的当下,最后自然得不出结果,这就需要我们使用代理了.使用不同地区的代理运行tracert ghs.google.com 命令. tracert ghs.google.com 的最后一跳会有类似这样的域名: eh-in-f121.google.com 如果在不用代理的情况下,你能ping通这个域名,那么恭喜你,你找到了!现在就可以用这个域名代替 ghs.google.com 设置你的CNAME了,ping出的IP地址可以做A记录,CNAME和A记录,只做其中之一就可以. hs-in-f121.google.com 64.233.179.121 eh-in-f121.google.com 72.14.207.121 bx-in-f121.google.com 66.249.81.121 ik-in-f121.google.com 66.249.91.121 这4个在我所在地区不能使用,而可以使用 tw-in-f121.google.com 72.14.235.121 或者209.85.171.121也可用 下面是一些不断更新的:74.125.43.121 除了使用代理来寻找,也可以通过一些网站提供的功能来寻找这个地址,很方便,就是使用网站提供的Traceroute.这些网站有: h...