Goal
Restore a Platform.sh live environment from a created snapshot.
Assumptions
This guide requires:
- An application running on Platform.sh
- A local repository with the Platform.sh project as git remote
- The Platform.sh CLI installed locally
- An SSH key configured on the project account
-
admin
role granted for the project - Your default/production git branch is named
main
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 main
environment is functioning as desired:
$ curl https://main-7rqtwti-<project id>.<region>.platformsh.site/
{"mysql":{"return":null,"status":"OK"},"redis":{"return":null,"status":"OK"}}
From main
create a snapshot using the Platform.sh CLI if one has not already been created.
$ platform snapshot:create
Creating a snapshot of main
Waiting for the snapshot to complete...
Waiting for the activity khlktlgy4r5uc (User created a backup of Main):
Backing up main
Backup name is <snapshot name>
[============================] 12 secs (complete)
A snapshot of environment main has been created
Snapshot name: <snapshot name>
2. Changes were made and merged to Main
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 main
.
$ platform environment:merge
Now the live site is failing:
$ curl https://main-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 main
Switched to branch 'main'
Your branch is up to date with 'platform/main'.
$ platform snapshots
Snapshots on the project <project name> (<project id>), environment Main (main):
+---------------------------+----------------------------+----------+----------+---------+
| 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 Main (main)? [Y/n] Y
Restoring snapshot <snapshot name> to Main (main)
Waiting for the restore to complete...
Waiting for the activity tb7sf5gd3ivfw (User restored environment main from backup <snapshot name>):
Provisioning certificates
Environment certificates
- certificate 7455422: expiring on 2019-06-09 17:53:35+00:00, covering main-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://main-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.