Almost all of us use separate environments for separate needs like dev, UAT, production, etc.
And we most probably have separate regions for separate environments to isolate them completely.
But many a times we need to have production data on our lower environments to test some functionality or see the actual effects of our actions.
In this post, we’ll be going through the process of synchronizing two separate WordPress environments.
To begin with, there are three major components of any wordpress application
- Uploads directory
- Code base (files like wp-config.php)
Code base should be maintained via GitHub (so we won’t need to sync it). Therefore we’ll talk about DB (RDS) and Uploads (EFS) only.
Synchronizing the Database
One way to do this — which seems very simple initially, is to copy an RDS snapshot to the other region.
Very simple, right?
No. Because when we launch a new RDS from that snapshot, the endpoint changes. Also, if you follow best practices, you’ll be aware that username, DB name and password for both the RDS should be different.
And having them changed, brings a lot of overhead in terms of modifying them in the codebase and also, not to forget, we’ll have same DB name, username and password for both the environments.
So, we won’t be taking this path. What else can be done is more of a traditional approach.
We’ll migrate DB dumps.
Steps to be done on source RDS
You can do that in the same VPC, but just to isolate ourselves, I’ll encourage you to use a separate VPC.
Now, in whatever VPC you are using, launch a new EC2 instance and an RDS instance from a snapshot of the RDS you want to export.
Make sure that you select a right sized EC2 and RDS instance type as this could take a lot longer with smaller instance types if you have a large DB
Once done, create an SQL dump of the desired database. You can do this by SSHing to the instance or use System Manager’s Run command.
To make a DB dump, run the following
mysqldump -h <endpoint of the RDS> -u <username> — password=<password> <DB name> > dump.sql
Once the db dump is completed, we’ll be moving this to S3 from where we can then download it in the destination region.
So create an S3 bucket for this purpose and attach S3FullAccess (or you can go with least privileges) to the instance. Then run the following command from the EC2 instance (or System Manager Run Command)
mv dump.sql sqldump/dump.sql
aws s3 sync sqldump/ s3://<bucket-name>/
This would upload your DB dump to the S3 bucket.
Steps to be done in destination environment
Next, create another EC2 instance in the destination region with the same IAM role attached to it. From this instance, we first need to drop the existing database in our RDS.
Once done, we’ll run the following commands
aws s3 sync s3://<bucket-name>/ .
mysqldump -h <endpoint of the RDS> -u <username> — password=<password> <DB name> < dump.sql
Now we have our DB synchronized. But one more thing is left.
Many times we have different URLs for different environments. So now we need to modify those URLs in our migrated DB as well. To do so, we’ll use wp-cli.
Go to the root directory of your wordpress website and run the following command
wp --allow-root --verbose search-replace "https://<source URL>" "https://<destination URL>" && wp --allow-root --verbose search-replace "http://<source URL>" "https://<destination URL>" && wp --allow-root --verbose search-replace "www.<source URL>" "<destination URL>"
Now we have our DB synced up 👍
We’ll cover up the synchronizing of file system in the next part of this post.