w3resource

Comprehensive Guide to pgBackRest for PostgreSQL Backup Management


Introduction to pgBackRest for PostgreSQL Backup and Restore

pgBackRest is a robust and efficient open-source tool for managing PostgreSQL backups and restores. It supports features like incremental backups, compression, encryption, and parallel processing, making it a popular choice for production-grade PostgreSQL environments.

Syntax:

The basic syntax of pgBackRest commands is:

pgbackrest [options] [command] [stanza]

Explanation:

  • Options: Additional parameters (e.g., --config)
  • Command: Operation type (e.g., backup, restore)
  • Stanza: Configuration name for a PostgreSQL cluster

Installation and Configuration

Step 1: Install pgBackRest

    1. Add the PostgreSQL repository:

    sudo sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt/ $(lsb_release -cs)-pgdg main" > /etc/apt/sources.list.d/pgdg.list'
    wget -qO - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
    sudo apt update
    

    2. Install pgBackRest:

    sudo apt install pgbackrest -y
    

Step 2: Configure pgBackRest

    1. Create the configuration directory:

    sudo mkdir -p /etc/pgbackrest
    sudo chown postgres:postgres /etc/pgbackrest
    

    2. Create the configuration file:

    sudo nano /etc/pgbackrest/pgbackrest.conf
    

    Add the following configuration:

     [my_stanza]
    pg1-path=/var/lib/postgresql/14/main
    
    [global]
    repo1-path=/var/lib/pgbackrest
    repo1-retention-full=2
    

    3. Initialize the stanza:

    sudo -u postgres pgbackrest --stanza=my_stanza --log-level-console=info stanza-create
    

Usage Examples:

Backup PostgreSQL Data

1. Run a full backup:

Code:

sudo -u postgres pgbackrest --stanza=my_stanza --log-level-console=info backup

2. Run an incremental backup:

Code:

sudo -u postgres pgbackrest --stanza=my_stanza --type=incr --log-level-console=info backup

Restore PostgreSQL Data

1. Stop the PostgreSQL service:

Code:

sudo systemctl stop postgresql

2. Restore from backup:

Code:

sudo -u postgres pgbackrest --stanza=my_stanza --log-level-console=info restore

3. Start the PostgreSQL service:

Code:

sudo systemctl start postgresql

Check Backup Status

Show backup information:

Code:

sudo -u postgres pgbackrest info

Explanation:

1. Stanza Configuration: A stanza defines a PostgreSQL instance. This ensures the tool understands where the database resides and how backups should be managed.

2. Incremental Backups: Only modified files are backed up, saving time and storage space.

3. Parallel Processing: pgBackRest uses parallelism to speed up backups and restores.

4. Data Security: It supports encrypted backups to enhance data protection.

Best Practices

  • Retention Policy: Configure retention to avoid excessive storage usage.
  • Regular Testing: Periodically test restores to ensure backups are functional.
  • Monitoring: Use monitoring tools to track backup status and issues.

All PostgreSQL Questions, Answers, and Code Snippets Collection.



Become a Patron!

Follow us on Facebook and Twitter for latest update.