Answers.Body: <p>Many of the steps documented on the "Migrating a site" wiki page are handled automatically now.  Here's my recipe, which is far from perfect, but is much closer.</p>
  
  <ul>
  <li>Copy the entire filesystem to the staging server.  Git works well for this, but when it's not an option, we'll use SFTP or create a tarball.</li>
  <li>Dump the database using <code>mysqldump</code>, either directly or using <code>drush civicrm-sql-dump</code> or <code>wp-cli cv sql-dump</code>.  Copy the db dump to the staging server.  We don't use git for this because of the database size; we either use <code>drush sql-sync</code> or copy it manually.</li>
  <li>Assuming a manual database dump copy, we need to remove the trigger definers - unless the MySQL username is identical on both production and staging.  I use <code>sed -i -e 's:/\*\!50017.*\*/::g'</code>.</li>
  <li>Load the database into MySQL on the staging server.</li>
  </ul>
  
  <p>Assuming this is not the first time we set up the server, we're done!  On first time setup, there are a few other things we need to do:</p>
  
  <ul>
  <li>Replace all the usernames, passwords, paths, etc. in <code>settings.php</code>, <code>civicrm.settings.php</code>, and/or <code>wp-config.php</code> with the values appropriate to the staging server.  If using git, these files are ignored in git, only local copies exist.</li>
  <li>There are a number of values we override in <code>civicrm.settings.php</code> on the staging server to use the appropriate paths.  Here's what I put in mine, directly below the <code>&lt;?php</code> marker:</li>
  </ul>
  
  <p>.</p>
  
  <pre><code>global $civicrm_setting;
  $civicrm_setting['CiviCRM Preferences']['allowPermDeleteFinancial'] = 1;
  $civicrm_setting['Directory Preferences']['customTemplateDir'] = '/home/jon/local/example/wp-content/civicrm/templates';
  $civicrm_setting['Directory Preferences']['customPHPPathDir'] = '/home/jon/local/example/wp-content/civicrm';
  $civicrm_setting['Directory Preferences']['extensionsDir'] = '/home/jon/local/example/wp-content/civicrm/extensions';
  $civicrm_setting['URL Preferences']['extensionsURL'] = 'http://example.local/civicrm/extensions/';
  $civicrm_setting['URL Preferences']['imageUploadURL'] = 'http://example.local/wp-content/plugins/files/civicrm/persist/contribute/';
  $civicrm_setting['URL Preferences']['userFrameworkResourceURL'] = 'http://example.local/wp-content/plugins/civicrm/civicrm';
  </code></pre>
  
  <p>In Wordpress, we also need to add a few overrides to <code>wp-config.php</code>.  Some of these may be overkill but:
  <code>
  define('WP_HOME','http://example.local');
  define('WP_SITEURL','http://example.local');
  define('RELOCATE',true);
  </code>
  Finally, if this is a multi-site Wordpress, we need to do a search and replace of the database for the URL.  We do this with <code>wp-cli search-replace https://oldsite.com https://newsite.com</code> but <a href="https://interconnectit.com/products/search-and-replace-for-wordpress-databases/" rel="noreferrer">this tool</a> also does the trick.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-27T22:39:54.800Z
Answers.Text: And in most cases, disable sending email, the most stable would be:
  Activate in civicrm.settings.php the CIVICRM_MAIL_LOG.
  
  An alternative is to do it in Admin screen, but you have to remember it with every staging copy, while in most cases you would keep the staging civicrm.settings.php with the next copy.
Answers.UserDisplayName: Catorghans
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-26T16:42:15.117Z
Answers.Text: It would be helpful to get this into the documentation.
Answers.UserDisplayName: Graham
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-24T11:39:19.057Z
Answers.Text: If you are using CiviCRM 4.7 or newer and you did not change the paths on production you don't need to change the Directory Preferences or URL preferences in civicrm.settings.php
Answers.UserDisplayName: Jaap Jansma - CiviCooP
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-02-27T22:28:20.947Z
Answers.LastActivityDate: 2018-02-21T02:59:33.573Z
Answers.LastEditDate: 2018-02-21T02:59:33.573Z
Answers.UserDisplayName: Jon G - Megaphone Tech
Answers.Body: <p>Thanks to Jon G and alec for this answer that mostly works for me.</p>
  
  <p>Just a detail on the sed expression that was not working out of the box for me, I use:</p>
  
  <pre><code>sed -e 's:/\*\!50017[^*]*\*/::g' &lt;DUMP_ORIG&gt; &gt; &lt;NEW_DUMP&gt;
  </code></pre>
  
  <p>The <code>/*!50017 ...*/</code> is in the middle of a long line that define other stuff related to triggers that I want to keep. This modified regex ensure that I remove <strong>only</strong> this <code>/*!50017 ... */</code> and not the whole comment.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-29T22:29:45.643Z
