
Migrate Wordpress Database
September 15, 2015I recently found the WP-CLI library and have been investigating ways I can integrate it into my workflow. I should note here that we use shared hosting extensively. Ideally the preferred solution would be to use a true migration tool. This is not always possible with shared hosting.
To use the tool the following commands would be issued:
migrate_wordpress.sh dev production
This command would pull the latest version of the wordpress database from the production server.
Finally, in the development version of the Wordpress site an defined variable is used to set the environment. This then is used by the script to inject the desired database credentials. One downside to this is that it requires remote access to the database. Fortunately this suits my use case but will not suit all.
The added code to the wp-config.php would look like:
if( ! defined( 'ENV' ) ) {
define( 'ENV', 'dev' );
}
if( 'production' === ENV ) {
define('DB\_NAME', '');
define('DB\_USER', '');
define('DB\_PASSWORD', '');
define('DB\_HOST', '');
} elseif( 'staging' === ENV ) {
define('DB\_NAME', '');
define('DB\_USER', '');
define('DB\_PASSWORD', '');
define('DB\_HOST', '');
} else {
define('DB\_NAME', '');
define('DB\_USER', '');
define('DB\_PASSWORD', '');
define('DB\_HOST', '');
}
The files used can be downloaded in the link below.