You are here

Drush synchronization of database and files

You can use drush to synchronize databases and files directories from production servers to your local development setup. This blog post show an easy way to setup these aliases and adds three new commands to do drush synchronization.

The code below adds a new array $sites which defines the aliases in an easy way and is used to build a more advanced drush aliases array and adds the three new synchronization commands to the options array. The $sites array should be defined as this:

$sites = array(
  'demo' => array( // Demo here is the aliases name.
    'live' => array( // The live site's information will be the alias @demo_live.
      'uri'  => 'demo.com', // Site URI (Required).
      'path' => '/home/www/aegir/', // Base path on the server (Optional, if not defined $default_root_remote_path is used).
      'root' => 'platforms/drupal-7.12', // The root path, which is add the path above (Required).
      'host' => 'aegir.server.com', // The server that hosts the site (Required).
      'user' => 'aegir', // The remote ssh user on the server (Required).
      'files' => 'sites/demo.com/files', // The files directory for the site (Optional, if not defined drush will try to detect it).
    ),
    'stg' => array( // The staging/sandbox site's information will be the alias @demo_stg (Optional).
      'uri'  => 'stg.demo.com',
      'root' => 'stg_demo_com/htdocs/',
      'host' => 'stg.server.com',
    ),
    'dev' => array( // The local development site's information will be the alias @demo_dev.
      'local' => TRUE, // Mark this sub-array as defining the local alias (Required).
      'uri'  => 'demo.localhost',
      'root' => 'work/drupal/demo/htdocs/',
    ),
  ),
);

The script

The aliases as written below should be placed in .drush/aliases.drushrc.php and start by defining the $sites array followed by the code that generates the actually aliases.

<?php
// Default configuration paths.
$default_root_remote_path = '/var/www/drupal/';
$default_root_local_path = '/home/cableman/www/';
$default_remote_user_name = 'cableman';

// Define the sites basic configuration (Two demo sites name a and b is defined here).
$sites = array(
  // Site A
  'sitea' => array(
    'live' => array(
      'uri'  => 'sitea.com/',
      'root' => 'sitea_com/htdocs/',
      'host' => 'server.com',
    ),
    'stg' => array(
      'uri'  => 'stg.sitea.com',
      'root' => 'stg_sitea_com/htdocs/',
      'host' => 'stg.server.com',
    ),
    'dev' => array(
      'local' => TRUE,
      'uri'  => 'sitea.localhost',
      'root' => 'work/drupal/sitea/htdocs/',
    ),
  ),
  // Site b
  'siteb' => array(
    'live' => array(
      'uri'  => 'siteb.com',
      'path' => '/home/www/aegir/',
      'root' => 'platforms/drupal-7.12.2',
      'host' => 'aegir.server.com',
      'user' => 'aegir',
    ),
    'dev' => array(
      'local' => TRUE,
      'uri'  => 'siteb.localhost',
      'root' => 'work/drupal/siteb/htdocs/',
    ),
  ),
);

/**
* This is the main part of the alias script that builds the $aliases and $options
* array that drush uses based on the custom $sites array defined above.
*/
foreach ($sites as $alias => $site) {
  foreach ($site as $key => $type) {
   // Check if this is the local type (which as default is live, stg or dev).
    $local = FALSE;
    if (isset($type['local'])) {
      $local = $type['local'];
    }

    // If not local add remote options else build local alias.
    if (!$local) {
      $user = array_key_exists('user', $type) ? $type['user'] : $default_remote_user_name;
      $aliases[$alias.'_'.$key] = array(
        'root' => (isset($type['path']) ? $type['path'] . $type['root'] : $default_root_remote_path . $type['root']),
        'uri' => $type['uri'],
        'remote-host' => $type['host'],
        'remote-user' => isset($type['user']) ? $type['user'] : $user,
      );
    }
    else {
      $aliases[$alias.'_'.$key] = array(
        'root' => $default_root_local_path . $type['root'],
        'uri' => $type['uri'],
        '#live' => '@' . $alias . '_live',
      );
    }

    // Set default dump path when using sql-sync.
    $aliases[$alias.'_'.$key]['path-aliases'] = array(
      '%dump' => '/tmp/sql-sync-' . $alias . '_' . $key . '.sql',
    );

    // Set files directory if defined, if not drush will try to detect it.
    if (isset($type['files'])) {
      $aliases[$alias.'_'.$key]['path-aliases']['%files'] = $type['files'];
    }
  }
}

// Remove the $sites array
unset($sites);

// Add three new commands to synchronize sites to local development environment.
$options = array();
$options['shell-aliases']['pull-data'] = '!drush sql-sync {{#live}} {{@target}} --no-cache && drush rsync {{#live}}:%files {{@target}}:%files';
$options['shell-aliases']['pull-sql'] = '!drush sql-sync {{#live}} {{@target}} --no-cache';
$options['shell-aliases']['pull-files'] = '!drush rsync {{#live}}:%files {{@target}}:%files';

// Debug statement, which can be used to see if the arrays are build correctly.
//print_r($options);
//print_r($aliases);

Synchronization commands

As mentioned the alias file above introduces three new commands, namely pull-data, pull-sql and pull-file. First command syncs the database and then the files folder, where the later sync database (sql) and files only. The commands can be added an -y to automatically answer yes to all questions.

~$ drush @site_dev pull-data
~$ drush @site_dev pull-sql
~$ drush @site_dev pull-files

References

Tags

Drupal drush PHP aliases

Add new comment