> For the complete documentation index, see [llms.txt](https://jembi.gitbook.io/jempi/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://jembi.gitbook.io/jempi/backup-and-restore.md).

# Backup and Restore

Enable backup and restore for Postgres and Dgraph datastore's

## Overview

This Functionality provides detailed instructions on how to perform backup and restore operations using the JeMPI\_BackupRestoreAPI.

This is a dedicated application for handling both backup and restore operations. The scripts included in this application cover:

* Backup Dgraph using API and dump JSON file.
* Backup Postgres using SQL-Dump
* Restore Dgraph using API using JSON file.
* Restore Postgres using SQL-Dump

## Backup Operations

1. Dump sql files for Postgresql.
2. Get all GoldenIds
3. For each GoldenId:\
   a. Retrieve the golden record\
   b. Get the list of Golden Record Source Ids\
   c. Get the list of interactions\
   d. Write the data to file (JSON)

## Prerequisites

* PostgreSQL Version: Ensure that PostgreSQL 15.5.0 is installed.
* The backup and restore operations are validated on this version. Verify the installation by running psql --version

**Python Installation**

* Make sure Python and the python-dotenv package are installed to manage environment variables.
* Verify the installation by running python3 -m dotenv --version

**Using python-dotenv** Load these variables using python-dotenv:

* from dotenv import load\_dotenv
* import os
* load\_dotenv(/path/to/your/.env)
* print(os.getenv(environment variable name))
* python3 test\_dotenv.py (This should load the environment variables from your .env.local file)

## Backup Dgraph and Postgres Process

* **Backup Directory Dgraph:**\
  JeMPI/devops/linux/docker/docker\_data/data/backups/dgraph
* **Backup Directory Postgres:**\
  JeMPI/devops/linux/docker/docker\_data/data/backups/postgres
* **Deployment File:** local-deployment.sh

```bash
cd JeMPI/devops/linux/docker/deployment
./local-deployment.sh
Select Option 5: Backup Postgres & Dgraph
```

* **Backup Script Path:**\
  JeMPI/devops/linux/docker/backup\_restore/dgraph-backup-api.sh
* **Backup Script Logic Dgraph:**\
  JeMPI/devops/linux/docker/backup\_restore/dgraph-backup-api.py
* **Backup Script Logic Postgres:**\
  JeMPI/devops/linux/docker/backup\_restore/postgres-backup.sh

The backup process creates a folder with a timestamp. Inside this folder, backups are created for each Dgraph and Postgres.

![Backup Postgres and Dgraph](https://220714941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQSuKUyYfw2QNaNiZIQ1s%2Fuploads%2Fgit-blob-7338a14fdfc2d8bf0156dd916aa74ae5c49cf094%2F14?alt=media)

**Manual Backup Process**

* pg\_dump -U -d \<database\_name> > /path/to/backup\_file.sql

**Verify if process was successful**

* echo $? (This variable holds the exit status of the last command executed. An exit status of 0 indicates that the last command (pg\_dump) completed successfully.)
* ls -lh

## Restore Dgraph and Postgres Process

* **Backup Directory:** JeMPI/devops/linux/docker/docker\_data/data/backups/dgraph
* **Deployment File:** local-deployment.sh

```bash
cd JeMPI/devops/linux/docker/deployment
./local-deployment.sh
Select Option 6: Restore Postgres & Dgraph
```

* It will prompt for confirmation (yes/no) and list the recent 5 backup folders.
* **Enter the backup folder name** - it will start the restoration from the selected backup file.
* **Backup Directory Dgraph:** JeMPI/devops/linux/docker/docker\_data/data/backups/dgraph
* **Backup Directory Postgres:** JeMPI/devops/linux/docker/docker\_data/data/backups/postgres
* **Manual Backup Run Script:** ./restore-dgraph-postgres.sh {{ Folder\_Name }}

![Restore Postgres and Dgraph](https://220714941-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FQSuKUyYfw2QNaNiZIQ1s%2Fuploads%2Fgit-blob-1992d9f17cbce20d787d045b7fe7f5b66371d8e7%2F15?alt=media)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://jembi.gitbook.io/jempi/backup-and-restore.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
