Typically, each developer working on a Magento project has a development environment of his/her own. Additionally you have a testing and/or staging environment in many cases which is more or less a copy of the live environment. In both cases, it makes sense to use data which doesn’t differ too much from the data in the live database. So, now and then, the database will be copied from the live environment to another environment.

The traditional approach

A typical process in Magento 1 or Magento 2 would look like this:

  1. Dump the live database to a text file
  2. Ideally strip the database dump from sensitive customer data (you can use the fantastic n98-magerun tool for that, available for Magento 1 and Magento 2
  3. Copy the database dump to another environment
  4. Import the database
  5. Adjust some settings in the database, especially in the database table core_config_data
  6. Clear the cache

The fifth step is the most error-prone and time-consuming step. Of course, you can automate that, i.e. with a shell script which could look like this:

Configuration settings to adjust

The code above does a couple useful things:

  • Set the base URL (mandatory)
  • Set the cookie domain (sometimes mandatory)
  • Set the robots tag so the site won’t be indexed
  • Set visual identifiers for the non-live server so you can distinguish them on first view
  • Disable Google Analytics

Additionally, a few more settings might make sense, depending on the project:

  • Activate sandbox accounts for payment methods
  • Change API endpoints, i.e. for search with Solr or ElasticSearch or for Varnish
  • Configure development-specific modules
  • Change email settings
  • Change logging settings

A new approach for Magento 2.2+

Since Magento 2.2, you have the possibility to store configuration data in the app/etc/config.php and app/etc/env.php files. You can read more about that in my blog post Magento 2.2: Configuration via Code.
You can now use exactly that to configure your environment so you can totally skip the fifth step from above: After importing the database and clearing the cache, your environment will work directly. The app/etc/env.php file for a staging system at staging.example.com can contain the following part:

The commands for updating the database may now look like this when both live and staging environment are on the same server:

With this approach, you won’t need a bash script for updating your database any more – just import your database dump and clear the cache.

Andreas von Studnitz

Author: Andreas von Studnitz

Andreas von Studnitz is a Magento developer and one of the Managing Directors at integer_net. His main areas of interest are backend development, Magento consulting and giving developer trainings. He is a Magento Certified Developer since 2011 (and 2018 for Magento 2) and a Magento Certified Solution Specialist since 2014 (Magento 1) respective 2017 (Magento 2).

More Information · Twitter · GitHub · LinkedIn