How to sync data/code between environments

Goal

Sync data and/or code from one environment to another

Assumptions

You will need:

Problem

After setting up your environments, you discover you need to sync data from one development environment to another, but Platform.sh only allows sync’ing from a parent environment to a child environment. For example, let’s say you have the following environments setup:

❯ platform environment:list
Your environments are:
+-----------+---------+--------+-------------+
| ID        | Title   | Status | Type        |
+-----------+---------+--------+-------------+
| main*     | Main    | Active | production  |
|   dev     | dev     | Active | development |
|   sprint  | sprint  | Active | development |
|   staging | staging | Active | development |
+-----------+---------+--------+-------------+

(Note: your production environment may be named something different than main; adjust accordingly)

However, after doing some work in the sprint environment, you want to sync that data to your dev and staging environments. You try platform environment:sync but discover you can only sync from the main environment because it is the parent. You could download a sql dump from sprint’s environment and then import it into the other environments following the How to migrate database changes between environments guide, but it requires you to download a new copy of the database dump and then import each and every time you need to sync. And if you also want the file mount data to sync, you’ll have to do it manually as well. Instead, let’s change dev and staging’s parent!

Steps

The first thing we need to do is change the parent for our two environments from master to sprint:

❯ platform environment:info parent sprint -e dev
Property parent set to: sprint

and

❯ platform environment:info parent sprint -e staging
Property parent set to: sprint

After you change both environments, if you do an environment:list you should now see the two environments indented to indicate sprint is their parent:

❯ platform environment:list
Your environments are:
+-------------+---------+--------+-------------+
| ID          | Title   | Status | Type        |
+-------------+---------+--------+-------------+
| main        | Main    | Active | production  |
|   sprint*   | sprint  | Active | development |
|     dev     | dev     | Active | development |
|     staging | staging | Active | development |
+-------------+---------+--------+-------------+

2.

Now we can sync data/code from sprint to dev and/or staging!

❯ platform sync data -e dev
Are you sure you want to synchronize data from sprint to dev? [Y/n] y
Synchronizing environment dev
Waiting for the activity img23cpt4lorc (Paul Gilzow synchronized devs data from sprint):
Redeploying environment dev, as a clone of sprint

Note that:

Synchronizing “code” means there will be a Git merge from the parent to the
child. Synchronizing “data” means that all files in all services (including
static files, databases, logs, search indices, etc.) will be copied from the
parent to the child.

3.

Once you are finished sync’ing, or just need to change an environment back to being a child of your production environment, simply do the same thing we did in Step 1, but point it back to main

❯ platform environment:info parent main -e dev
Property parent set to: main

Now if I perform a sync on my dev environment, it will sync from the main environment:

❯ platform sync data -e dev
Are you sure you want to synchronize data from main to dev? [Y/n] y
Synchronizing environment dev
Waiting for the activity dz424vndxxto6 (Paul Gilzow synchronized devs data from Main):
<snip>
Redeploying environment dev, as a clone of main

Conclusion

Platform.sh is extremely flexible and gives you a multitude of ways to bend it to fit your workflow, including the ability to sync from any environment to another!

3 Likes

Hi, Thanks for the info, much help. But I have a question:

platform sync data -e dev
My command return this message: Operation not available: The environment "aaa-bbb" can't be synchronized.
And the sync button in the console also is disabled. how can I do next?
Thanks for your time,
Regards, Thi Dang

Hi!

I’m confused by your error message a bit - the CLI command you’ve written is identical to the post (i.e. syncing to the environment dev), but the error listed is for syncing to environment aaa-bbb.

Is aaa-bbb an active environment?

@thi-dang-dg
Can you run platform environment:list and paste that output here? That’ll give us a better idea of your branching structure.