In today’s article, I will tell you about Pgbackrest Installation on PostgreSQL Community 13.
If you have not installed PostgreSQL Community before, you can install it by following the link below.
1 | https://dbtut.com/index.php/2022/03/13/postgresql-community-13-installation-on-oracle-linux-7/ |
Briefly, pgBackrest is a backup tool for PostgreSQL. You can get full backup, incremental backup, differantial backup with this tool. You can restore from these backups or simply restore the missing files.
After briefly explaining what pgBackrest is, we can start the installation. First, we install pgBacgrest.
1 | [root@testdb ~]#yum -y install pgbackrest |
Repository Creation:
The repository is where pgBackRest stores backups and archives WAL segments.
1 2 3 | [root@testdb ~]#mkdir -p /pg_backup/pgbackrest [root@testdb ~]#chmod 750 /pg_backup/pgbackrest [root@testdb ~]#chown postgres:postgres /pg_backup/pgbackrest |
Archiving Configuration:
To put the database in archive mode, we write the following parameters into the postgresql.conf file.
1 2 | archive_mode = on archive_command = ‘pgbackrest --stanza=demo archive-push %p’ |
We restart the postgresql service so that the change we made can be active.
1 | [root@testdb ~]#systemctl restart postgresql-13 |
Backup
If we are going to backup on the same server, we configure pgBackrest as follows.
1 2 3 4 5 6 7 8 9 10 | $ vi /etc/pgbackrest.conf [global] repo-path=/pg_backup/pgbackrest/ backup-user=postgres retention-full=2 retention-diff=2 process-max=3 log-path=/pg_backup/log/pgbackrest [demo] pg1-path=/var/lib/pgsql/13/data/ |
If a backup will be made to a different server, we will configure both the master server and the server that we will backup.
The pgBacrest configuration to be done on the master server:
1 2 3 4 5 6 | $vi /etc/pgbackrest.conf [global] repo1-host=<backup-sunucu-ip> repo1-host-user=postgres [demo] pg1-path=/var/lib/pgsql/13/data/ |
The pgBackrest configuration to be made on the server to be backed up:
1 2 3 4 5 6 7 8 9 10 | $ vi /etc/pgbackrest.conf [global] repo1-path=/pg_backup/pgbackrest repo1-retention-full=2 repo1-retention-diff=2 process-max=3 [demo] pg1-host=<master-sunucu-ip> pg1-path=/var/lib/pgsql/13/data/ pg1-user=postgres |
To take a backup with pgBackrest, we must first create the backup catalog. After creating, we check the correctness of the operation we have done.
1 2 | $ pgbackrest --stanza=demo --log-level-console=info stanza-create $ pgbackrest --stanza=demo --log-level-console=info check |
After checking, we can get a full backup with the following command.
1 | $ pgbackrest --stanza=demo --log-level-console=info backup |
Differential backup:
1 | $ pgbackrest --stanza=demo --type=diff --log-level-console=info backup |
Incremental backup:
1 | $ pgbackrest --stanza=demo --type=incr --log-level-console=info backup |
We can use the following command to get information about the previously taken backups.
1 | $ pgbackrest info |
Backup Restore
For full backup restore, we must first stop the postgresql service. After performing the restore process, we can start the service again.
1 2 3 | [root@testdb ~]systemctl stop postgresql-13 $ pgbackrest --stanza=demo --delta restore [root@testdb ~]systemctl start postgresql-13 |
Incremental backup restore:
1 2 3 4 | [root@testdb ~]systemctl stop postgresql-13 $ pgbackrest --stanza=demo --delta restore \ --recovery-option=recovery_target=immediate [root@testdb ~]systemctl start postgresql-13 |
I hope it will be a useful document for you.