Copying your live site to a test site (command line)

Last modified: September 1, 2009 - 20:19

Before you begin, decide where the new installation will be located. Usually you can just install Drupal to a subdirectory: http://www.example.com/test_site/.

Make sure you have already completed the steps for backing up your database.

Note:

In order to have clean urls in your test_site directory you have to edit the mod_rewrite settings in test_site/.htaccess, uncomment and modify:
RewriteBase /test_site

I. Copy your live site files to a test site directory

Copy the contents of your live site directory to a new directory called "test_site", or whatever name you would like to use:

cp -R /path/to/drupal/ path/to/new/drupal/site

Make sure the .htaccess file gets copied to the new directory as well.

II. Setup a blank test-site database with MySQL

a) Create a new, blank MySQL database (login to MySQL) and give permissions for a user to access it:

mysqladmin -u dba_user -p create test_site_database

(Where 'dba_user' is an example MySQL user which has the CREATE and GRANT privileges. Use the appropriate user name for your system.)

b) MySQL will prompt for the 'dba_user' database password and then create a blank test_site database. Next you must login and set the access database permissions:

mysql -u dba_user -p

c) Again, you will be asked for the 'dba_user' database password. At the MySQL prompt, enter following command:

GRANT ALL PRIVILEGES ON test_site_database.* TO nobody@localhost IDENTIFIED BY 'password';

where 'test_site_database' is the name of your database 'nobody@localhost' is the username of your webserver MySQL account 'password' is the password required to log in as the MySQL user.

d) To activate the new permissions you must enter the command:

flush privileges;

and then enter '\q' to exit MySQL.

III. Change settings.php (or includes/conf.php) in your test_site directory

a) Open and edit the file located under sites/default/settings.php (or includes/conf.php) with an editor. Follow the directions in the file and modify these three settings:

  • $db_url = 'mysql://user:pass@localhost/testsite_db';

    (test-site database login & password, URL location of the test-site database. "localhost" usually works by default.)

  • $db_prefix = '';

    (Sometimes your database tables will have a prefix. Ask your webhosting company for help if you think this is causing a problem. Otherwise leave it blank.)

  • $base_url = 'http://www.example.com/test_site';

    (This is where you want to move Drupal. You can install Drupal to a subdirectory such as http://www.example.com/test_site. This is what we want.)

IV. Import your database backup into the test_site database

Now that you have a new blank test_site database, you must take the backup.sql file you created in the backup step and import it into your new database. This will create a copy of the live site database on the new test site database. There are several methods for doing this.

IV method 1: Use the contributed database insertion script

a) Change to the test_site directory.

cd test_site

b) Get the database insertion script, and set permissions on the script. Your database may have a limit on the size of file it can import. Use the --max_allowed_packet option for mysql to increase the size of db import.

wget 'http://cvs.drupal.org/viewvc.py/drupal/contributions/sandbox/drumm/tools/drupalsql.sh?revision=1.1' -O drupalsql.sh; chmod +x drupalsql.sh

c) Now execute the script, which will create all the Drupal tables needed on the test_site database:

  • If you are running a version of Drupal 4.6 or newer, then enter the following:

    ./drupalsql.sh test_site/sites/default/settings.php < /path/to/backup.sql

  • If you are running a version of Drupal 4.5 and older, run the following:

    ./drupalsql.sh test_site/includes/conf.php < /path/to/backup.sql

IV method 2: Use mysqldump directly

You can also copy the database directly from the old database to the new database, by issuing this command:

mysqldump --opt --user=<user> --password=<password> <original database> | mysql --user=<user> --password=<password> <new database>

This way you do not need to make a dump, which greatly improves speed.

IV method 3: Using the mysql console

You can also use the MySQL console to import your database, by issuing the following commands. This assumes you already created the new MySQL database, following the instructions above.

mysql -u {user name you are using for the new database} -p
Enter password: {enter new database password}
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is xxxxx
Server version: 5.0.44

Type 'help;' or '\h' for help. Type '\c' to clear the buffer.

mysql> use {new database name};

mysql> source /path/to/your/backup/file/such/as/backup.sql;

mysql>\q

V. Check if your test site works

Navigate to http://www.example.com/test_site and you should see a working copy of your live site.

 
 

Drupal is a registered trademark of Dries Buytaert.