A Small Script to Create a Database Dump for a Joomla Website

We get occasional requests from customers who don’t have access to a phpMyAdmin instance asking us to backup their whole database. Since we, at itoctopus, want to make the life of every Joomla website administrator easier, we are releasing the code for doing this, for free, for anyone to use! The code is below:

	ini_set('max_execution_time', 3600);
	$config = new JConfig();
	$dbFile = md5(microtime()).'.sql';
	exec('mysqldump --user='.$config->user.' --password='.$config->password.' --host='.$config->host.' '.$config->db.' > '.$_SERVER['DOCUMENT_ROOT'].'/'.$dbFile);
	echo('<a href=http://'.$_SERVER['SERVER_NAME'].'/'.$dbFile.' download>Download Database Export File</a>');

All you need to do is to place the above code in a file called dbdump.php and put that file under the root directory of your Joomla site. Once you do that, just point your browser to http://yourjoomlawebsite.com/dbdump.php and you’ll be presented with a link to download the database backup (please note that it might take some time for the link to appear).

Note that the dbdump.php file must be under the root directory of your website and must be at the same level of the configuration.php file. If your configuration.php file is stored elsewhere, then you will need to hardcode the database settings into the script.

Now, here’s a little FAQ on this script:

  • Will it work on all databases?

    No – it won’t. It’ll only work on MySQL (which is the database powering over 90% of Joomla websites anyway), this is because it uses the MySQL tool called mysqldump. Other RDBMS’s have similar tools, although not as easy to use. Contact us if you’re using another RDBMS (such as Oracle, MySQL, or PostgreSQL) and we’ll create the export script for you.

  • Is it reliable?

    Yes – it is very reliable and you will be able to replicate your current database based on this export.

  • Will it take a long time to execute?

    That depends on the size of your database and your server’s horsepower. For small websites, it can take less than a second to finish. For very larges sites, it might take an hour or more (note that you will need to modify max_execution_time in the script to a higher value if your database is quite large).

  • Will it work on all Joomla versions?

    The script will work on Joomla versions 1.5.x, 1.6.x, 1.7.x, 2.5.x, and 3.2.x (3.5 is not yet released at the time of publishing this article). It will not work out of the box on Joomla 1.0, but it will work if you hardcode the database information in the script (e.g. replace $config->user with the actual user, etc…).

  • Will it work on Windows?

    We have not tested the script on Windows, but theoretically it should. If it doesn’t, then a small modification to the script should make it work.

  • Will it always work in a LAMP environment?

    On some servers it won’t. This is because the script uses the exec function, which is a dangerous function that is blocked on some servers. In case you’re wondering, if this function is blocked then it is typically blocked in the php.ini file (there’s a disable_functions variable there that is used to block dangerous PHP functions).

  • Will we help in case you need help?

    Definitely! That’s why we’re here for! All you need to do is to contact us! Please note that our extremely affordable fees apply.

2 Responses to “A Small Script to Create a Database Dump for a Joomla Website”
  1. Comment by Paul — January 29, 2018 @ 7:03 am


    This is a very useful piece of code – thanks – but I had to change the exec string before it worked on my site. As it is, the database backup filename is being created in the folder above the site root folder, in my case /var/sites/mysite/public_html/ as a file called /var/sites/mysite/public_htmlmd5generatedfilename.sql instead of /var/sites/mysite/public_html/md5generatedfilename.sql. The generated link tries to retrieve the file but since the file is created above the site root, public_html, the file can not be downloaded although it is successfully created and can be retrieved by ftp. The existing code would probably work if the php file was stored in a subfolder of the site but you specifically say to place the php file in the root folder. To get this to work I had to modify the exec command to add the forward slash which creates the sql file in the site root like so:

    Old Version:$_SERVER['DOCUMENT_ROOT'].$dbFile
    New Version:.$_SERVER['DOCUMENT_ROOT'].'/'.$dbFile

    Perhaps you could update the article to suggest the php file is stored in a site subfolder or change the code to include the forward slash as above? Thanks for the code, it helped me a lot.

  2. Comment by Fadi — February 13, 2018 @ 1:08 pm

    Hi Paul,

    You are correct! The post has been updated to reflect your suggestion/correction.

    Thank you very much!

Leave a comment