PostgreSQL incremental backups in pgBackRest is a backup method that allows you to save only changed data since the last full or incremental backup. Helps save disk space and reduce backup time.
Links to resources:
You can set pgBackRest with teams or script.
Installing pgBackRest
Select one of the installation methods:
through the repository,
from sources.
Installation from repository
sudo apt install pgbackrest postgresql-client
Installation from source
Astra Linux does not have a package to install pgBackRest, so build it from source.
Note
Successful results are influenced by accurate timing. We recommend that you read the official Astra Linux documentation <https://wiki.astralinux.ru/pages/viewpage.action?pageId=27361687>`_.
First make sure you have Perl installed:
apt list perl
Install packages.
The official build documentation lists the following packages:
meson gcc libpq-dev libssl-dev libxml2 libxml2-dev pkg-config liblz4-dev libzstd-dev libbz2-dev libz-dev libyaml-dev libssh2 libssh2-1-dev postgresql-clientMake sure all packages are in the Astra Linux repository:
for pkg in meson gcc libpq-dev libssl-dev libxml2 libxml2-dev pkg-config liblz4-dev libzstd-dev libbz2-dev libz-dev libyaml-dev libssh2 libssh2-1-dev postgresql-client; do apt-cache show "$pkg" > /dev/null 2>&1 && echo "$pkg: найден" || echo "$pkg: не найден"; doneConclusion:
meson: найден gcc: найден libpq-dev: найден libssl-dev: найден libxml2: найден libxml2-dev: найден pkg-config: найден liblz4-dev: найден libzstd-dev: найден libbz2-dev: найден libz-dev: найден libyaml-dev: найден libssh2: не найден libssh2-1-dev: найден postgresql-client: найденThe unfound package libssh2 has a vulnerability. Description on the FSTEC website.
Apparently, the package has been fixed and has changed its name to libssh2-1:
![]()
Build pgBackRest without libssh2 because it is already installed:
sudo apt install meson gcc libpq-dev libssl-dev libxml2 libxml2-dev pkg-config liblz4-dev libzstd-dev libbz2-dev libz-dev libyaml-dev libssh2-1-dev postgresql-client
Building pgBackRest
Use the Meson and Ninja assembly system for assembly.
Build the version that is used in the Ubuntu repositories:
sudo mkdir /build && cd /build sudo wget https://github.com/pgbackrest/pgbackrest/archive/refs/tags/release/2.53.tar.gz sudo tar -xvf 2.53.tar.gz sudo meson setup /build/pgbackrest /build/pgbackrest-release-2.53/ sudo ninja -C /build/pgbackrestExample output for successful build and compilation:
provisor@iBatyr Monitor-54:/build$ sudo meson setup /build/pgbackrest /build/pgbackrest-release-2.53/ The Meson build system Version: 0.53.2 Source dir: /build/pgbackrest-release-2.53 Build dir: /build/pgbackrest Build type: native build WARNING: Unknown options: "unity_size" Project name: pgbackrest Project version: 2.53 C compiler for the host machine: cc (gcc 9.4.0 "cc (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0") C linker for the host machine: cc ld.bfd 2.34 Host machine cpu family: x86_64 Host machine cpu: x86_64 Compiler for C supports arguments -Wconversion: YES Compiler for C supports arguments -Wduplicated-cond: YES Compiler for C supports arguments -Wduplicated-branches: YES Compiler for C supports arguments -Wformat-nonliteral: YES Compiler for C supports arguments -Wformat=2: YES Compiler for C supports arguments -Wformat-signedness: YES Compiler for C supports arguments -Wpointer-arith: YES Compiler for C supports arguments -Wstrict-prototypes: YES Compiler for C supports arguments -Wvla: YES Compiler for C supports arguments -Wwrite-strings: YES Compiler for C supports arguments -Wno-clobbered -Wclobbered: YES Compiler for C supports arguments -Wno-missing-field-initializers -Wmissing-field-initializers: YES Compiler for C supports arguments -Wno-implicit-fallthrough -Wimplicit-fallthrough: YES Compiler for C supports arguments -funroll-loops: YES Compiler for C supports arguments -ftree-vectorize: YES Program python3 found: YES (/usr/bin/python3) Compiler for C supports arguments -fmacro-prefix-map=../pgbackrest-release-2.53/src/=: YES Compiler for C supports arguments -fmacro-prefix-map=../pgbackrest-release-2.53/test/src/=test/: YES Library backtrace found: YES Library bz2 found: YES Found pkg-config: /usr/bin/pkg-config (0.29.1) Run-time dependency liblz4 found: YES 1.9.2 Run-time dependency openssl found: YES 1.1.1f Run-time dependency libpq found: YES 16.4 Run-time dependency libxml-2.0 found: YES 2.9.10 Run-time dependency yaml-0.1 found: YES 0.2.2 Run-time dependency zlib found: YES 1.2.11 Run-time dependency libssh2 found: YES 1.8.0 Run-time dependency libzstd found: YES 1.4.4 Configuring build.auto.h using configuration Configuring build.auto.h using configuration Configuring build.auto.h using configuration Build targets in project: 10 Found ninja-1.10.0 at /usr/bin/ninja provisor@iBatyr Monitor-54:/build$ sudo ninja -C /build/pgbackrest ninja: Entering directory `/build/pgbackrest' [250/250] Linking target src/pgbackrest.
Transfer the resulting binary file:
sudo cp /build/pgbackrest/src/pgbackrest /usr/bin/ sudo chmod 755 /usr/bin/pgbackrest
Create directories for configuration files and logs:
sudo mkdir -p -m 770 /var/log/pgbackrest && sudo chown postgres:postgres /var/log/pgbackrest && sudo mkdir -p /etc/pgbackrest && sudo mkdir -p /etc/pgbackrest/conf.d && sudo touch /etc/pgbackrest/pgbackrest.conf && sudo chmod 640 /etc/pgbackrest/pgbackrest.conf && sudo chown postgres:postgres /etc/pgbackrest/pgbackrest.conf
Create directories for the backup repository.
Warning
The directories are provided as an example. You can specify your own directories, or mount backup drives to the specified directories.
sudo mkdir -p /var/lib/pgbackrest sudo chmod 750 /var/lib/pgbackrest sudo chown postgres:postgres /var/lib/pgbackrest
Verifying the installation
Check installation:
sudo -u postgres pgbackrest
Conclusion:
pgBackRest 2.53 - General help
Usage:
pgbackrest [options] [command]
Commands:
annotate Add or modify backup annotation.
archive-get Get a WAL segment from the archive.
archive-push Push a WAL segment to the archive.
backup Backup a database cluster.
check Check the configuration.
expire Expire backups that exceed retention.
help Get help.
info Retrieve information about backups.
repo-get Get a file from a repository.
repo-ls List files in a repository.
restore Restore a database cluster.
server pgBackRest server.
server-ping Ping pgBackRest server.
stanza-create Create the required stanza data.
stanza-delete Delete a stanza.
stanza-upgrade Upgrade a stanza.
start Allow pgBackRest processes to run.
stop Stop pgBackRest processes from running.
verify Verify contents of the repository.
version Get version.
Use 'pgbackrest help [command]' for more information.
If there are no errors, pgBackRest is installed correctly.
Setting up pgBackRest
Edit the configuration file /etc/pgbackrest/pgbackrest.conf in an editor convenient for you.
Warning
Be careful when editing the file. PostgreSQL versions may vary.
[demo]
pg1-path=/var/lib/postgresql/15/main
[global]
repo1-path=/var/lib/pgbackrest
repo1-retention-full=2
[global:archive-push]
compress-level=3
Setting up PostgreSQL
Add lines to the end of the /etc/postgresql/15/main/postgresql.conf file. It is highly recommended to add a comment so that in the future it is clear why these changes were made.
#2.09.2024 settings for incremental backup. System administrator Ivanov I. archive_command = 'pgbackrest --stanza=demo archive-push %p' archive_mode = on max_wal_senders = 3 wal_level = replica
Restart PostgreSQL:
sudo systemctl restart postgresql
Make sure PostgreSQL starts after making changes to the configuration file:
sudo systemctl status postgresqlCorrect output:
provisor@astr-sce54:/build$ sudo systemctl status postgresql ● postgresql.service - PostgreSQL RDBMS Loaded: loaded (/lib/systemd/system/postgresql.service; enabled; vendor preset: enabled) Active: active (exited) since Mon 2024-09-02 17:15:48 +07; 9s ago Process: 3903 ExecStart=/bin/true (code=exited, status=0/SUCCESS) Main PID: 3903 (code=exited, status=0/SUCCESS) CPU: 7ms сен 02 17:15:48 astr-sce54 systemd[1]: Starting PostgreSQL RDBMS... сен 02 17:15:48 astr-sce54 systemd[1]: Started PostgreSQL RDBMS.
Running the pgBackRest package for the cluster
Use the command to run:
sudo -u postgres pgbackrest --stanza=your_stanza_name --log-level-console=info stanza-createNote
stanzais a PostgreSQL database cluster configuration that determines the location of the cluster. how to create a backup, archiving options, etc. Most database servers have only one PostgreSQL database cluster and therefore one stanza. At the same time, backup servers can have a separatestanzaconfiguration for each database cluster that they serve.The following command uses
your_stanza_nameas the name for thestanzaconfiguration.Do not name
stanzawith the name of the primary cluster. A more appropriate name describes the databases that are part of the cluster. Since the namestanzais used for the primary and all replicas, it is more appropriate to choose a name that describes the actual function of the cluster. For example,appordwinstead of a local cluster name likemainorprod.Conclusion:
sudo -u postgres pgbackrest --stanza=demo --log-level-console=info stanza-create 2024-05-28 13:20:12.820 P00 INFO: stanza-create command begin 2.51: --exec-id=40337-0602407f --log-level-console=info --pg1-path=/var/lib/postgresql/15/main --repo1-path=/var/lib/pgbackrest --stanza=demo 2024-05-28 13:20:13.626 P00 INFO: stanza-create for stanza 'demo' on repo1 2024-05-28 13:20:13.633 P00 INFO: stanza-create command end: completed successfully (814ms)
Check out the created repository:
sudo -u postgres pgbackrest --stanza=demo --log-level-console=info checkConclusion:
sudo -u postgres pgbackrest --stanza=demo --log-level-console=info check 2024-05-28 13:24:03.434 P00 INFO: check command begin 2.51: --exec-id=40938-d6c55df1 --log-level-console=info --pg1-path=/var/lib/postgresql/15/main --repo1-path=/var/lib/pgbackrest --stanza=demo 2024-05-28 13:24:04.041 P00 INFO: check repo1 configuration (primary) 2024-05-28 13:24:04.443 P00 INFO: check repo1 archive for WAL (primary) 2024-05-28 13:24:04.443 P00 INFO: WAL segment 0000000100000003000000B9 successfully archived to '/var/lib/pgbackrest/archive/demo/15-1/0000000100000003/0000000100000003000000B9-f099b13a5bce53307041b204c4 edd5215e2402b0.gz' on repo1 2024-05-28 13:24:04.444 P00 INFO: check command end: completed successfully (1011ms)
Creating a backup
An incremental backup should be based on a full backup. Since a full backup does not exist, you need to make one. While creating the backup, stop receiving data from the agent.
Warning
Before making a backup, it is extremely important to shut down iBatyr Monitor and make sure that all processes are stopped. The fact is that even after blocking the reception of data from agents via Iptables or stopping Nginx, ETL processes can continue inside, which will change the state of the database.
Stop data transfer to the iBatyr Monitor server:
ibatyr stop
Make a full backup - this is the first complete copy of all data, which serves as the basis for future incremental backups:
sudo -u postgres pgbackrest --stanza=demo --type=full backup
Create an incremental backup - backing up only those changes that have occurred since the last full backup:
sudo -u postgres pgbackrest --stanza=demo--type=incr backup
Additional findings
pgBackRest supports extended output when creating backups, which allows you to get more detailed information about the process. In the previous examples, additional output was not specified, but it can be added for more informative monitoring.
Use the --log-level-console parameter to control the level of verbosity of the console output.
The main logging levels you can use are:
error: Shows only errors.warn: Shows warnings and errors.info: Shows informational messages, warnings and errors.detail: Shows detailed messages, informational messages, warnings and errors.debug: Shows debug messages, detail messages, informational messages, warnings and errors.trace: Shows trace messages, debug messages, detail messages, informational messages, warnings and errors.
Examples of logging levels
If you want to create a backup with extended output, it is recommended to use the level detail or debug.
Level detail
sudo pgbackrest --stanza=your_stanza_name --log-level-console=detail backup
Level debug
For more granularity use debug:
sudo pgbackrest --stanza=your_stanza_name --log-level-console=debug backup
Level trace
Use trace for maximum level of detail:
sudo pgbackrest --stanza=your_stanza_name --log-level-console=trace backup
Setting the logging level in the configuration file
Instead of executing the command, you can set the logging level in the pgbackrest.conf configuration file. For example:
[global]
log-level-console=detail
View backup
To view the backup, do:
sudo -u postgres pgbackrest info
Conclusion:
sudo -u postgres pgbackrest info
stanza: demo
status: ok
cipher: none
db (current)
wal archive min/max (15): 0000000100000003000000B8/0000000100000003000000BD
full backup: 20240528-132503F
timestamp start/stop: 2024-05-28 13:25:03+07 / 2024-05-28 13:25:39+07
wal start/stop: 0000000100000003000000BB / 0000000100000003000000BB
database size: 98.9MB, database backup size: 98.9MB
repo1: backup set size: 16.8MB, backup size: 16.8MB
incr backup: 20240528-132503F_20240528-133032I
timestamp start/stop: 2024-05-28 13:30:32+07 / 2024-05-28 13:30:40+07
wal start/stop: 0000000100000003000000BD / 0000000100000003000000BD
database size: 99.0MB, database backup size: 16.1MB
repo1: backup set size: 16.8MB, backup size: 4.2MB
backup reference list: 20240528-132503F
Restoring from an incremental backup
Restore from the database from the latest backup.
Look at what clusters there are and what their names are:
pg_lsclustersStop the cluster:
sudo pg_ctlcluster 15 main stopJust in case, check that the cluster is stopped:
pg_lsclusters![]()
Start recovery. First, look at what copies you have:
sudo -u postgres pgbackrest infoConclusion:
stanza: demo status: ok cipher: none db (current) wal archive min/max (15): 0000000100000003000000B8/0000000100000003000000C2 full backup: 20240528-132503F timestamp start/stop: 2024-05-28 13:25:03+07 / 2024-05-28 13:25:39+07 wal start/stop: 0000000100000003000000BB / 0000000100000003000000BB database size: 98.9MB, database backup size: 98.9MB repo1: backup set size: 16.8MB, backup size: 16.8MB incr backup: 20240528-132503F_20240528-133032I timestamp start/stop: 2024-05-28 13:30:32+07 / 2024-05-28 13:30:40+07 wal start/stop: 0000000100000003000000BD / 0000000100000003000000BD database size: 99.0MB, database backup size: 16.1MB repo1: backup set size: 16.8MB, backup size: 4.2MB backup reference list: 20240528-132503F incr backup: 20240528-132503F_20240528-143603I timestamp start/stop: 2024-05-28 14:36:03+07 / 2024-05-28 14:36:11+07 wal start/stop: 0000000100000003000000BF / 0000000100000003000000BF database size: 99.0MB, database backup size: 16.5MB repo1: backup set size: 16.8MB, backup size: 4.2MB backup reference list: 20240528-132503F, 20240528-132503F_20240528-133032I incr backup: 20240528-132503F_20240528-152508I timestamp start/stop: 2024-05-28 15:25:08+07 / 2024-05-28 15:26:51+07 wal start/stop: 0000000100000003000000C2 / 0000000100000003000000C2 database size: 114.8MB, database backup size: 77.8MB repo1: backup set size: 21.3MB, backup size: 17.8MB backup reference list: 20240528-132503F, 20240528-132503F_20240528-133032I, 20240528-132503F_20240528-143603IRestore the database from the latest full backup:
sudo -u postgres pgbackrest --stanza=demo --log-level-console=info --delta restoreRestore to an incremental backup. Please note that paths may vary.
ibatyr stop sudo systemctl stop postgresql rm -rf /var/lib/postgresql/11/main/* sudo -u postgres pgbackrest --stanza=ibatyr --log-level-console=info restore --set=20240929-132900F_20240929-134211I --type=time "--target=2024-09-29 13:42:11+03"
The backup was restored, pg_wal was “rewinded” to the date of the required incremental backup. The database is available in read-only mode.
Checking the backup
Go to PostgreSQL and run:
sudo systemctl start postgresql sudo -u postgres psql select pg_wal_replay_resume();
Start the cluster:
sudo pg_ctlcluster 15 main start
Make sure the cluster is running:
pg_lsclusters
Launch iBatyr Monitor:
sudo ibatyr start
Look at the recovery information in the logs:
sudo -u postgres cat /var/lib/postgresql/15/demo/postgresql.auto.conf cat: /var/lib/postgresql/15/demo/postgresql.auto.conf: No such file or directory sudo -u postgres cat /var/lib/postgresql/15/main/postgresql.auto.conf # Do not edit this file manually! # It will be overwritten by the ALTER SYSTEM command. # Recovery settings generated by pgBackRest restore on 2024-05-28 15:50:57 restore_command = 'pgbackrest --stanza=demo archive-get %f "%p"' recovery_target_time = '2024-05-28 15:26:51+07' recovery_target_action = 'promote'
Script for automation
Alternatively you can use a script.
Save the script as a test file on your server. Name the file backup-and-restore.sh.
#! /bin/bash RED="\e[31m" GREEN="\e[32m" NORM="\e[0m" # Определение системного языка USELANG=0 if [ $(echo ${LANG/_/-} | grep -Ei "\\b(ru|RU)\\b") ]; then USELANG=1; fi # Определение функции echo с поддержкой вывода на нескольких языках function echo2(){ if [ $USELANG == 1 ]; then echo -e $1; #Show Russian else echo -e $2; #Show English fi } #you root? UID_ROOT=0 if [ "$UID" -ne "$UID_ROOT" ]; then echo2 "${RED}Эта операция может быть выполнена только пользователем root${NORM}" "${RED}This operation can only be performed by the root user${NORM}" exit 1 fi if [ -e /etc/pgbackrest.conf ]; then DB=$(grep pg1-path /etc/pgbackrest.conf | sed -e 's/pg1-path=\/var\/lib\///g') elif [ -e /etc/pgbackrest/pgbackrest.conf ]; then DB=$(grep pg1-path /etc/pgbackrest/pgbackrest.conf | sed -e 's/pg1-path=\/var\/lib\///g') else echo2 "${RED}pgbackrest не установлен, либо не настроен.${NORM}" "${RED}pgbackrest not installed or misconfigured.${NORM}" exit 1 fi STANZA=$(grep stanza /etc/$DB/postgresql.conf | sed -e 's/^.*=//g' -e 's/ .*//g') BACKUPS=$(sudo -u postgres pgbackrest info | grep 'backup:' | sed -e 's/^.*backup: //g') COUNT=$(sudo -u postgres pgbackrest info | grep -c 'backup:') echo2 "${GREEN}Количество точек восстановления: $COUNT${NORM}" "${GREEN}Number of recovery points: $COUNT${NORM}" function error() { echo2 "Не могу распознать ответ '$var1'" "Can't recognize the answer '$var1'" echo2 "Введите Y или N" "Enter Y or N" } function todo_dialog(){ echo2 "${RED}Создать новую точку восстановления? y/n${NORM}" "${RED}Create a new recovery point? y/n${NORM}" read var1 case $var1 in Y) backup_dialog ;; y) backup_dialog ;; N) restore_dialog ;; n) restore_dialog ;; *) error todo_dialog ;; esac } function backup_dialog(){ if [ $COUNT >0 ]; then echo2 "${RED}Создать новый инкрементальный бэкап?. y/n${NORM}" "${RED}Create an incremental backup?. y/n${NORM}" echo2 "${RED}(Нет = создать новый полный бэкап.)${NORM}" "${RED}(No = create a new full backup.)${NORM}" read var1 case $var1 in Y) TYPE=incr backup ;; y) TYPE=incr backup ;; N) TYPE=full backup ;; n) TYPE=full backup ;; *) error backup_dialog ;; esac else echo2 "${GREEN}Не найдено ни одной точки восстановления. Будет создан полный бэкап.${NORM}" "${GREEN}No recovery points found. A full backup will be created.${NORM}" TYPE=full backup fi } function backup(){ echo2 "${RED}Создание точки восстановления: $TYPE backup${NORM}" "${RED}Creating recovery point: $TYPE backup${NORM}" ibatyr stop echo "select max(id) from agent_event;select count(*) from agent_event;" | ibatyr sql sudo -u postgres pgbackrest --stanza=$STANZA --type=$TYPE backup ibatyr start } function restore_dialog(){ if [ $COUNT >0 ]; then echo2 "${RED}Восстановить БД из бэкапа? y/n${NORM}" "${RED}Restore DB from a backup? y/n${NORM}" read var1 case $var1 in Y) restore_prepare ;; y) restore_prepare ;; N) exit 0 ;; n) exit 0 ;; *) error restore_dialog ;; esac else echo2 "${RED}Не найдено ни одной точки восстановления.${NORM}" "${RED}No recovery points found.${NORM}" todo_dialog fi } function select_restorepoint(){ sudo -u postgres pgbackrest info echo2 "${RED}Нажмите кнопку от [ 1 ] до [ $COUNT ] для выбора точки восстановления.${NORM}" "${RED}Press button from [ 1 ] to [ $COUNT ] to select a recovery point.${NORM}" echo2 "${RED}Или любую другую кнопку для выхода из режима восстановления...${NORM}" "${RED}Or any other button to exit recovery process...${NORM}" read var1 if ((var1 >= 1 && var1 <= COUNT)); then SELECTPOINT=$(sudo -u postgres pgbackrest info | grep -m$var1 'backup:' | tail -n1 | sed -e 's/^.*backup: //g') SELECTTARGET=$(sudo -u postgres pgbackrest info | grep -m$var1 'timestamp start/stop:' | tail -n1 | sed -e 's/^.*stop: //g' -e 's/ \/.*//g') else exit 0 fi } function restore_prepare(){ if [ -e /etc/pgbackrest.conf ]; then DBNAME=$(grep pg1-path /etc/pgbackrest.conf | sed -e 's/.*\///g') DBVER=$(grep pg1-path /etc/pgbackrest.conf | sed -e 's/.*\/1/1/g' -e 's/\/.*//g') else DBNAME=$(grep pg1-path /etc/pgbackrest/pgbackrest.conf | sed -e 's/.*\///g') DBVER=$(grep pg1-path /etc/pgbackrest/pgbackrest.conf | sed -e 's/.*\/1/1/g' -e 's/\/.*//g') fi select_restorepoint echo2 "${RED}Выбранная точка [ $SELECTPOINT ] [ $SELECTTARGET ]. Начать восстановление? y/n${NORM}" "${RED}Selected point [ $SELECTPOINT ] [ $SELECTTARGET ]. Proceed recovery? y/n${NORM}" read var1 case $var1 in Y) restore ;; y) restore ;; N) select_restorepoint ;; n) select_restorepoint ;; *) error select_restorepoint ;; esac } function restore(){ ibatyr stop pg_ctlcluster $DBVER $DBNAME stop if [ $(pg_lsclusters | grep -c down) != 0 ]; then systemctl stop postgresql sudo -u postgres rm -rf /var/lib/$DB/* sudo -u postgres pgbackrest --stanza=$STANZA --log-level-console=info restore --set=$SELECTPOINT --type=time "--target=$SELECTTARGET" systemctl start postgresql echo "select pg_wal_replay_resume();" | sudo -u postgres psql pg_ctlcluster $DBVER $DBNAME start if [ $(pg_lsclusters | grep -c online) != 0 ]; then echo "select max(id) from agent_event;select count(*) from agent_event;" | ibatyr sql ibatyr start echo2 "${GREEN}БД успешно восстановлена.${NORM}" "${GREEN}DB recovered successfully.${NORM}" else echo2 "${RED}Возникла непредвиденная ошибка во время восстановления БД.${NORM}" "${RED}An unexpected error occured during DB recovery.${NORM}" pg_lsclusters exit 1 fi else pg_lsclusters echo2 "${RED}ОШИБКА. Кластер не остановлен. Восстановление в данный момент невозможно.${NORM}" "${RED}ERROR. Cluster is not stopped. Recovery is not possible at the moment.${NORM}" echo2 "Попробуйте перезапустить скрипт позже." "Try restarting the script later." exit 1 fi } todo_dialog exit 0
Make the script executable:
chmod +x backup-and-restore.sh
Run the script:
sudo ./backup-and-restore.sh
Follow the instructions on the screen.