Back Up & Restore External Config Database

Overview

When you install NGINX Controller, you have the option to configure your own external PostgreSQL config database. This guide explains how to back up and restore the external config database.

Before You Begin

To backup and restore the external config database, you’ll need the following:

  • Login credentials for your NGINX Controller PostgreSQL database
  • A connection to your NGINX Controller PostgreSQL database
  • psql and pg_dump installed on the server where you’ll be performing the backup or restore

Set the PostgreSQL Environment Variables

  1. Log in to the NGINX Controller host using SSH.

  2. Set the following environment variables using the credentials for your NGINX Controller PostgreSQL database:

    export PGHOST=<postgres host>
    export PGPORT=5432
    export PGUSER=<postgres user>
    export PGPASSWORD=<postgres password>
    
    Note:
    If you’ve configured PostgreSQL to use SSL, ensure that you’ve placed your certs in ~/.postgresql. For more information, see Client Certificates in the PostgreSQL documentation.

 


Back Up External Config Database

Take the following steps to back up the external NGINX Controller config database:

  1. Stop NGINX Controller:

    /opt/nginx-controller/helper.sh controller stop
    
  2. Run the following script to back up the NGINX Controller database. The backup files are saved in a directory that looks like pgbackup_<timestamp>.

    DATE=$(date +"%Y%m%d%H%M")
    mkdir ~/pgbackup_${DATE}
    
    for db in common data system; do
      pg_dump -w -E utf8 -d ${db} -F c -f ~/pgbackup_${DATE}/${db}-${DATE}.backup
    done
    
  3. Start NGINX Controller:

    /opt/nginx-controller/helper.sh controller start
    

 


Restore External Config Database

Important:
If you restore the config database on top of a new installation of NGINX Controller, make sure to follow the steps to restore your NGINX config and encryption keys afterward.

To restore the external NGINX Controller config database:

  1. Stop NGINX Controller:

    /opt/nginx-controller/helper.sh controller stop
    
  2. Locate the backup directory and save the name as a local environment variable. The name of the backup directory follows the format pgbackup_<timestamp>.

    BACKUP_PATH=~/pgbackup_<timestamp>
    
  3. Run the restore script:

    for backup_file in "$BACKUP_PATH"/*.backup; do
      db="$(basename "$backup_file" | cut -d '-' -f 1)"
      pg_restore -c -C -d "$db" "$backup_file"
    done
    
  4. Start NGINX Controller:

    /opt/nginx-controller/helper.sh controller start
    

 


What’s Next


This documentation applies to the following versions of NGINX Controller Documentation: 3.0, 3.1, 3.2, 3.3, 3.4, 3.5, 3.6, 3.7, 3.8, 3.9, 3.10, 3.11, 3.12, 3.13, 3.14, 3.15 and 3.16.