engineering

How to sync databases with GDB API

September 05, 2024

Ensuring your databases are in sync is crucial for seamless operations, whether it’s between staging and production or any other set of environments.
With GDB API, you can maintain synchronized, reliable, and up-to-date databases that closely mirror each other, ensuring smooth and efficient operations across all environments.

We’d like to point out that there’s no downtime involved in the origin database, so if you’re keeping production and development environments in sync, your production database won’t suffer from downtime.

Key GrapheneDB features for efficient synchronization

GDB API
GDB API enables you to automate the syncronization of the chosen databases with ease. You can create and restore snapshots of your databases programmatically, ensuring that your databases are always closely mirror each other.

Snapshots
Snapshots allow you to capture the state of your database at any given moment. This is incredibly useful for creating an close mirror of your environment for testing or development purposes. In addition, we offer a Snapshot policies feature, which allows you to choose the frequency and retention for the Snapshots of your databases.

How to restore data from one database to another?

In this example we will focus on how you can easily restore the production database’s data into your staging database. We’ll assume that you already have a snapshot policy in place, which ensures regular snapshots of your production database. With the snapshot readily available, you’ll be able to easily obtain the snapshot ID needed for the restoration process.

Step 1: Get Access token

curl https://api.db.graphenedb.com/organizations/oauth/token \
-d "client_secret"="SECRET" \
-d "client_id"="ID" \
-d "grant_type"="client_credentials" 

Step 2: Get Environments (to get environmentId)

curl https://api.db.graphenedb.com/deployments/environments \
-H "authorization: Bearer ACCESS_TOKEN" \
-H "content-type: application/json" 

Step 3: Get database ID

curl https://api.db.graphenedb.com/deployments/environments/{environmentId}/databases \
-H "authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json"

Step 4: Get list of Sheduled Snapshots

  • with this request you will get a list of all scheduled snapshots
curl https://api.db.graphenedb.com/deployments/databases/graphneo/{databaseId}/snapshots/scheduled \
-H "authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json"

Step 5: Get Snapshot ID

  • you would want to get the most recent sheduled snapshot of your database
  • snapshotId: the ID of the most recent scheduled snapshot
curl https://api.db.graphenedb.com/deployments/databases/graphneo/{databaseID}/snapshots/scheduled/{snapshotID} \
-H "authorization: Bearer ACCESS_TOKEN" \
-H "Content-Type: application/json"

Step 6: Restore database from Snapshot

  • databaseId: The ID of the target database
  • snapshotID: the ID of the most recent scheduled snapshot
  • snapshotEnvironmentID: The ID of the Environment, where the database is deployed from which the snapshot was taken.
curl -X POST https://api.db.graphenedb.com/deployments/databases/graphneo/{databaseId}/restore \
-H "Content-Type: application/json" \
-H "authorization: Bearer ACCESS_TOKEN" \
-d '{"snapshotId": "string","snapshotEnvironmentId": "{environmentId}"}'

What if I need to switch to backup database, in a case of some disaster?

If a server fails or another disaster occurs, having a redundant database means you have a backup ready to take over, ensuring your services keep running smoothly without long interruption. In this example we will focus on how you can easily switch connection URL from origin to backup database after the restore is done, so that your application can continue using origin connection URL. It includes same steps as our previous one, in addition to switch connection URL step that you can see the example request from below.

Step 7: Switch DNS

  • databaseId: The ID of the origin database. Its connection urls will be switched to the target database.
  • target database (databaseId): The ID of the target database.
curl -X POST https://api.db.graphenedb.com/deployments/databases/graphneo/{databaseId}/connections/switch \
-H "Content-Type: application/json" \
-H "authorization: Bearer ACCESS_TOKEN" \
-d '{"targetDatabase": "{databaseId}"}'
Try out today and get $50 in CreditsTry out today and get $50
Check
Evaluate for free
Check
Pay as you go
Check
No hidden costs