pgBackRest
pgBackRest is a PostgreSQL backup tool with S3 support for Linux.
Customize pgBackRest
- Configure access to S3.
- Configure PostgreSQL to archive the WAL.
- Install pgBackRest.
- Create a configuration and set up backups.
1. Set up access to S3
Access can be configured by the Account Owner or a user with the role of iam_admin.
- Create a service user with role with access to S3. If you have created a service user with the role object_storage_user or
s3.bucket.user, the bucket must have an access access policy. - Issue an S3 key to the user.
2. Configure PostgreSQL for WAL archiving
-
Open the CLI.
-
Open the
postgresql.conffile:sudo nano /etc/postgresql/16/main/postgresql.conf -
Set the values of the parameters:
wal_level = replica
archive_mode = on
archive_command = 'pgbackrest --stanza=main archive-push %p'
max_wal_senders = 3 -
Remove the
#before each of the listed parameters and the comments after them. -
To exit the nano text editor while saving your changes, press Ctrl+X → Y+Enter.
-
Apply the changes:
sudo systemctl restart postgresql
3. Install pgBackRest
Install pgBackRest:
sudo apt install -y pgbackrest
4. Create a configuration and set up backups
-
Create a configuration directory:
sudo mkdir -p /etc/pgbackrest -
Open the configuration file
/etc/pgbackrest.conf:nano /etc/pgbackrest.conf -
Complete the
/etc/pgbackrest.conffile:[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=infoSpecify:
<postgresql_version>- PostgreSQL data catalog;<s3_domain>- S3 API domain depends on pool in which S3 is located;<bucket_name>- S3 bucket name. The bucket must have vHosted-addressing;<pool>- pool where S3 is located;<access_key>- field value Access key from S3 key;<secret_key>- field value Secret key from the S3 key;<path_to_repository>- the root of the pgBackRest repository;<number_of_backups>- number of backups stored simultaneously.
-
To exit the nano text editor while saving your changes, press Ctrl+X → Y+Enter.
-
Make the
postgresuser the owner of the configuration:sudo chown -R postgres:postgres /var/lib/pgbackrest
sudo chown postgres:postgres /etc/pgbackrest/pgbackrest.conf -
Initialize
stanza, a logical unit for pgBackRest that combines the cluster and the repository:sudo -u postgres pgbackrest --stanza=main --log-level-console=info stanza-create
sudo -u postgres pgbackrest --stanza=main --log-level-console=info checkHere:
stanza-create- checks the data catalog and repository, creates metadata for the backup;check- confirms that the setting is correct.
Working with pgBackRest
Create a full backup
-
Open the CLI.
-
Create a full backup:
sudo -u postgres pgbackrest --stanza=main --type=full backup
Create an incremental backup
At least one full backup must be done to perform an incremental backup.
-
Open the CLI.
-
Create an incremental backup:
sudo -u postgres pgbackrest --stanza=main --type=incr --log-level-console=info backup
View information about saved backups
-
Open the CLI.
-
Look at the information about the backups that have been saved:
sudo -u postgres pgbackrest --stanza=main info
Restore a cluster from backup
-
Open the CLI.
-
Stop PostgreSQL before restoring the cluster:
sudo systemctl stop postgresql -
Restore backup - the last backup created is automatically selected:
sudo -u postgres pgbackrest --stanza=main restore -
Start the PostgreSQL job:
sudo systemctl start postgresql -
Check the list of clusters:
sudo pg_lsclusters -
Check the list of databases:
sudo -u postgres psql -c "\l"