How to create and restore backups using the CLI

Goal

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

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 backups 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 backups. Creation and restoration of backups can be executed from the UI and using the CLI.

Additional information can be found in the public documentation

Steps

1. Create a backup

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 backup using the Platform.sh CLI if one has not already been created.

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

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

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

List the saved backups to retrieve the backup name.

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

4. Restore the backup

The backup can be restored to the original environment:

$ platform backup:restore <backup name>
Are you sure you want to restore the backup <backup name> from 2019-03-11T15:16:16-04:00 to environment Master (master)? [Y/n] Y
Restoring backup <backup name> to Master (master)
Waiting for the restore to complete...
Waiting for the activity tb7sf5gd3ivfw (User restored environment master from backup <backup 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 backup was successfully restored

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

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

5. Verify the restoration was successful

Check that the backup 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 backup is created, undesired changes can be reverted on an active branch using the Platform.sh CLI.