As of Drush 4.5, migrating a Drupal site between servers became much easier. The new, little-known
drush archive-dump and
drush archive-restore commands make it an essentially three step process.
A basic Drupal site is made of two fundamental elements: the codebase and the database. When you migrate a Drupal site, you need to migrate both of these elements, often with a bit of re-configuration to boot.
The Old Way
Before using Drush to migrate a site, my standard procedure for site migration looked something like this:
- create a new, empty database
- create a new user
- associate the new user with the new database (with correct permissions)
- create a db dump from the source environment
- import the db dump to the target environment
- compress the codebase into a tar.gz file
- transfer files
- unarchive tarball
- edit the settings.php file to reflect the new database settings
- check file ownership, group membership, and permissions
The New Way (for me)
I'll run through the first iteration using psuedo [tokens] as placeholder for file and sitenames.
Open up the command line on the source machine (or SSH in) and
cd to your Drupal installation.
- Create a Drush archive dump (code and db in one)
drush archive-dump [site]
- Transfer the dump to your target machine (clearly, you could just FTP it)
scp [new-backup-archive] [user]@[target-machine]:[target-folder]
- Now switch to the command line on your target machine,
cdto the folder above your to-be-created Drupal installation and type:
drush archive-restore [new-backup-archive] [site] --destination=./[new-folder-name] --db-url=mysql://[msql-user]:[mysql-password]@[target-server]/[db-name]
Drush will then create the new directory, unarchive your tarball into it, create a new database, populate it, and edit your settings.php file accordingly. Wow. Awesome.
Here's how the process looks for me when moving to my local MAMP stack (without tokens):
drush archive-dump default
scp /home/grasmash/drush-backups/archive-dump/20111256023713/grasmash.20116223_023613.tar.gz [email protected][my-ip]:/Applications/MAMP/htdocs
drush archive-restore grasmash.20116223_023613.tar.gz default --destination=./grasmash --db-url=mysql://root:[email protected]/grasmash
Now it's important to note that you can use an existing directory or an existing database. If you'd like the contents of the existing directly overwritten, you can use the
--overwrite flag. Likewise, you can grant Drush sudo MYSQL privileges to modify your dbs by adding a
--db-su flag, followed by the correct credentials.
Furthermore, you don't need to use
--db-url flags at all. If you leave these out, Drush will attempt to unarchive to the current working directory, and will attempt to use the database credentials defined in the settings.php file.
For more information on these commands, use drush's --help flag, e.g.,
drush archive-restore --help
Quick disclaimer: There are many ways to migrate a Drupal site between servers. You should certainly be using SCM (like git), which can be used to move the codebase. You can use drush or backup_migrate to transfer the database. Or, you could simply move around Virtual Machines.
At present, it seems that the --db-url flag only works with Drupal 6. Please leave a comment and let me know if this is not the case!