CRON Jobs Give WordPress Users Peace of Mind

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

Reminder: Don’t Forget to Backup Your WordPress Database!

This wasn’t going to be the subject for my post today, but a series of events have changed my mind. Here they are:

Yesterday on my blog, WPShout I published ‘10 Awesome Things to Do With WordPress’ Custom Fields‘. This morning I awoke to find not a single comment on the post. I was disappointed as the post had taken ages, but I didn’t think much more of it. Until this evening. I wanted to email a friend a link to the post, so I loaded up WPShout, only to find the post wasn’t there! In the admin was only my draft from a couple of days ago. Odd, I thought. I copied and pasted the post from Google Reader and republished the post. And then I realized that a heck of a lot of comments I’d spent yesterday evening replying to had gone, and so had my replies. In other words, my database had reverted to a version a couple of days old. Why? I don’t know (if anyone does have any idea, could you drop me an email?!) at this point.

Of course, at this point you’re (probably not) screaming at your monitor

“just restore the backup you’ve got!… you, you do have a backup, right?!”

Yes. Of course I did. Or so I thought. I’d set up the WordPress Database Backup plugin to email me a backup of the database every 24 hours, and that email automatically got archived. Which meant I didn’t see it hadn’t been sent for a couple of weeks because when moving domains I’d forgotten to reinstall the plugin. Which meant I didn’t have a backup.

Where this post is going is simple – don’t be an idiot like me and only realize your backup doesn’t exist when you actually need it, spend five minutes now installing the plugin I mention above and set it up to email you every day. Just don’t archive the email automatically. WordPress Hacks Top Tip: don’t be an idiot….always have a backup.

How To: Moving WordPress to Another Server

Yesterday I wrote a post explaining how to move your WordPress install within your website/server.  Today I wanted to cover how to move your WordPress install to a completely different server.

Again, the flexibility of WordPress shines, making this not overly difficult to accomplish.  If you aren’t changing your domain name, all you need to do is update your wp-config.php file and upload all of your files to their new server.

If you are changing your domain name with your move, here is the information you’ll need:

  1. Backup your WordPress database.
  2. Download the complete WordPress install to your hard drive and identify the folder as your OLD installation.
  3. Login into your old blogs dashboard and update the Settings to reflect the website and blogs new location (both fields should be the same).
  4. Now, download the complete WordPress install to your hard drive, but this time identify the folder as your NEW installation.  This will include the settings change you just made.
  5. Download a copy of your WordPress database (keeping the old one) and then upload it to the new server.  You’ll want to keep the same database name and recreate the user login information (you can use your same user name and password).  If you change the database name, you’ll need to update your wp-config.php file to reflect the change.
  6. Upload your NEW installation folder so that your blog is now working in its new location!

If you want to keep your old blog, you’ll need to upload the OLD folder to the OLD location of your blog and readjust the General Settings tab.

Another way to accomplish all of this is to simply make a fresh WordPress install and export/import your posts to the new location.   This isn’t the most ideal method, but it is much easier and will get the job done.

For additional information, you’ll want to consult the WordPress Codex.

How To: Manually Backing Up Your WordPress Blog

In the past I’ve talked about some great WordPress plugins for backing up your database.   This is a great way to backup your WordPress blog for people that don’t know how to do it manually.   You can also set it up to do your backup automatically, which I find extremely useful because I run several websites that need backed up.

Throughout the past few years I been fortunate to meet a lot of bloggers, and I’ve found that many prefer to avoid plugins as much as possible, or sometimes they are just the kind of people that like to be hands on and learn how to do things themselves.   For these people, Performancing recently posted a Bloggers Guide To Safely Backing Up Your WordPress Site, where they detail the steps you need to take to manually backup your WordPress blog.

As a quick note, if you decide you want to manually backup your WordPress blog, you’ll first need to make sure you have FTP access for your website and the ability to access your Control Panel (CPanel).   From there you will want to follow the steps in Performancing’s guide to create a backup of your posts and a backup of your database.

How To: Avoid Blog Disasters With WordPress Database Backup

This guest post was written by Keith Dsouza of Techie-Buzz. Keith is a WordPress enthusiast who has contributed numerous WordPress plugins to the WordPress community. If you have WordPress knowledge and are interested in writing a guest post for Hack WordPress, please contact me.

Disasters are unavoidable and could happen any day, your server could have a crash one day and you will be left with nothing. Maybe your database will crash and you will lose all your posts. Your blog could be hacked too and someone may just delete all your posts. These are some things that could happen any day to anyone.

Taking precautions to safe guard your WordPress Blog is one of the best option to stay on top of it. The key to remember is that your database is the backbone of your blog, if you lose it your blog will be bankrupt, having a regular backup is they key to keep your blog up and running at all times.

WordPress is a platform that has many great developers working for it and providing with numerous amounts of plugins that could help to overcome the unavoidable.

WordPress Database Backup is a handy and blog saving plugin that will help you take daily backups of your database, you can either run the backup manually or setup cron tasks (scheduled tasks) to send you a backup of your database on a day to day basis.

The cron tasks have been introduced since WordPress 2.1 and if you do not have the latest version you should upgrade your WordPress installation first.

Below are some detailed instructions and tips on how to use the WordPress Database Backup plugin to setup cron tasks to automatically send you emails every day so you have the latest copy whenever you require it.

Step 1

Download and activate the WordPress Database Plugin for your blog.

Step 2

Click on the Manage option in the Admin panel navigation and from there click on the Backup link in the submenu.

Step 3

Once you are in the backup page, you will see a lot of options. Scroll down till you see an option to schedule a backup. In the scheduled backup you will see various options to schedule you backup.

You can schedule a backup varying upon how frequently you post on your blog. The best option is too choose a daily backup of your database. You will also be given an option to choose the database tables you want to backup. By default all core WordPress tables are backed up.

While making a choice to backup other tables you should only choose those that are critical to running of your blog, if you choose all the tables there are chances that the script may fail due to reading huge data.

If you want to backup all the tables the best option is to run the task manually and only email critical data to you every day.

That’s it with three simple steps you can save your blog from liquidation and always be sure that you are one step ahead of failures. You just have to follow the steps once and then you can ensure that you have a plan against disaster.