Answers.Text: Welcome to CiviCRM.SE. I've flagged for a moderator to convert this into a comment, as it is not a full answer to the original question. If you are new to Stack Exchange, I strongly recommend you take the site [tour] and review the [help].
Answers.UserDisplayName: choster
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-29T13:40:30.910Z
Answers.LastActivityDate: 2018-05-29T13:40:30.910Z
Answers.UserDisplayName: Erachen
Answers.Body: <p>Steps to migrate site having CiviCRM from prod to staging:</p>
  
  <ol>
  <li>Take SQL dump of your CMS database and if civicrm is using a different database than CMS (which in most cases people does that), take sqldump of civicrm database too</li>
  <li>Copy codebase over to staging server</li>
  <li>Create a new database for your CMS and civicrm (if civicrm on prod was using different DB than of CMS) on staging server</li>
  <li>Load both databases with dumps you took in #1</li>
  <li>Update CMS DB config file (drupal = settings.php || Wordpress = wp-config.php) to match correct database credentials</li>
  <li>Update civicrm_settings.php file to match correct database credentials</li>
  <li>Update civicrm_settings.php file for correct CIVICRM_UF_BASEURL and civicrm_root variables</li>
  </ol>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2018-05-30T21:36:28.330Z
Answers.LastActivityDate: 2018-05-30T21:36:28.330Z
Answers.UserDisplayName: Hassan Farooq
Body: <p>I think the procedures on migrating a site at <a href="https://wiki.civicrm.org/confluence/display/CRMDOC/Moving+an+Existing+Installation+to+a+New+Server+or+Location">https://wiki.civicrm.org/confluence/display/CRMDOC/Moving+an+Existing+Installation+to+a+New+Server+or+Location</a> are out of date. They certainly don't reflect how we at JMA migrate sites from production to staging in 2016. For example, we don't create a fresh install of CiviCRM, and instead pull the source code from git and copy the dbs. We also use various techniques to prevent staging, dev and local test sites from sending out emails, which are not included the recipe. </p>
  
  <p>So: what is a good recipe for migrating a production site to a staging instance?</p>
  
