Posts Tagged ‘Joomla’

Joomla content backup using php

editphp / programmingcommentsNo Comments »
May 3rd 2008

A while back one of my project was to create a joomla based content management website. Afterwards I was asked to make a backup mechanism that would back up all of the content on the website. There were plenty of third party plugins available on joomla’s extension directory but I found them either too complicated for normal users to operate or costed money. Another option was to use PhpMyAdmin to backup the entire joomla database, however that was too
complicated for the user in this situation as well. Therefore I decided to write a custom php script made just to handle the content backup.

Some file you need download: (PHP ZIP library: PHP zip library, contains: Zip.php and PEAR.php)

1st: I had to find where the content was stored, luckily for joomla based CMS all contents are stored in the database under the table “jos_content” or whatever the pre-fix: “xx_content” in the joomla database you created.

2nd: I had to create the basic MySQL connection script: here are the following:

openning the database:

//********************************************************************************
//opendb
$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');

mysql_select_db($dbname);
//********************************************************************************

configure the database information: Simple enter the MySQL database information

//*******************************************************************************
//configuration file
$dbhost = 'localhost';
$dbuser = 'username';
$dbpass = 'password';
$dbname = 'db_name';
//end of configuration file
//*******************************************************************************

Close connection:

//*******************************************************************************
//close connection
mysql_close($conn);
//*******************************************************************************

Here are the basic scripts for openning and closing a mySQL database connection in php, for more information on them, visit w3schools to learn more

Now comes the meat of the script:

//************************************************
//include the php library for zip file compression
//************************************************
include ('Zip.php');

//*************************************************************************************
//Querying the database table containing the contents of the website, notice we used a
//concat function to combine the introtext and fulltext together
//*************************************************************************************
$query  = "SELECT id, title, concat(introtext, concat('\n', `fulltext`)) content FROM jos_content";
$result = mysql_query($query);

//*****************************************************************
//Displaying the SQL querying result in html using a form text area
//*****************************************************************
echo "";
echo " 

$number files created under /help/backup

"; //providing download link echo "Click here to download the backup package";

As some of you can tell, this script is still in very immature stage, future updates could include SQL injection prevention measures, validation of the backup directory and also deletion of the html files once they have been packaged. Anyways I hope this have been useful.