How to perform a database dump via Platform.sh CLI

Goal

Perform a database dump using the platform CLI tool.

Problems

Sometimes it is necessary to do a database dump of a project. This can be especially useful when doing some testing, planning a migration or for recovery purposes.

Platform CLI can be used to backup the database with a single command.

Steps

In general, the following command will be used to perform a database dump using the Platform CLI:

Usage: platform db:dump [-f|--file FILE] [-t|--timestamp] [--stdout] [-p|--project PROJECT] [--host HOST] [-e|--environment ENVIRONMENT] [-A|--app APP]

1. Single Database

For a single configured database, use the command below:

$ platform db:dump
Creating SQL dump file:/Users/my_user/platformsh-enterprise/hiera/mysqldb--main--dump.sql

2. Multiple Databases

Running the previous command with multiple databases, choosing a relationship will be prompted by the CLI:

$ platform db:dump
Enter a number to choose a relationship:
  [0] postgresql (main@postgresql.internal)
  [1] database (user@database.internal)

Dumping the MySQL database (database, in this case) can be chosen by selecting 1. database can be chosen directly ahead of time with the CLI with the --relationship flag:

$ platform db:dump --relationship database

Further options can be specified, as per the example below:

$ platform db:dump --relationship database -p project_id -e environment_name -A app

To find the platform relationship details, SSS into the environment and run:

$ echo $PLATFORM_RELATIONSHIPS | base64 --decode | json_pp

3. Compression

By default the dump file will be uncompressed. To compress it, use the --gzip ( -z ) option:

platform db:dump --gzip

4. Troubleshooting

Error output: Permission denied (publickey).

This error can sometimes show up, after running the db dump command. If this happens, try the following:

  1. Redeploy the environment the user is trying to access.
  2. The SSH key may be improperly configured. Check with platform ssh-key:list and platform ssh-key:add if necessary.
  3. Remove the user from the project, then add them back and redeploy the environment.

Conclusion

Database dumps can be easily done by using Platform.sh CLI.