Locally mirror your Joomla website E-mail
Sunday, 18 October 2009 08:41

In much the same way as I used scripts to mirror my WordPress blog to my local machine, I use the following procedure to backup the Joomla folders on my remote VPS and restore them to my local system.

Before you start, you'll need two pre-requisites to make sure the process is smooth.

  1. A local install of Apache, PHP and MySQL
  2. A shared SSH public key to allow password-free administration of your remote server

The backup

On the server machine, you'll need the following script called "backup-joomla", which I've placed in a folder called scripts under the home folder of the backup user account. Replace <root> with the root password for the MySQL database, <password> with the password and finally <database> with the name of the joomla database, most likely "joomla".

#!/usr/bin/env bash

db_user="<root>"
db_password="<password>"
db_name="<database>"

# Backup the Joomla database
mysqldump -u ${db_user} --password=${db_password} ${db_name} > /tmp/joomla-db.sql
cd /tmp
tar -jcf /tmp/joomla-db.tar.bz2 joomla-db.sql
rm -f /tmp/joomla-db.sql

# Backup the Joomla app
cd /var/www/html/blog
tar -jcf /tmp/joomla.tar.bz2 *

This uses mysqldump to write the joomla database to an SQL file, then creates two bzip2 compressed files, on containing the SQL we've just dumped and the second the complete directory structure for the joomla web application. Both files are placed into the /tmp folder with known filenames where the client machine can find them to download in the second part of the backup process.

Don't forget to set the execute using the following command.

chmod u+x backup-joomla

On the client machine, you'll need the following script also called backup-joomla, which I've placed into the scripts folder under my home account. Replace <user> with the name of the user that you've shared the SSH public key with on the server, and <server> with the URL or IP address of your remote machine, and <backup folder> with the folder on your local machine that you want to store your backups in.

#!/usr/bin/env bash

server="<user>@<server>"
backup_path="<backup folder>"

# Execute the backup script
ssh ${server} "./scripts/backup-joomla"

# Copy the files to the local machine
scp "${server}:/tmp/joomla*.tar.bz2" /tmp/

# Move and rename the backup files
filedate=`date '+%Y%m%d-%H%M'`
mv /tmp/joomla-db.tar.bz2 ${backup_path}/joomla-db-${filedate}.tar.bz2
mv /tmp/joomla.tar.bz2 ${backup_path}/joomla-${filedate}.tar.bz2

The script simply executes backup-joomla on the server then copies the resulting bzip2 files to the local temp folder using scp. I chose bzip2 from amongst tar's support compression algorithms because it compresses better than the others on offer, though it is potentially slower to compress and more taxing on the CPU.

The final step is to rename the files to include the create date and time as part of the file in order to keep multiple snapshots of the database in the same location.

The restore

With the backup safely copied to your local machine, it's now time to restore it, which involves adding the following script, restore-joomla, to your scripts folder. As with the previous scripts, <backup_path> is the folder where you store your joomla backup files, <local www> is the folder into which you'd like your local copy of Joomla to be installed, while <db_user>, <db_password> and <db_name> are the credentials necessary to connect to your local MySQL install.

#!/usr/bin/env bash

backup_path="<backup folder>"
joomla_path="<local www>"
db_user="<root>"
db_password="<password>"
db_name="<database>"

# Get the most recent backup files
filename=`ls -t ${backup_path}/ -I *-db-* | awk 'NR<2'`
filename_db=`ls -t ${backup_path}/*-db-* | awk 'NR<2'`

# Restore the database, assumes the DB has already been created in MySQL
tar -C /tmp/ -xvf ${filename_db} > /dev/null
mysql --user=${db_user} --password=${db_password} ${db_name} < /tmp/joomla-db.sql

# Extract the joomla backup into the destination folder, overwriting whatever is already there
tar -C ${joomla_path} -xvf ${backup_path}/${filename} > /dev/null

# Allow the currently logged in user to edit the Joomla configuration file
chmod u+rw ${joomla_path}configuration.php

# Disable URL rewriting
cat ${joomla_path}configuration.php | sed -e 's|^.*var $sef =.*$|\tvar $sef = '\''0'\'';|' > /tmp/tmp.php
mv -f /tmp/tmp.php ${joomla_path}configuration.php
cat ${joomla_path}configuration.php | sed -e 's|^.*var $sef_rewrite =.*$|\tvar $sef_rewrite = '\''0'\'';|' > /tmp/tmp.php
mv -f /tmp/tmp.php ${joomla_path}configuration.php

# Update the paths
cat ${joomla_path}configuration.php | sed -e 's|^.*var $log_path =.*$|\tvar $log_path = '\'$joomla_path'logs'\'';|' > /tmp/tmp.php
mv -f /tmp/tmp.php ${joomla_path}configuration.php
cat ${joomla_path}configuration.php | sed -e 's|^.*var $tmp_path =.*$|\tvar $tmp_path = '\'$joomla_path'tmp'\'';|' > /tmp/tmp.php
mv -f /tmp/tmp.php ${joomla_path}configuration.php

# Disable SSL for logging into admin
cat ${joomla_path}configuration.php | sed -e 's|^.*var $force_ssl =.*$|\tvar $force_ssl = '\''0'\'';|' > /tmp/tmp.php
mv -f /tmp/tmp.php ${joomla_path}configuration.php

This script will find the most recent database and Joomla files that have been downloaded from the server, then extract and install them.

The latter part of the script is used to tweak the Joomla configuration.php file so that it doesn't use "pretty" URLs, and to re-point the log and tmp folders to their new locations.

There are still additional things that could be done, such as disabling any plugins that might send data to online servers, but apart from that, you've now got a local mirror of your Joomla server, which you can use to develop, debug or just try things out on.

If you now navigate to your local Joomla folder in your browser of choice, you should be looking at a clone of your remote server.

blog comments powered by Disqus