Home | Write for Us | Archives | Advertise | About | Contact

RSS Feed | Email RSS

spacer

CRON Jobs Give WordPress Users Peace of Mind

By Jennifer Nodwell | Filed Under Hacks, Tutorials
Tag(s): CRON, WordPress Database Backup

spacer

This guest post was written by Jennifer Nodwell, a coder and website developer since 1997 who works almost exclusively with open source CMS applications like WordPress. If you have webmaster or WordPress knowledge and are interested in writing a post for WordPress Hacks, please contact us.

If you’ve never had a website go missing, then you’re very lucky. Your host can have a network error or hardware failure that loses your site’s files. A malicious hacker can penetrate your FTP server and replace your site with files of his choice. You might get curious about a new plugin, install it, and break your site to pieces in the process. No words really describe the panic you feel when you realize your website is gone!

If your host allows you to use CRON jobs, you can protect yourself from all those things by running a daily backup of your database and web site files. If you lose your site for any reason, you can put it right back in a few minutes.

A CRON job is script that your web server executes at a specified time. CRON comes from the word chronograph, and it is a time-based job scheduler for LINUX based operating systems. Most WordPress sites are running on such an operating system. If you have access to a CRON scheduler through your hosting control panel or have command-line access to your hosting server, you should be able to write a shell script to back up your database and site files.

The first thing you will need is a shell script. The script will connect to your database, export the whole thing to a file, and zip the file up so you can store it safely somewhere. Then, it will zip up all your website files.

If you have a host panel where you can create and edit a file, you can do this there. Otherwise, open any non WYSIWYG text editor and create a file called daily_backups.sh. The .sh file extension indicates this is a shell script file.

#!/bin/sh
TERM=linux
export TERM
NOWDATE=`date +%a` # Sets the date variable format for zipped file: Sun
clear # clears terminal window
echo
echo "Hi, $USER!"
echo
mysqldump --opt -Q -h your-website-host-name --user=your-db-user-name --password=your-db-password your-database-name | gzip -v9 - > /www/public_html/backups/MySQL-$NOWDATE-yourwebsite.sql.gz

The first line is an indicator to the server for which shell processing language to use. Your server might require

#!/bin/bash or something else there. Use echo $SHELL from the command prompt to determine your shell type if you have command line access.

The TERM instruction tells the server what terminal type it is communicating with; in this case, a text terminal.

Next we create a variable named NOWDATE and use a little script magic to set it to be equal to the abbreviation for the current day of the week e.g. Sun, Mon, Tue, etc.

The mysqldump command will “dump” the entire database including create procedures and insert procedures for all the current data.

Replace your-website-host-name with your website host name for your database. You can find this in your wp-config file if you don’t know it.

Replace your-db-user-name, your-db-password, your-db-name with actual values. Again, these values are likely exactly what they are in your wp-config file.

Replace /www/public_html/ with whatever your hosting account’s root path is.

Replace yourwebsite with some meaningful name.

Now, save the file. You need to set the permissions on the file so that it is executable. If your cpanel has a cron scheduler, just add this file to the list of files it runs. If you need to edit your crontab from command line, see this tutorial.

To back up your site files, as well as your database, add a few more lines to your script.

echo
echo "Zipping wordpress directory structure..."
tar -czf $HOME/backups/$NOWDATE.mywebsite.tar.gz $HOME/public_html/*

Hopefully, you do have a directory outside your public web root so that your backup files are stored in a location that is not accessible via the internet.

The tar command will pack the files up into a tarball for you (like a zip archive).

Want automatic updates? Subscribe to our RSS feed or
Get Email Updates sent directly to your inbox!
Tweet This | Like Us! | Digg This | Stumble it | | Print This |
spacer

Jennifer Nodwell

Jennifer worked as a systems analyst and developer on large-scale scale systems at EDS and Nortel where she wrote over 2 million lines of code. She has been building websites since 1997, and after realizing there was life beyond the cubicle, chucked the corporate life for free-lancing as a web developer in 2005. Now, she works almost exclusively with open source CMS applications like WordPress.

Related WordPress Posts

  • Hack WordPress NCAA Tournament Challenge
  • Join the WordPress Hacks NCAA Tournament Challenge
  • Huge Compilation of WordPress Code

There Are 6 Responses So Far »

  1. spacer Hillary Bost says:
    May 2, 2011 at 1:25 pm

    That is good to know. Thanks for the update. Things should run a bit smother now.

    Reply
  2. spacer Shubham Dev says:
    May 15, 2011 at 10:01 pm

    Seems a bit complicated to me, but I’ll try to figure it out soon! Thanks for sharing btw spacer

    Reply
  3. spacer Shah Zina says:
    May 23, 2011 at 7:37 am

    Thank you so much for such a nice and complete information mostely blogger provide uncomplete information which i found here after half hour browsing. Thank you so much.

    Reply
  4. spacer Peter Yoga says:
    June 2, 2011 at 2:18 am

    I will do this today – thanks!

    Reply
  5. spacer Celeste says:
    July 7, 2011 at 8:32 am

    Useful tips to keep file security
    thanks ..

    Reply
  6. spacer Nishant says:
    September 2, 2011 at 8:28 am

    thanks for the tut, as said by Celeste, defiantly useful tips for security.

    Reply

Trackbacks/Pingbacks »

  1. CRON Jobs Give WordPress Users Peace of Mind « mefaba.com says:

Leave a Reply

Click here to cancel reply.

gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.