Tags: <migration>
Title: How should I copy a site from production to staging?
ThreadText: # How should I copy a site from production to staging?
  
  (Asked by: username_0 on 2016-02-18)
  
  I think the procedures on migrating a site at <https://wiki.civicrm.org/confluence/display/CRMDOC/Moving+an+Existing+Installation+to+a+New+Server+or+Location> are out of date. They certainly don't reflect how we at JMA migrate sites from production to staging in 2016. For example, we don't create a fresh install of CiviCRM, and instead pull the source code from git and copy the dbs. We also use various techniques to prevent staging, dev and local test sites from sending out emails, which are not included the recipe.
  
  So: what is a good recipe for migrating a production site to a staging instance?
  
  **Tags:** migration
  
  
  ---
  
  ## 3 answers
  
  ### Answer by username_1 on 2016-02-27. Score: 13
  
  Many of the steps documented on the "Migrating a site" wiki page are handled automatically now. Here's my recipe, which is far from perfect, but is much closer.
  
  * Copy the entire filesystem to the staging server. Git works well for this, but when it's not an option, we'll use SFTP or create a tarball.
  * Dump the database using `mysqldump`, either directly or using `drush civicrm-sql-dump` or `wp-cli cv sql-dump`. Copy the db dump to the staging server. We don't use git for this because of the database size; we either use `drush sql-sync` or copy it manually.
  * Assuming a manual database dump copy, we need to remove the trigger definers - unless the MySQL username is identical on both production and staging. I use `sed -i -e 's:/\*\!50017.*\*/::g'`.
  * Load the database into MySQL on the staging server.
  
  Assuming this is not the first time we set up the server, we're done! On first time setup, there are a few other things we need to do:
  
  * Replace all the usernames, passwords, paths, etc. in `settings.php`, `civicrm.settings.php`, and/or `wp-config.php` with the values appropriate to the staging server. If using git, these files are ignored in git, only local copies exist.
  * There are a number of values we override in `civicrm.settings.php` on the staging server to use the appropriate paths. Here's what I put in mine, directly below the `<?php` marker:
  
  .
  
  ```
  global $civicrm_setting;
  $civicrm_setting['CiviCRM Preferences']['allowPermDeleteFinancial'] = 1;
  $civicrm_setting['Directory Preferences']['customTemplateDir'] = '/home/jon/local/example/wp-content/civicrm/templates';
  $civicrm_setting['Directory Preferences']['customPHPPathDir'] = '/home/jon/local/example/wp-content/civicrm';
  $civicrm_setting['Directory Preferences']['extensionsDir'] = '/home/jon/local/example/wp-content/civicrm/extensions';
  $civicrm_setting['URL Preferences']['extensionsURL'] = 'http://example.local/civicrm/extensions/';
  $civicrm_setting['URL Preferences']['imageUploadURL'] = 'http://example.local/wp-content/plugins/files/civicrm/persist/contribute/';
  $civicrm_setting['URL Preferences']['userFrameworkResourceURL'] = 'http://example.local/wp-content/plugins/civicrm/civicrm';
  
  ```
  
  In Wordpress, we also need to add a few overrides to `wp-config.php`. Some of these may be overkill but:
  `define('WP_HOME','http://example.local');
  define('WP_SITEURL','http://example.local');
  define('RELOCATE',true);`
  Finally, if this is a multi-site Wordpress, we need to do a search and replace of the database for the URL. We do this with `wp-cli search-replace https://oldsite.com https://newsite.com` but [this tool](https://interconnectit.com/products/search-and-replace-for-wordpress-databases/) also does the trick.
  
  > **Comments:**
  > **username_2**: And in most cases, disable sending email, the most stable would be: Activate in civicrm.settings.php the CIVICRM_MAIL_LOG.  An alternative is to do it in Admin screen, but you have to remember it with every staging copy, while in most cases you would keep the staging civicrm.settings.php with the next copy.  
  > **username_3**: It would be helpful to get this into the documentation.  
  > **username_4**: If you are using CiviCRM 4.7 or newer and you did not change the paths on production you don't need to change the Directory Preferences or URL preferences in civicrm.settings.php  
  
  ---
  
  ### Answer by username_5 on 2018-05-29. Score: 1
  
  Thanks to Jon G and alec for this answer that mostly works for me.
  
  Just a detail on the sed expression that was not working out of the box for me, I use:
  
  ```
  sed -e 's:/\*\!50017[^*]*\*/::g' <DUMP_ORIG> > <NEW_DUMP>
  
  ```
  
  The `/*!50017 ...*/` is in the middle of a long line that define other stuff related to triggers that I want to keep. This modified regex ensure that I remove **only** this `/*!50017 ... */` and not the whole comment.
  
  > **Comments:**
  > **username_6**: Welcome to CiviCRM.SE. I've flagged for a moderator to convert this into a comment, as it is not a full answer to the original question. If you are new to Stack Exchange, I strongly recommend you take the site [tour] and review the [help].  
  
  ---
  
  ### Answer by username_7 on 2018-05-30. Score: 0
  
  Steps to migrate site having CiviCRM from prod to staging:
  
  1. Take SQL dump of your CMS database and if civicrm is using a different database than CMS (which in most cases people does that), take sqldump of civicrm database too
  2. Copy codebase over to staging server
  3. Create a new database for your CMS and civicrm (if civicrm on prod was using different DB than of CMS) on staging server
  4. Load both databases with dumps you took in #1
  5. Update CMS DB config file (drupal = settings.php || Wordpress = wp-config.php) to match correct database credentials
  6. Update civicrm\_settings.php file to match correct database credentials
  7. Update civicrm\_settings.php file for correct CIVICRM\_UF\_BASEURL and civicrm\_root variables
  
  
  ---
  
  