Backup and Restore Database

This page describes how to backup and restore the database.

Before you begin

For this task, you need to have PostgreSQL installed on your machine.

You also need to have enough space on your machine to store the backup. To check the size of the database, follow the steps below:

  1. From the Pod section in UMHLens / OpenLens, click on united-manufacturing-hub-timescaledb-0 to open the details page.

  2. Click the Pod Shell button to open a shell in the container.

    Lens Pod Shell
    Lens Pod Shell

  3. Enter the postgres shell:

    psql
    
  4. Connect to the database:

    \c factoryinsight
    
  1. Run the following command to get the size of the database:

    SELECT pg_size_pretty(pg_database_size('factoryinsight'));
    

Backing up the database

Follow these steps to create a backup of the factoryinsight database on your machine:

  1. Open a terminal, and using the cd command, navigate to the folder where you want to store the backup. For example:

    
       cd C:\Users\user\backups
       

    
       cd /Users/user/backups
       

    
       cd /home/user/backups
       

    If the folder does not exist, you can create it using the mkdir command or your file manager.

  2. Run the following command:

    pg_dump -h <REMOTE_HOST> -p 5432 -U factoryinsight -Fc -f <BACKUP_NAME>.bak factoryinsight
    
    • <REMOTE_HOST> is the IP of the server where the database is running. Use localhost if you installed the United Manufacturing Hub using k3d.
    • <BACKUP_NAME> is the name of the backup file.

Grafana database

If you want to backup the Grafana database, you can follow the same steps as above, but you need to replace any occurence of factoryinsight with grafana.

Additionally, you also need to write down the credentials in the grafana-secret Secret, as they will be needed to access the dashboard after restoring the database.

Restoring the database

This section is untested. Please report any issues you encounter.

For this section, we assume that you are restoring the data to a fresh United Manufacturing Hub installation with an empty database.

Copy the backup file to the database pod

  1. Open UMHLens / OpenLens.

  2. Launch a new terminal sesstion by clicking on the + button in the bottom-left corner of the window.

  3. Run the following command to copy the backup file to the database pod:

    kubectl cp /path/to/local/backup.bak united-manufacturing-hub-timescaledb-0:/tmp/backup.bak
    

    Replace /path/to/local/backup.bak with the path to the backup file on your machine.

This step could take a while depending on the size of the backup file.

Temporarly disable kafkatopostrgesql

  1. Navigate to Workloads > Deployments.
  2. Select the united-manufacturing-hub-kafkatopostgresql Deployment.
  3. Click the Scale button to select the number of replicas for the Deployment.

    Lens Deployment Scale
    Lens Deployment Scale

  4. Scale the number of replicas to 0.

Open a shell in the database pod

  1. From the Pod section in UMHLens / OpenLens, click on united-manufacturing-hub-timescaledb-0 to open the details page.

  2. Click the Pod Shell button to open a shell in the container.

    Lens Pod Shell
    Lens Pod Shell

  3. Enter the postgres shell:

    psql
    
  4. Connect to the database:

    \c factoryinsight
    

Restore the database

  1. Drop the existing database:

    DROP DATABASE factoryinsight;
    
  2. Create a new database:

    CREATE DATABASE factoryinsight;
    \c factoryinsight
    CREATE EXTENSION IF NOT EXISTS timescaledb;
    
  3. Put the database in maintenance mode:

    SELECT timescaledb_pre_restore();
    
  4. Restore the database:

    \! pg_restore -Fc -d factoryinsight /tmp/backup.bak
    
  5. Take the database out of maintenance mode:

    SELECT timescaledb_post_restore();
    

Enable kafkatopostgresql

  1. Navigate to Workloads > Deployments.
  2. Select the united-manufacturing-hub-kafkatopostgresql Deployment.
  3. Click the Scale button to select the number of replicas for the Deployment.

    Lens Deployment Scale
    Lens Deployment Scale

  4. Scale the number of replicas to the original value, usually 1.

What’s next

Last modified April 17, 2023: build: version 0.9.13 (6cb0a01)