2020-09-09

How to backup Hasura

elitasson
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", "--inserts"],
  "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", "--inserts"],
  "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 got to the Hasura console and click the gearwheel in the top right corner.

Then click Export metadata.

Save the hasura_metadata_[...].json on your computer.

Your Hasura metadata is now backed up.


Awesome. You're done!

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

What's next?
Did you find this blog post interesting? Follow me on Twitter where I write more about my journey with Nhost.
Try Nhost for Free Today
Focus on your app and your users!

The Jamstack backend with Hasura.

Product
Build
Resources
© Nhost
githubtwitterdiscordyoutubelinkedin