---
title: "pgBackRest"
sidebar_label: "pgBackRest"
sidebar_position: 12
description: "How to set up pgBackRest and work with S3"
---

import PlusIcon from '@selectel/docux/icons/plus'
import Formbricks from '@theme/MDXComponents/Formbricks'
import GrantAccess from '@site/i18n/en/docusaurus-plugin-content-docs/current/_partials/s3/grant-access.mdx'

# pgBackRest

pgBackRest is a tool for backing up PostgreSQL with S3 support for Linux OS.

## Set up pgBackRest \{#configure-pgbackrest}

1. [Configure access to S3](#configure-s3-access).
2. [Configure PostgreSQL for WAL archiving](#configure-postgresql).
3. [Install pgBackRest](#install-pgbackrest).
4. [Create the configuration and set up backups](#create-configuration-and-configure-backup).

### 1. Configure access to S3 \{#configure-s3-access}

<GrantAccess />

### 2. Configure PostgreSQL for WAL archiving \{#configure-postgresql}

1. Open the CLI.

2. Open the `postgresql.conf` file:

   ```bash
   sudo nano /etc/postgresql/16/main/postgresql.conf
   ```

3. Set the parameter values:

   ```bash
   wal_level = replica
   archive_mode = on
   archive_command = 'pgbackrest --stanza=main archive-push %p'
   max_wal_senders = 3
   ```

4. Remove the `#` before each of the listed parameters and the comments after them.

5. To exit the nano text editor and save changes, press **Ctrl+X** → **Y+Enter**.

6. Apply the changes:

   ```bash
   sudo systemctl restart postgresql
   ```

### 3. Install pgBackRest \{#install-pgbackrest}

Install pgBackRest:

```bash
sudo apt install -y pgbackrest
```

### 4. Create the configuration and set up backups \{#create-configuration-and-configure-backup}

1. Create the configuration directory:

   ```bash
   sudo mkdir -p /etc/pgbackrest
   ```

2. Open the configuration file `/etc/pgbackrest.conf`:

   ```bash
   nano /etc/pgbackrest.conf
   ```

3. Fill out the file `/etc/pgbackrest.conf`:

   ```bash
   [main]
   pg1-path=/var/lib/postgresql/<postgresql_version>/main   

   [global]
   repo1-type=s3
   repo1-s3-endpoint=<s3_domain>
   repo1-s3-bucket=<bucket_name>
   repo1-s3-region=<pool>
   repo1-s3-key=<access_key>
   repo1-s3-key-secret=<secret_key>
   repo1-path=<path_to_repository>
   repo1-retention-full=<number_of_backups>
   log-level-console=info
   ```

   Specify:

   * `<postgresql_version>` – PostgreSQL data directory;
   * `<s3_domain>` – [S3 API domain](/s3/manage/domains.mdx#s3-api-domains). The domain depends on the [pool](/infrastructure/locations.mdx#pool) where S3 is located;
   * `<bucket_name>` – S3 bucket name. The bucket must have vHosted addressing;
   * `<pool>` – [pool](/infrastructure/locations.mdx#pool) where S3 is located;
   * `<access_key>` – value of the **Access key** field from the [S3 key](/access-control/manage/edit-user-data-or-role.mdx#issue-s3-key);
   * `<secret_key>` – value of the **Secret key** field from the S3 key;
   * `<path_to_repository>` – pgBackRest repository root;
   * `<number_of_backups>` – number of simultaneously stored backups.

4. To exit the nano text editor and save changes, press **Ctrl+X** → **Y+Enter**.

5. Set the `postgres` user as the owner of the configuration:

   ```bash
   sudo chown -R postgres:postgres /var/lib/pgbackrest
   sudo chown postgres:postgres /etc/pgbackrest/pgbackrest.conf
   ```

6. Initialize the `stanza`, a logical unit for pgBackRest that combines a cluster and a repository:

   ```bash
   sudo -u postgres pgbackrest --stanza=main --log-level-console=info stanza-create
   sudo -u postgres pgbackrest --stanza=main --log-level-console=info check
   ```

   Where:

   * `stanza-create` — checks the data directory and repository, creates metadata for a backup;
   * `check` — confirms that the setup is correct.

## Working with pgBackRest \{#work-with-pgbackrest}

### Create a full backup \{#create-full-backup}

1. Open the CLI.
2. Create a full backup:

   ```bash
   sudo -u postgres pgbackrest --stanza=main --type=full backup
   ```

### Create an incremental backup \{#create-incremental-backup}

To perform an incremental backup, at least one [full backup must be created](#create-full-backup).

1. Open the CLI.
2. Create an incremental backup:

   ```bash
   sudo -u postgres pgbackrest --stanza=main --type=incr --log-level-console=info backup
   ```

### View information about stored backups \{#view-info-about-saved-backups}

1. Open the CLI.
2. View information about stored backups:

   ```bash
   sudo -u postgres pgbackrest --stanza=main info
   ```

### Restore a cluster from a backup \{#restore-cluster-from-backup}

1. Open the CLI.

2. Before restoring a cluster, stop PostgreSQL:

   ```bash
   sudo systemctl stop postgresql
   ```

3. Restore the backup — the last created backup is selected automatically:

   ```bash
   sudo -u postgres pgbackrest --stanza=main restore
   ```

4. Start PostgreSQL:

   ```bash
   sudo systemctl start postgresql
   ```

5. Check the list of clusters:

   ```bash
   sudo pg_lsclusters
   ```

6. Check the list of databases:

   ```bash
   sudo -u postgres psql -c "\l"
   ```

<Formbricks />
