Manage Backups for WordPress Files and Databases

This is the continuation of a WordPress backup tutorial that ran last month. This part will discuss two important topics: executing the wordpressmysqlbackup project in your hosting cron, and managing backups and testing the integrity of WordPress backups in your local server. The latter is particularly important.

You can read the first part of this series here (http://www.devshed.com/c/a/Administration/How-to-Back-Up-WordPress-Files-and-Databases/).

Cron to Periodically Create WordPress Database Backups

This section assumes you’ve already filled the wordpressmysqbackup.php with your WordPress MySQL database access information. It also assumes that you’ve already uploaded the wordpressmysqlbackup folder to the define path on your web server (the one defined by $full_serverpath_to_backup).

The path of wordpressmysqlbackup is above the web root folder. The web root folder is where your WordPress files are installed, and can be accessed via web browser. Most hosting companies use  “www,” “html” and “public_html” as folder names for the web root folder.

Below is a screen shot of the sample correct location of the wordpressmysqlbackup folder in the hosting server:

The next thing we need to do is let cron execute this PHP script so that the WordPress database backup will be created automatically. Cron is a hosting feature that you can use to automate the execution of tasks at specified time intervals (daily, weekly, monthly, etc).

The following is a simple procedure (using Agilityhoster.com cron tabs, which might be similar to other hosting environments):

  1. Log in to your hosting control panel.
  2. Click the ”Crontab” feature.
  3. You will be asked for the command. The command is simply the full server path to the wordpressmysqlbackup.php.

Example: /home/wordpressmysqlbackup/wordpressmysqlbackup.php

It will then ask when to run the cron. If you need to run it weekly, simply set:

Minutes: 00
Hours: 00
Days: 7
Months: Every Month
Weekdays: Every Weekday

Then click “Set Cron Job”. It will then generate command such as:

0 0 7 * * /home/wordpressmysqlbackup/wordpressmysqlbackup.php

It is beneficial for you to configure the cron to send you an email every time it successfully executes the script. You will know from the details in the email whether or not the backup was successfully created.

For other hosting environments, such as Go Daddy, the method for setting up the cron is entirely different from the approach outlined above.

  1. Log in to the hosting control panel and go to Cron Manager.
  2. You need to assign a Cron Job title, Frequency (weekly e.g), Day, hour and the cron command.
  3. Since you are going to place the wordpressmysqlbackup file above the HTML webroot, the cron command (for PHP version 5) is:

/web/cgi-bin/php5 "$HOME/wordpressmysqlbackup/wordpressmysqlbackup.php"

In the example above, the path to WordPress index.php (which is under the HTML folder) is: $HOME/html/index.php

Here is a screen shot (for a cron to run the WordPress database backup weekly):

{mospagebreak title=Test Your WordPress Backups to Verify Integrity}

Suppose you already have a backup copy of both your WordPress database and personal files. Your next job is to assemble them together in a local server environment. Why?

The major reason is to test the integrity of your backups. A common mistake in doing backups is failing to test them to see if they work. Below are the important steps you’ll need to undertake to test WordPress backups.

1. Install XAMPP on your computer. XAMPP is a local Apache server that can run PHP and MySQL. It is  available for common operating systems, such as Linux and Windows.

If you are using Windows, you need to download XAMPP here: http://www.apachefriends.org/en/xampp-windows.html

Scroll down to the “Download” section and download the “EXE” version of XAMPP Windows 1.7.3 (Basic Package). This was the latest version at the time this tutorial was written. The installer is around 51 MB.

The installation is straightforward. If you are using a TrueCrypt container (this was discussed in the first part of this tutorial), create a separate container for XAMPP and install XAMPP to this container. For example, suppose you have a TrueCrypt virtual drive L: . XAMPP should be installed at the root of the virtual drive. So the installation path will be L:XAMPP.

The benefit of installing XAMPP in an encrypted container is added security for any passwords present in the scripts and database. They are not stored in clear text.

To learn some tips and techniques on the use of XAMPP in Windows, you can read this tutorial: http://www.aspfree.com/c/a/BrainDump/XAMPP-Tips-for-Running-an-ApacheMySQL-Server-in-Windows-XP/1/

If you are using Ubuntu/Linux, you can read this XAMPP installation tutorial: http://www.devshed.com/c/a/Administration/How-to-Install-XAMPP-on-Ubuntu-Linux/

Do not proceed to the next step if your XAMPP is not fully installed or not running properly. Try running a few basic PHP scripts to see if the test server runs properly.

2. Download the latest version of WordPress here: http://wordpress.org/download/. This should be the same version as the one you are using on your live site. If not, you should update your website to use the latest version of WordPress for security reasons.

3. Unzip the WordPress package. It will extract a folder named ”wordpress,” whose contents include the core files of WordPress:

4. Change the name of the folder from wordpress to the domain name of your website (e.g. yourdomain). Make it one word only.

5. Copy this renamed folder (containing the WordPress core files) to XAMPP htdocs.

6. In the first part, you backed up the following important and personal WordPress files: .htaccess, the wp-content directory, robots.txt and the other website files and directories that you created, except for the core WordPress files.

Let’s start integrating your wp-content directory with the wp-content folder contained in the core files. First, delete the wp-content core directory. Now replace it with your backup wp-content directory (the one containing your own WordPress theme files, plugins, uploads, etc).

Now copy and paste robots.txt, as well as your other non-WordPress files, into your local wordpress site root directory: /htdocs/yourdomain/, the same relative path used in your live site.

Do not yet include your .htaccess file.

7. Create a MySQL database for your local wordpress installation. Go to this URL: http://localhost/phpmyadmin/

8. Enter your MySQL root credentials (username: root and password: yourpassword).

9. Under  the”MySQL localhost” – “Create new database” text box, enter the name you plan to use for your MySQL database. Use the same WordPress database name you use in your live hosting account. Then click the “Create” button.

10. Log out of phpMyAdmin.

11. Go back to /htdocs/yourdomain/ and open wp-config-sample.php. Enter your local MySQL database credentials.

define(‘DB_NAME’, ‘Your wordpress database name’);
/** MySQL database username */
define(‘DB_USER’, ‘root’);
/** MySQL database password */
define(‘DB_PASSWORD’, ‘Your XAMPP MySQL root password’);
/** MySQL hostname */
define(‘DB_HOST’, ‘localhost’);

Save this as wp-config.php

12. Now in the browser address bar, install WordPress:

http://localhost/yourdomain/wp-admin/install.php

Input the site title, test password (not your actual password), your email, check “Allow my site…” then click “Install WordPress.”

13. If you can see “Success,” go back to http://localhost/phpmyadmin/ and then click the WordPress database until you see its tables:

14. You need to import your actual WordPress website’s MySQL database (the one you have backed up using the wordpressmysqlbackup PHP script).

The following are the detailed steps:

a. Click the “SQL” link.

b. You need to empty all WordPress database tables. Copy and paste the query below into the text box field in “Run SQL query…”

TRUNCATE wp_commentmeta;
TRUNCATE wp_comments;
TRUNCATE wp_links;
TRUNCATE wp_options;
TRUNCATE wp_postmeta;
TRUNCATE wp_posts;
TRUNCATE wp_terms;
TRUNCATE wp_term_relationships;
TRUNCATE wp_term_taxonomy;
TRUNCATE wp_usermeta;
TRUNCATE wp_users;

Click “Go.”

c. Click “Import” and then click “Browse.” Browse to the location in your hard drive where you have saved your WordPress MySQL backup. After locating the file, click “Open.”

d. Finally, click “Go.” This will import the your WordPress database into the XAMPP MySQL database.

15. Copy your actual WordPress .htaccess to your website root directory into XAMPP /htdocs/yourdomain/

Open the .htaccess. Revise this line from:

RewriteRule . /index.php [L]

TO:

RewriteRule . /yourdomain/index.php [L]

16. Now go to the wp_options table in your WordPress database using your XAMPP phpMyAdmin. Assign a new option_value for the following option_name:

a.)
Option name: siteurl

