How to create and restore snapshots using the CLI

Goal

Restore a Platform.sh live environment from a created snapshot.

Assumptions

This guide requires:

This guide uses a Python 3 template for a Platform.sh application using Flask as well as MySQL and Redis for its services. The template runs a test on both MySQL and Redis and returns a status dictionary to the live page.

Problems

Platform.sh recommends that snapshots are created of the live environment before merging, or when the storage space of services is increased. For one reason or another, it may become necessary to restore an active environment to a previous working state using snapshots. Creation and restoration of snapshots can be executed from the UI and using the CLI.

Additional information can be found in the public documentation

Steps

1. Create a snapshot

The master environment is functioning as desired:

$ curl https://master-7rqtwti-<project id>.<region>.platformsh.site/ 
{"mysql":{"return":null,"status":"OK"},"redis":{"return":null,"status":"OK"}}

From master create a snapshot using the Platform.sh CLI if one has not already been created.

$ platform snapshot:create
Creating a snapshot of master
Waiting for the snapshot to complete...
Waiting for the activity khlktlgy4r5uc (User created a backup of Master):
    Backing up master
    Backup name is <snapshot name>
   [============================] 12 secs (complete)
A snapshot of environment master has been created
Snapshot name: <snapshot name>

2. Changes were made and merged to Master

In another branch dev, some changes were made. For the Python 3 template example, the Redis test was changed from

70     r.set(key_name, "bar")

to

70     r.set(key_name, "BEAR")

which will result in an Exception for the failed test. It was not caught in time, and dev was merged into master.

$ platform environment:merge

Now the live site is failing:

$ curl https://master-7rqtwti-<project id>.<region>.platformsh.site/ 
{"mysql":{"return":null,"status":"OK"},"redis":{"error":["Traceback (most recent call last):\n","  
File \"server.py\", line 30, in wrap_test\n    result = callback(*args, **kwargs)\n","  
File \"server.py\", line 71, in test_redis\n    assert value == r.get(key_name)\n","AssertionError\n"],
"status":"ERROR"}}

3. Retrieve Snapshot Name

Actions must take place from the branch the snapshot was taken from, and will not be available from dev.

$ git checkout dev
Switched to branch 'dev'
$ platform snapshots
No snapshots found

List the saved snapshots to retrieve the snapshot name.

$ git checkout master
Switched to branch 'master'
Your branch is up to date with 'platform/master'.
$ platform snapshots
Snapshots on the project <project name> (<project id>), environment Master (master):
+---------------------------+----------------------------+----------+----------+---------+
| Created                   | Snapshot name              | Progress | State    | Result  |
+---------------------------+----------------------------+----------+----------+---------+
| 2019-03-11T15:16:16-04:00 | <snapshot name>            | 100%     | complete | success |
+---------------------------+----------------------------+----------+----------+---------+

4. Restore the snapshot

The snapshot can be restored to the original environment:

$ platform snapshot:restore <snapshot name>
Are you sure you want to restore the snapshot <snapshot name> from 2019-03-11T15:16:16-04:00 to environment Master (master)? [Y/n] Y
Restoring snapshot <snapshot name> to Master (master)
Waiting for the restore to complete...
Waiting for the activity tb7sf5gd3ivfw (User restored environment master from backup <snapshot name>):
    Provisioning certificates
    Environment certificates
    - certificate 7455422: expiring on 2019-06-09 17:53:35+00:00, covering master-7rqtwti-<project id>.<region>.platformsh.site
      [============================] 35 secs (complete)
The snapshot was successfully restored

It can also be restored to another active branch. In this case, the target is the branch feature-x:

platform snapshot:restore --target=feature-x <snapshot name>

5. Verify the restoration was successful

Check that the snapshot has been restored to the environment.

$ curl https://master-7rqtwti-<project id>.<region>.platformsh.site/ 
{"mysql":{"return":null,"status":"OK"},"redis":{"return":null,"status":"OK"}}
$
$ curl https://feature-x-c2qo5ma-<project id>.<region>.platformsh.site/
 {"mysql":{"return":null,"status":"OK"},"redis":{"return":null,"status":"OK"}}

Conclusion

Once a snapshot is created, undesired changes can be reverted on an active branch using the Platform.sh CLI.