How to backup Hasura

elitasson's avatar on Twitter
Johan Eliasson

I don't really need to tell you the importance of backups. You are already here. But just to make it obvious why you need to backup Hasura:

Now your motivation to backup your Hasura (and PostgreSQL) is through the roof!

Let's get into the details.

Through out this post, make sure to:

  • change the Hasura URL to your Hasura URL.
  • change the <your-admin-secret> to your actual Hasura Admin Secret.

A Hasura backup consists of three parts.

  1. Database schema
  2. Database data
  3. Hasura metadata

Backup database schema and data

To backup both the database schema and data we can use Hasura's /v1alpha1/pg_dump endpoint. This gives us direct access to pg_dump on our Hasura / PostgreSQL instance.

This request will return a full backup of the schema and data:

POST /v1alpha1/pg_dump HTTP/1.1
Content-Type: application/json
x-hasura-admin-secret: <your-admin-secret>

  "opts": ["-O", "-x", "--schema", "public", "--schema", "auth"],
  "clean_output": true

We can turn this into a cURL command that we can run in the terminal or in a script:

curl --location --request POST 'https://hasura-xxx.nhost.app/v1alpha1/pg_dump' \
--header 'x-hasura-admin-secret: <your-admin-secret>' \
--header 'Content-Type: application/json' \
--data-raw '{
  "opts": ["-O", "-x", "--schema", "public", "--schema", "auth"],
  "clean_output": true
}' -o backup.sql

This command will do a full backup of the database schema and the database data and save everything in a backup.sql file. The database schema and data are now backed up and safe.

Backup Hasura metadata

Hasura metadata is all the information of the GraphQL API, Even triggers, Remote schemas, Actions etc. This is also very important to backup.

To backup the Hasura metadata we'll use the Hasura CLI.

Make sure you got the CLI installed:

curl -L https://github.com/hasura/graphql-engine/raw/stable/cli/get.sh | bash

Create a file config.yaml with this content:

version: 2
endpoint: https://hasura-xxx.nhost.app
admin_secret: <your-admin-secret>

Now execute hasura metadata export.

All of Hasura's metadata is now backed up in metadata/metadata.yaml.

Awesome. You're done!

You now got a backup of your Hasura instance and you'll sleep much better tonight.

Try Nhost for Free Today

Get a perfect backend with Hasura
Get started