Learn how to migrate a Drupal website using Drush, a utility which allows you to work with Drupal from the command line. Drush can be used to easily migrate a Drupal website from one folder to another on the same server, or from one server to another.
- A server running Linux (CentOS 7 or Ubuntu 14.04).
- Drupal installed and running.
- At least one Drupal website.
For more information on installing Drupal, see our articles:
You will need to install Drush if you have not already. The best way to install Drush on CentOS 7 and Ubuntu 14.04 is to use PHP's Composer utility with the command:
sudo php -r "readfile('http://files.drush.org/drush.phar');" > drush
Next, make the Drush installation executable with the command:
sudo chmod +x drush
Finally, move the Drush files so that you can use Drush from anywhere on the server:
sudo mv drush /usr/local/bin
You can now test the Drush installation with the command:
sudo drush status
If Drush is installed correctly, this will return information about the server:
user@localhost:~# sudo drush status PHP configuration : /etc/php5/cli/php.ini PHP OS : Linux Drush script : /usr/local/bin/drush Drush version : 8.1.3 Drush temp directory : /tmp Drush configuration : Drush alias files :
Create the Alias
You will need to create two Drush aliases: one for the existing site, and one for the new site. Begin by editing the aliases.drushrc.php file with the command:
sudo nano /root/.drush/aliases.drushrc.php
This is a PHP script, which means that the first line of the file needs to be:
And the last line needs to be:
In between those brackets, we will be putting the aliases for both sites. The format for this is:
$aliases['old-site'] = array( 'root' => '/var/www/html', 'uri' => 'example.com', );
$aliases['new-site'] = array( 'root' => '/var/www/html/new-site', 'uri' => 'example.com/new-site', );
For example, to create an alias to old-site which has its files located in /var/www/html as per the Drupal installation, and is visible at http://example.com you would use the following:
Let's say we want to move this existing site to a sub-directory /var/www/html/new-site. We will need to add a second alias for this site:
Altogether, the file will read:
<?php $aliases['old-site'] = array( 'root' => '/var/www/html', 'uri' => 'example.com', ); $aliases['new-site'] = array( 'root' => '/var/www/html/new-site', 'uri' => 'example.com/new-site', ); ?>
Save and exit the file. Then verify that you have set it up correctly by checking the status of the existing site with the command:
sudo drush @old-site status
Next, create the folder for the new site. In this example, the command will be:
sudo mkdir /var/www/html/new-site
Create the New Database
You will need to create a new database and add a new database user for the new site. To do this, log in to MySQL/MariaDB with the command:
sudo mysql -u root -p
After you enter the MySQL/MariaDB root user password, you will be logged into the MySQL/MariaDB client.
Create a database with the command:
CREATE DATABASE [database name];
Replace [database name] with the name you want to use for your new database. For example, if you wanted to name your database new_drupal_site the command would be:
CREATE DATABASE new_drupal_site;
Create a user for this database and grant them privileges with the command:
GRANT ALL ON [database name].* TO [database username]@localhost IDENTIFIED BY '[database user password]';
- [database name] with the name of your database.
- [database username] with the username you want to create for your database.
- [database user password] with a password for this user. Note: Be sure to give the user a strong password.
For example, to create a user named new_drupal_user with the password Fr4i*Re!2 and give the user privileges on the new_drupal_site database, the command would be:
GRANT ALL ON new_drupal_site.* TO new_drupal_user@localhost IDENTIFIED BY 'Fr4i*Re!2';
Once you have finished, exit the database with the command:
Migrate the Files
Now that everything is set up, it's time to migrate the website files with the command:
sudo drush core-rsync @old-site @new-site --include-conf
To activate the new site, you will need to edit the settings.php file and update the database info. This file is found in the [website document root]/sites/default directory. In our example, we will edit this file with the command:
sudo nano /var/www/html/new-site/sites/default/settings.php
Scroll down to this part:
$databases = array ( 'default' => array ( 'default' => array ( 'database' => '[database name]', 'username' => '[database username]', 'password' => '[database password]', 'host' => 'localhost', 'port' => '', 'driver' => 'mysql', 'prefix' => '', ), ), );
Replace the database name, database username, and database password with the information you created in the previous step. In our example, this will be:
$databases = array ( 'default' => array ( 'default' => array ( 'database' => 'new_drupal_site', 'username' => 'new_drupal_user', 'password' => 'Fr4i*Re!2', 'host' => 'localhost', 'port' => '', 'driver' => 'mysql', 'prefix' => '', ), ), );
Save and exit the file. Then migrate the site to the new database with the command:
sudo drush sql-sync @old-site @new-site --create-db
This will complete the migration, and your Drupal site will now be active at its new location.