Skip to content

Perception Database Backup

Introduction

This guide explains how to back up and restore the Perception deployment.

We’ll go through the following steps:

  1. Create a backup of your database inside the Docker container.
  2. Copy the backup file to your host machine.
  3. Restore the database from this backup if needed.

Create Backup

  1. Create the backup file inside the Docker Container: Create a database dump in the database container of perception:

    docker exec perception-database-server-1 pg_dump -U perception -Fc -f backup.bak perceptiondb 
    
  2. Copy the backup file to the host machine: After creating the backup file, copy it to the current directory on the host machine:

    docker cp perception-database-server-1:/backup.bak .
    
    3. Backup the file to a safe location. We recommend a location on a different device.

  3. (Optional) Enable this backup as a cron job


Restore from Backup

To restore the database:

  1. Copy the Backup File to the Docker Container:

    docker cp backup.bak perception-database-server-1:/
    
  2. Access the Docker Container’s Shell:

    docker exec -it perception-database-server-1 /bin/bash
    
  3. Connect to PostgreSQL and Prepare for Restoration:

    psql -U perception perceptiondb
    
  4. Drop and Recreate the Database:

    \c postgres 
    DROP DATABASE perceptiondb; 
    CREATE DATABASE perceptiondb;
    
  5. Run TimescaleDB Pre-Restoration Command:

    SELECT timescaledb_pre_restore();
    
  6. Restore the Backup:

    \! pg_restore -Fc -U perception -d perceptiondb /backup.bak
    
  7. Finalize the Restore with TimescaleDB:

    SELECT timescaledb_post_restore();
    

Code for Copy Paste

# Create backup
docker exec perception-database-server-1 pg_dump -U perception -Fc -f backup.bak perceptiondb
docker cp perception-database-server-1:/backup.bak .

# Restore from backup
docker cp backup.bak perception-database-server-1:/
docker exec -it perception-database-server-1 /bin/bash
psql -U perception perceptiondb

\c postgres
DROP DATABASE perceptiondb;
CREATE DATABASE perceptiondb;
SELECT timescaledb_pre_restore();
\! pg_restore -Fc -U perception -d perceptiondb backup.bak
SELECT timescaledb_post_restore();