Neuen Kommentar schreiben
by Brant Wynn
Note: This is an updated version of a blog post originally published by Promet Source. Moshe Weitzman contributed to this post.
One of main draws to Drush is the library's ability to make developer's lives easier. There are two simple commands that work using Drush aliases that can help sync database and files between multiple Drupal instances. First, we'll go over setting up an alias file for Drush. After that, we'll document the usage of Drush's sql-sync and rsync commands.
Create a file to store your aliases that is readable by Drush.
If it does not exist already, create a .drush folder in your home directory.
$ mkdir ~/.drush
Create an aliases file for the current project that you are setting up
$ vim ~/.drush/aliases.drushrc.php
You can call this file WHATEVER.aliases.drushrc.php or even just put this code in your drushrc.php file. One tactic might be to divide *.aliases.drushrc.php into different files per-project or per-environment.
Shown below is a template for setting up a local and remote alias for a given site. The 'local' alias is configured for OSX while the 'remote' aliases are configured for an environment running Ubuntu.
Your actual configuration will vary, but this should suffice as an example.
First we'll set up a local alias.
// local alias
$local_sites = '/users/username/Sites/';
$aliases[demo.local'] = array(
'root' => $local_sites . 'demo',
'path-aliases' => array(
'%dump-dir' => $local_sites . 'drush.dbdumps',
'%files' => $local_sites . 'demo/sites/default/files'
Next we'll set up a remote alias. If you're using Acquia Cloud, you can skip this part as remote aliases are available for download from your subscription.
// remote alias
$remote_sites = '/var/www/sites/';
$aliases['demo.dev'] = array(
'remote-host' => '10.0.0.83',
'remote-user' => 'username',
'root' => $remote_sites . demo.developmentbranch.com',
'path-aliases' => array(
'%dump-dir' => '/home/username/drush.dbdumps',
'%files' => $remote_sites . demo.developmentbranch.com/sites/default/files'
Using the $aliases array, one can define as many aliases as they need. One can also create parent alias and inherit values from child aliases. For more examples, see example.aliases.drushrc.php on Drush.org.
Easy Drush aliases for your Acquia Cloud environments
Acquia Cloud provides downloadable Drush aliases for all of your site's remote environments. You can find the link for your site's aliases from any Cloud workflow page under the Drush and API link.
Additionally, you can find aliases for all your subscriptions via the 'Credentials' link on your account profile. (Or just click here and look under Drush integration.) You can also get an up-to-date copy of all your drush aliases using drush itself. See these helpful Acquia Cloud API docs - specifically, the bit about drush @site.env acquia-update is helpful.
Also check out Using Drush Aliases in the Acquia Cloud Docs for more detailed usage.
Once you've downloaded and extracted the contents from the provided tarball, you'll have aliases for Dev, Stage and Prod ready to go. Remember these are the remote aliases. You'll still need to create local aliases if you wish to sync data between your machine and the remote server.
Now that we have aliases defined for our remote and local Drupal sites, we can sync database and files between them.
Import the remote database to your local database
$ drush sql-sync @demo.dev @demo.local
There are many --flags for drush sql-sync listed here: drush sql-sync at drushcommands.com. One of the most important ones to note is '--no-cache' - you will find this flag useful if you are syncing your database more than once in a 24 hour span and you want to be using a new db dump rather than a cached version of the db dump. Note that we defined a '%dump-dir' in our aliases settings. You can find recent db dumps in this folder. Drush must have permissions to write to the folder in order to successfully sql-sync your db dump.
Import the remote files to your local files folder
$ drush rsync @demo.dev:%files/ @demo.local:%files
This command will pull down the entire files directory from the remote site to the local site. This may take quite some time. Drush does not provide any indication that it is working outside of holding up your command line interface from accepting a new command until it is done. You can use a network monitor, such as the program that comes with OSX utilities, to actually see the files being downloaded and track progress. There are many --flags for drush rsync as well, and they are listed here: core-rsync at drushcommands.com.
The sql-sync and rsync commands can be very useful for setting up a new local testing environment from an existing project. Syncing DB and files from Prod to Dev frequently is a great way to keep your development environment up to date. The value of being able to automatically sync files and database saves time and energy.