Cloning Drupal sites - Part I: with CPanel access

This article is about making an exact duplicate of any live Drupal 6.x site onto one's local machine. It assumes you have a web server installed on your local machine, and have CPanel or access for a Drupal site on the web. Part II will address the case where you have root command-line access to the web server.

First I discuss how I use site cloning in my work and the many uses of this capability. Then I move on to instructions on cloning a typical Drupal 6.x site that uses MySQL in the case where you have (or use) only CPanel access to the server.

Why clone a Drupal site?

Recently, while I was assisting a client with a large data migration, the client's server suddenly developed multiple software issues that kept it from serving web pages the night before the deadline. We had both been working on that same server. What to do? To be as efficient as possible, I downloaded a dump of the site's MySQL database to my laptop and made the 17,000 Drupal users from legacy data locally, while the client dealt with the errant server and rebooted it. When the web server was back up I transferred a dump from my laptop to the live web server . The site we continued working on had one big difference to the site before the glitch -- it had 17,000 Drupal users. This was possible because I had earlier copied the site's PHP files to my laptop, in order to be able to trace the PHP execution line-by-line as a page was built, and get at the root causes of a software issue.

That anecdote sums up the two main reasons to clone a Drupal site:

  • To get the site on a development machine, in order to debug PHP line-by-line
  • To have another option of a machine to do site configuration on.

There are clearly, millions of variations of these reasons. As a Drupal developer, let me say that it has always been worthwhile to a client, to allow me to make a temporary clone of the client's web site on my laptop.

A simple site to clone

