paul's picture

 Recently I was asked to clone a drupal 6 sub-site. They wanted a clone of their production site so that they could do some testing with the clone sub-site. 
This drupal site had many sub-sites - all using the same database with table prefixes for each site.
To clone a drupal sub-site, you need to do 2 things:

  • Clone the database
  • Clone the sites/sub-site directory
  • Change the settings file

Clone the Database

The mysql database concern had hundreds (maybe even thousands) of tables due to the multiple sites using the same database. The new sub-site should also be using this database. So the problem is - how to clone all the tables for the site been copied. All these tables will have a prefix - like abc_. We need to copy all abc_ tables to def_ tables. 

Single (or a few) Table

It is easy to do this for 1 table:

  • CREATE TABLE def_xxx SELECT * FROM abc_xxx; or
  • CREATE TABLE def_xxx LIKE abc;
    INSERT def_xxx SELECT * FROM abc_xxx;

But when you have 95 tables to copy, this would be a lot of work. 
Update: Just discovered a hack that will do a dump for all sub-site files with a particular prefix
mysqldump -u DB_USER -p DRUPAL_DB $(mysql -u DB_USER -p -D DRUPAL_DB -Bse "show tables like 'PREFIX%'") > PREFIX.sql
You will have to enter the password twice. 

MySQL dump

Second option - do a mysqldump of the database to a file :
     mysqldump -u user -p database >database.sql
You then need to edit the file - remove not relevent tables and rename all the abc_xxxx to def_xxxx. Then use 
     mysql -u user <database.sql
My problem - the database is far too big - the dump was a 125Mb file. Far too big to work with in a text editor.

Table File Copy

The third option - which is the one I use is to copy the table files. Each Mysql table is a set of files in the database directory on the server.

  • Copy the required files - all named abc_xxxx.ext - to another directory: cp abc_* /dir/.
  • Rename the files using rename: rename 's/abc/def/' *
  • Copy the renamed files back to the database directory

You now have clone the database.

Clone the sub-site directory

Compare to the database, this is easy - just copy the whole sub-site directory. You need to be in drupal sites directory:
    cp -r abc_site def_site

Change the Settings file

You now need to edit the def_site settings file. Specifically you need to change the prefix from abc_ to def_.
The settings file you need to change is def_site/settings.php
That is it - you now have a clone of your original sub-site