使用 drush sql-sync 进行 Drupal 数据库同步

注意事项:
如支付后未自动显示完整内容,可点击“已支付?点此查询订单”进行查看。
如遇内容不符或缺失,请联系内容作者或平台客服(工作日 9:00-18:00)。

  sql-sync 是 drush 的一个子命令,可以用于在站点之间进行数据库同步。有关 drush sql-sync 的相关信息可以参考 drush 官方文档:drush sql-sync

使用 drush sql-sync 同步 Drupal 数据库

  使用以下命令即可将源环境的数据库同步到目标环境

drush sql-sync @source @target

  @source@target 是 drush 中定义的站点别名,站点别名信息通过 .drush 目录下的 *.aliases.drushrc.php 文件进行定义。以下是一个别名文件的示例内容,如果不知道格式的话可以基于以下内容创建进行创建,修改相应的值即可:

<?php
/**
 * @file yoursite.aliases.drushrc.php
 * Site aliases for [your site domain]
 * Place this file at ~/.drush/  (~/ means your home path)
 *
 * Usage:
 *   To copy the development database to your local site:
 *   $ drush sql-sync @yoursite.dev @yoursite.local
 *   To copy your local database to the development site:
 *   $ drush sql-sync @yoursite.local @yoursite.dev -structure-tables-key=common --no-ordered-dump --sanitize=0 --no-cache
 *   To copy the production database to your local site:
 *   $ drush sql-sync @yoursite.prod @yoursite.local
 *   To copy all files in development site to your local site:
 *   $ drush rsync @yoursite.dev:%files @yoursite.local:%files
 *   Clear the cache in production:
 *   $ drush @yoursite.prod clear-cache all
 *
 * You can copy the site alias configuration of an existing site into a file
 * with the following commands:
 *   $ cd /path/to/settings.php/of/the/site/
 *   $ drush site-alias @self --full --with-optional >> ~/.drush/mysite.aliases.drushrc.php
 */

/**
 * Local alias
 * Set the root and site_path values to point to your local site
 */
$aliases['local'] = array(
  'root' => '/path/to/drupal/root',
  'uri'  => 'yoursite.localhost',
  'path-aliases' => array(
    '%dump-dir' => '/tmp',
  ),
);

/**
 * Development alias
 * Set up each entry to suit your site configuration
 */
$aliases['dev'] = array (
  'uri' => 'yoursite.dev',
  'root' => '/path/to/drupal/root',
  'remote-user' => 'ssh-username',
  'remote-host' => 'ssh-host',
  'ssh-options'  => '-p 2222',  // To change the default port on remote server
  'path-aliases' => array(
    '%dump-dir' => '/tmp',
  ),
  'source-command-specific' => array (
    'sql-sync' => array (
      'no-cache' => TRUE,
      'structure-tables-key' => 'common',
    ),
  ),
  // No need to modify the following settings
  'command-specific' => array (
    'sql-sync' => array (
      'sanitize' => TRUE,
      'no-ordered-dump' => TRUE,
      'structure-tables' => array(
       // You can add more tables which contain data to be ignored by the database dump
        'common' => array('cache', 'cache_filter', 'cache_menu', 'cache_page', 'history', 'sessions', 'watchdog'),
      ),
    ),
  ),
);

/**
 * Production alias
 * Set each option to match your configuration
 */
$aliases['prod'] = array (
  // This is the full site alias name from which we inherit its config.
  'parent' => '@yoursite.dev',
  'uri' => 'yoursite.com',
  'root' => '/path/to/drupal/root',
  'remote-user' => 'ssh-user',
  'remote-host' => 'ssh-host',
);

参考代码:example.aliases.drushrc.php

  创建好对应的别名文件后,执行以下命令即可开始将生产环境的数据库同步到开发环境

drush sql-sync @example.prod @example.dev

  除了同步数据库之外,drush 还可以使用 rsync 子命令进行文件同步,对于站点迁移和部署来讲都是非常方便实用的工具。

看完了?还不过瘾?点此向作者提问