I put a simple web on-line in order to demonstrate cloning. The site is bare Drupal 6.19 install with the following configuration:

  • site information set
  • one Story created
  • core Upload module enabled
  • two file attachments (centipede image and README) added to the one Story
  • anonymous user granted permission to view file attachements.
  • added and enabled contrib module Administration Menu (from http://drupal.org/project/admin_menu)
  • added and enabled contributed theme sky (from http://drupal.org/project/sky)
  • created a sub-theme of sky called ctc_sky, with:
    1. The CTC logo at sites/all/themes/ctc_sky/logo.gif
    2. An template override templates/page-front.tpl.php that hard codes logo IMG tag with src="sites/all/themes/ctc_sky/logo.gif"
    3. A style.css override at sites/all/themes/ctc_sky/style.css that centers the logo properly by including a 1.5 em top-margin on elements of id="logo".

The site is installed in subdirectory "simple" of my document root on a shared server. I created the subdomain simplesite.crotown.com to point at that directory, so site can be accessed at http://simplesite.crotown.com. Here is a screenshot when I browse to this site with Firefox.

Cloning how-to: CPanel access

The first (and most important) task in cloning a site is downloading a database dump and installing it on your local machine. The tool I will use on both ends to accomplish this is phpMyAdmin, so I will assume you can run that through your CPanel on your web host and on your local machine (I use MAMP).

  1. Get a dump of the sites database on the local computer with the following steps:
    • Get the sites' [database_name] by looking at sites/default/settings.php in the web site's document root directory. And looking at the end of the path given in the line that begins with "$db_url =".
      $db_url = 'mysqli://.../[database_name]'
    • Open phpMyAdmin on your web host using CPanel.
    • Open [database_name] in phpMyAdmin.
    • Hit the "export" tab.
    • Scroll down and hit "Go" in the "Save as file"-section.
    • Locate the file called "[database_name].sql" on your local computer, in the directory where you browser puts downloaded files (e.g. your desktop or "Downloads" folder).
  2. Import the database onto the local machine using the following steps: (note MAMP-specific screenshots)
    • Open phpMyAdmin on your local machine.
    • Create a local database of the name [database_name].
    • Hit "Import" tab.
    • Hit "Browse" in the "File to import" section.
    • Select the dump in Step 1 and hit "Import".
      You should receive confirmation that the database was imported, similar to this:

      You should see the left sidebar list all the tables in your newly-imported Drupal database, like this. Congratulations, you now have the database of your web site cloned on your local machine.

  3. Transfer the files of the remote site to the local machine with the following steps:
    • Open CPanel File Manager and navigate to the root directory of the Drupal installation for the remote web site.
    • Check the box next to the name of the root directory and hit "Compress".
    • Select a "GZiped Tar Archive" and hit "Compress File(s)"
    • Dismiss the "Compression results" dialog.
    • Note the location of the new file called "simple.tar.gz"
    • Open your ftp client (I use FileZilla) and connect with sftp to the remote site. Navigate to simple.tar.gz on the remote site. Your window should look something like this.
    • Transfer the simple.tar.gz to your local web root directory (e.g. /Applications/MAMP/htdocs or /var/www) by dragging it across from the remote side to the local side or by selecting transfer from a menu.
    • Decompress and unarchive the file on your local machine (e.g. by opening it with StuffIt Expander.app).
    • Observe the local directory simple. It should look like this. Congratulations, you now have the files of the remote Drupal site on your local machine.
  4. Connect the database to your local copy of the site's files by the following steps:
    • Copy simple/sites/default/default_settings.php over simple/sites/default/settings.php. [Note that you may have to change permissions to do this, as you might in a regular Drupal install.]
    • Visit the URL that points to the local site, e.g. http://localhost:8888/simple
    • Choose "English" on Drupal's Choose a language page.
    • Set the database. Note that the default MySQL access is username "root" and password root. Hit "Save and continue".
    • Hit "Visit existing site" link.
    • Note that clone site is all correct, except for the logo (whose path was hard-coded into the theme ctc_sky).
  5. Make a virtual domain simplesite.local, as I described in a past blog post, so that the site can be accessed at http://simplesite.local:8888 and note that this cures the hard-coded path problem. The clone is perfectly identical now.

Where to go from here

A logical next step is to investigate cloning sites from the command-line entirely. This streamlines the process and will be covered in Part II of this post. There are also ways to clone using scripts, Drush, and Aegir. See my post about handling large databases with phpMyAdmin on shared hosts. And there is an excellent post (and comments) on dealing with large MySQL databases from the command-line.

Cloning is essential to any development/staging/live-type environment, so there is lots of experience using it in the real-world. You may investigate posts on various problems cloning, because there are lots of things that can go wrong. But handling whatever comes up while using this technique is a mark of a experienced Drupal developer. Rest assured, we Drupalistas can clone any Drupal site. It is an amazing fact.

ADDENDUM [last updated Sept. 10, 2010]
Here are some notes about the cloned site and dealing with things going worng on it. I will expand and improve this section occassionally

  • The first time you log into your cloned site - remember that your credentials will be identical to those of the on-line; the cloned site has an identical users table in its database.
  • If the cloned site will not boot Drupal, it is important not to settle for a White Screen of Death and merely guess what is causing the problem. Add the three lines:
    ini_set('display_errors', TRUE);
    ini_set('display_startup_errors', TRUE);

    at the top of index.php so that PHP will try to give to give you error page when it crashes.
  • If a crontrib module is crashing you can disable it easily by renaming its directory from sites/all/modules/[MODULE] to anything else, like sites/all/modules/OLD_[MODULE].


Glad you got your second clone site working (and your first)

If I had read your first comment and replied before you figured it out and commented again, I would have assured you that all the different variations of cloning a site (using CPanel as the main tool) are not very different. But you figured that out by trying a slightly different situation and making it work. Excellent!

For the post I needed to pick one concrete situation and give paths for that one exact situation so the instructions would not be too complex by trying to include many cases. But the post was designed to get you "on your way" with cloning Drupal sites, and in your case it has done that. Thanks for commenting and drop me an email using my contact form on the site talk about other things.

Any idea why modules don't show up after clone, or otherwise....

I have a db where conditional fields module started throwing PHP error on boot, and many of the remote installed modules are not in the local version. Cron is also stuck. Hmmmm.... I fixed the CF/CKK problem, but cron says it's been stuck for over an hour and hangs localhost when run manually. I tried this:
DELETE FROM `variable` WHERE name = 'cron_semaphore';
DELETE FROM `variable` WHERE name = 'cron_last';
but that did nothing to help.

Try visiting admin/build/modules

It turns out that just visiting the modules page at admin/build/modules magically heals lots of troubles since it takes the extra time to *discover* the modules you have in the modules directory at /sites/all/modules/. So my advice is to try that first. If it fails to fix the problem, make sure that each module is in a subdirectory of the modules directory and try going to that URL again. This is a tip that should be in the original post. Thanks for bringing it out (and sorry that you had to)!

Well after trying other methods...

Well after trying other methods this one is clearly the best. Besides my own cobbling before I found this was to try Backup & Migrate module, but that failed first try so I dropped that idea for cloning (looks good for backups however). Well, thx again for this very valuable newbie tip!

Clones from Backup & Migrate modules database dumps

I'm curious why clones from the Backup & Migrate Module's dumps fail for you. I think that module is probably "clever" and leaves out tables for things like caches and the search index (which saves a lot of space in the dumps). I suspect that just recreating empty versions the missing tables (that Drupal expects to see) would solve the issue. If you work out what to do, please post it here.

I will work this out pretty soon since cloning the old site is the first thing I would think of doing with a backup. But if you get to it first I won't have to, which would be great and an example of how the Drupal community works.

Me again, I figured it out...

It was really easy to clone from one mac to another using your instructions for server --> local machine. I can write up the specifics if you want to post here. Perhaps you would like to have an off shore partner in Nepal :) Seriously, this is my first drupal site http://hum.homedns.org/ and I am looking for help with the issues. But I just started yesterday...

great if not terse instructions, but...

how different are the instructions if I want to move the entire site to another local machine. I have my perfectly working drupal site working on an imac, and i want to move to mbp that has drupal/mamp already set up but completely bare bones. thx for any advice!

ps.Also looking for instructions on gettng the captcha installed and working for comments :)

Drupal SEO