FROM:
Option_value: http://www.yourdomain.com
TO:
Option_value: http://localhost/yourdomain

b.)
Option_name: home

 

FROM:
Option_value: http://www.yourdomain.com
TO:
Option_value: http://localhost/yourdomain

17. Finally test your website backups if it is fully working, enter this URL in your browser: http://localhost/yourdomain

Try visiting your WordPress website pages to see if everything works the same as it does in your live site. 

WordPress Backup Management Tips

Now you have a fully running backup of your website in your local server. Here are some final tips.

  1. Any time you download a more updated version of your WordPress database backup (every week, for example, from the cron), load it up to your local server using phpMyAdmin and the techniques illustrated earlier.

  2. Any time you update the live site’s WordPress files, download only those files that you have changed (you need to do this weekly or monthly as an example). There is no need to download everything again. You can trace which files are modified by looking at the “Last Modified” column using your SSH client, such as Filezilla, in the live site’s hosting server.

  3. NEVER edit the WordPress core files. If you do, all of your customized changes will be lost during a WordPress upgrade, and can make your backup system (discussed in this tutorial) not so useful. Instead of editing core files, look for a WordPress plug-in which can give your site the functionality you seek.

This is a more efficient approach to doing WordPress backups. In case a disaster happens to the live site, you can confidently restore your website, because you have a fully working backup version.

Google+ Comments

Google+ Comments