Install and Configure BARMAN for PostgreSQL
BARMAN (Backup and Recovery Manager) is an open-source administration tool for backup and disaster recovery of PostgreSQL servers. It allows performing remote backups of multiple servers. Following are barman features.
1) Open Source Tool.
2) Remote backup and restore of multiple Servers.
3) Backup catalogs.
4) Incremental backup is possible.
5) You can define retention policies.
6) Archiving and compression of Wal files and backup files.
7) Support both rsync or postgresql protocols.
Sr No | Hostname | IP | Role |
1 | test-machine01 | 192.168.114.177 | Barman Server (Backup Server) |
2 | test-machine02 | 192.168.114.176 | PostgreSQL Database Server |
Follow below steps to install and configure barman
Step 1. Verify PostgresQL repository: Use OS command yum repolist to confirm postgresql repository exist in Backup Server. Click here to configure postgresql repository if it is not configured.
[root@test-machine01 ~]# yum repolist
Loaded plugins: langpacks, ulninfo
repo id repo name status
!mysql-connectors-community/x86_64 MySQL Connectors Community 203
!mysql-tools-community/x86_64 MySQL Tools Community 129
!mysql80-community/x86_64 MySQL 8.0 Community Server 265
!ol7_UEKR5/x86_64 Latest Unbreakable Enterprise Kernel Release 5 for Oracle Linux 7Server (x86_64) 404
!ol7_UEKR6/x86_64 Latest Unbreakable Enterprise Kernel Release 6 for Oracle Linux 7Server (x86_64) 323
!ol7_latest/x86_64 Oracle Linux 7Server Latest (x86_64) 22,766
!pgdg-common/7Server/x86_64 PostgreSQL common RPMs for RHEL/CentOS 7Server - x86_64 303
!pgdg10/7Server/x86_64 PostgreSQL 10 for RHEL/CentOS 7Server - x86_64 918
!pgdg11/7Server/x86_64 PostgreSQL 11 for RHEL/CentOS 7Server - x86_64 1,006
!pgdg12/7Server/x86_64 PostgreSQL 12 for RHEL/CentOS 7Server - x86_64 572
!pgdg13/7Server/x86_64 PostgreSQL 13 for RHEL/CentOS 7Server - x86_64 298
!pgdg95/7Server/x86_64 PostgreSQL 9.5 for RHEL/CentOS 7Server - x86_64 763
!pgdg96/7Server/x86_64 PostgreSQL 9.6 for RHEL/CentOS 7Server - x86_64 889
repolist: 28,839
[root@test-machine01 ~]#
Step 2. Install barman: Use OS command yum install barman to install barman package in Backup Server.
[root@test-machine01 tmp]# yum install barman
Loaded plugins: langpacks, ulninfo
Resolving Dependencies
--> Running transaction check
---> Package barman.noarch 0:2.12-1.rhel7 will be installed
--> Processing Dependency: python-barman = 2.12 for package: barman-2.12-1.rhel7.noarch
--> Running transaction check
---> Package python-barman.noarch 0:2.12-1.rhel7 will be installed
--> Processing Dependency: python-psycopg2 >= 2.4.2 for package: python-barman-2.12-1.rhel7.noarch
--> Running transaction check
---> Package python2-psycopg2.x86_64 0:2.8.6-1.rhel7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================
Installing:
barman noarch 2.12-1.rhel7 pgdg-common 36 k
Installing for dependencies:
python-barman noarch 2.12-1.rhel7 pgdg-common 356 k
python2-psycopg2 x86_64 2.8.6-1.rhel7 pgdg-common 171 k
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 563 k
Installed size: 2.5 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): barman-2.12-1.rhel7.noarch.rpm | 36 kB 00:00:01
(2/3): python2-psycopg2-2.8.6-1.rhel7.x86_64.rpm | 171 kB 00:00:01
(3/3): python-barman-2.12-1.rhel7.noarch.rpm | 356 kB 00:00:03
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 176 kB/s | 563 kB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : python2-psycopg2-2.8.6-1.rhel7.x86_64 1/3
Installing : python-barman-2.12-1.rhel7.noarch 2/3
Installing : barman-2.12-1.rhel7.noarch 3/3
Verifying : python2-psycopg2-2.8.6-1.rhel7.x86_64 1/3
Verifying : barman-2.12-1.rhel7.noarch 2/3
Verifying : python-barman-2.12-1.rhel7.noarch 3/3
Installed:
barman.noarch 0:2.12-1.rhel7
Dependency Installed:
python-barman.noarch 0:2.12-1.rhel7 python2-psycopg2.x86_64 0:2.8.6-1.rhel7
Complete!
[root@test-machine01 tmp]#
Step 3. In case of Error: In case if you encounter errors Error: Package: python-barman-2.12-1.rhel7.noarch (pgdg-common) Requires: python-argcomplete & Error: Package: python-barman-2.12-1.rhel7.noarch (pgdg-common) Requires: python-argh >= 0.21.2 in yum install barman command. Download rpm python2-argh-0.26.1-5.el7.noarch.rpm & python2-argcomplete-1.7.0-4.el7.noarch.rpm from below links and transfer these rpm to Backup Server and install the rpm and then try to install barman package.
https://download-ib01.fedoraproject.org/pub/epel/7/x86_64/Packages/p/python2-argcomplete-1.7.0-4.el7.noarch.rpm
https://download-ib01.fedoraproject.org/pub/epel/7/aarch64/Packages/p/python2-argh-0.26.1-5.el7.noarch.rpm
[root@test-machine01 u01]# yum install barman
Loaded plugins: langpacks, ulninfo
Repository ol7_UEKR5 is listed more than once in the configuration
Repository ol7_UEKR4 is listed more than once in the configuration
Repository ol7_UEKR3 is listed more than once in the configuration
---> Package python-backports.x86_64 0:1.0-8.el7 will be installed
---> Package python-barman.noarch 0:2.12-1.rhel7 will be installed
--> Processing Dependency: python-argh >= 0.21.2 for package: python-barman-2.12-1.rhel7.noarch
--> Processing Dependency: python-argcomplete for package: python-barman-2.12-1.rhel7.noarch
---> Package python-ipaddress.noarch 0:1.0.16-2.el7 will be installed
--> Finished Dependency Resolution
Error: Package: python-barman-2.12-1.rhel7.noarch (pgdg-common)
Requires: python-argcomplete
Error: Package: python-barman-2.12-1.rhel7.noarch (pgdg-common)
Requires: python-argh >= 0.21.2
You could try using --skip-broken to work around the problem
You could try running: rpm -Va --nofiles --nodigest
[root@test-machine01 u01]#
root@test-machine01 u01]# cd /tmp
[root@test-machine01 tmp]#
[root@test-machine01 tmp]# ls -ltr
total 156
-rw-r--r--. 1 root root 55120 Jun 22 09:57 python2-argh-0.26.1-5.el7.noarch.rpm
-rw-r--r--. 1 root root 50402 Jun 22 2021 python2-argcomplete-1.7.0-4.el7.noarch.rpm
[root@test-machine01 u01]#
[root@test-machine01 tmp]# rpm -ivh python2-argh-0.26.1-5.el7.noarch.rpm
warning: python2-argh-0.26.1-5.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:python2-argh-0.26.1-5.el7 ################################# [100%]
[root@test-machine01 tmp]#
[root@test-machine01 tmp]# rpm -ivh python2-argcomplete-1.7.0-4.el7.noarch.rpm
warning: python2-argcomplete-1.7.0-4.el7.noarch.rpm: Header V3 RSA/SHA256 Signature, key ID 352c64e5: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:python2-argcomplete-1.7.0-4.el7 ################################# [100%]
[root@test-machine01 tmp]#
[root@test-machine01 tmp]# yum install barman
Loaded plugins: langpacks, ulninfo
Resolving Dependencies
--> Running transaction check
---> Package barman.noarch 0:2.12-1.rhel7 will be installed
--> Processing Dependency: python-barman = 2.12 for package: barman-2.12-1.rhel7.noarch
--> Running transaction check
---> Package python-barman.noarch 0:2.12-1.rhel7 will be installed
--> Processing Dependency: python-psycopg2 >= 2.4.2 for package: python-barman-2.12-1.rhel7.noarch
--> Running transaction check
---> Package python2-psycopg2.x86_64 0:2.8.6-1.rhel7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==============================================================================================================================================================================================
Package Arch Version Repository Size
==============================================================================================================================================================================================
Installing:
barman noarch 2.12-1.rhel7 pgdg-common 36 k
Installing for dependencies:
python-barman noarch 2.12-1.rhel7 pgdg-common 356 k
python2-psycopg2 x86_64 2.8.6-1.rhel7 pgdg-common 171 k
Transaction Summary
==============================================================================================================================================================================================
Install 1 Package (+2 Dependent packages)
Total download size: 563 k
Installed size: 2.5 M
Is this ok [y/d/N]: y
Downloading packages:
(1/3): barman-2.12-1.rhel7.noarch.rpm | 36 kB 00:00:01
(2/3): python2-psycopg2-2.8.6-1.rhel7.x86_64.rpm | 171 kB 00:00:01
(3/3): python-barman-2.12-1.rhel7.noarch.rpm | 356 kB 00:00:03
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Total 176 kB/s | 563 kB 00:00:03
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Warning: RPMDB altered outside of yum.
Installing : python2-psycopg2-2.8.6-1.rhel7.x86_64 1/3
Installing : python-barman-2.12-1.rhel7.noarch 2/3
Installing : barman-2.12-1.rhel7.noarch 3/3
Verifying : python2-psycopg2-2.8.6-1.rhel7.x86_64 1/3
Verifying : barman-2.12-1.rhel7.noarch 2/3
Verifying : python-barman-2.12-1.rhel7.noarch 3/3
Installed:
barman.noarch 0:2.12-1.rhel7
Dependency Installed:
python-barman.noarch 0:2.12-1.rhel7 python2-psycopg2.x86_64 0:2.8.6-1.rhel7
Complete!
[root@test-machine01 tmp]#
Step 4. Verify barman package installation: yum install barman will create barman account in Server. Use command barman –version to verify install barman version in Backup Server.
[root@test-machine01 tmp]# id barman
uid=994(barman) gid=990(barman) groups=990(barman)
[root@test-machine01 tmp]#
[root@test-machine01 tmp]# su - barman
-bash-4.2$
-bash-4.2$ whoami
barman
-bash-4.2$
-bash-4.2$ barman --version
2.12
Barman by 2ndQuadrant (www.2ndQuadrant.com)
-bash-4.2$
Step 5. Set Password for OS Account barman: Use OS command passwd barman to reset barman OS account in Backup Server.
[root@test-machine01 tmp]# passwd barman
Changing password for user barman.
New password:
Retype new password:
passwd: all authentication tokens updated successfully.
[root@test-machine01 tmp]#
Step 6. Set Password for OS Account postgres: Use OS command passwd postgres to reset postgres OS account in PostgreSQL Database Server.
[root@test-machine02 ~]# passwd postgres
Changing password for user postgres.
New password:
BAD PASSWORD: The password contains the user name in some form
Retype new password:
passwd: all authentication tokens updated successfully.
[root@test-machine02 ~]#
Step 7. Create barman role: Create role barman in PostgreSQL Database Server. This role will use in barman server configuration file.
[root@test-machine02 ~]# su - postgres
Last login: Tue Jun 22 15:06:21 +03 2021 on pts/0
-bash-4.2$ psql
psql (13.2)
Type "help" for help.
postgres=#
postgres=# create user barman superuser password 'barman';
CREATE ROLE
postgres=#
Step 8. Configure Passwordless Authentication in Barman Server: Use OS command ssh-keygen to generate public and private keys. Once keys are generated use OS command ssh-copy-id to transfer public key to PostgreSQL Database Server.
[root@test-machine01 ~]# su - barman
Last login: Tue Jun 22 10:02:53 +03 2021 on pts/0
-bash-4.2$
-bash-4.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/barman/.ssh/id_rsa):
Created directory '/var/lib/barman/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/barman/.ssh/id_rsa.
Your public key has been saved in /var/lib/barman/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:fg8qzAkNeneq4qKoYRNJCTNP5+S7z4RQWXb889KpETA barman@test-machine01
The key's randomart image is:
+---[RSA 2048]----+
|+ . o o.. |
|.=.= + .E |
| o. = + |
|. .... + |
| o...o S = . |
| o.ooo.. o + |
|.o .o=.+. o+ |
|+.o +* o.o |
|*o....o.. . |
+----[SHA256]-----+
-bash-4.2$
-bash-4.2$ls -ltr .ssh
total 8
-rw-r--r--. 1 barman barman 403 Jun 22 10:12 id_rsa.pub
-rw-------. 1 barman barman 1675 Jun 22 10:12 id_rsa
-bash-4.2$
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@test-machine02
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/barman/.ssh/id_rsa.pub"
The authenticity of host 'test-machine02 (192.168.114.176)' can't be established.
ECDSA key fingerprint is SHA256:NbJdl0Pi8lmjbKHbz7q0GgYAy++evY3rNH7gCupnx+c.
ECDSA key fingerprint is MD5:39:33:1f:a7:bb:09:ac:81:cc:ff:cc:18:46:ca:76:c1.
Are you sure you want to continue connecting (yes/no)? yes
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
postgres@test-machine02's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'postgres@test-machine02'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$
-bash-4.2$ ssh 'postgres@test-machine02'
Last login: Tue Jun 22 10:30:29 2021
-bash-4.2$
-bash-4.2$ exit
logout
Connection to test-machine02 closed.
-bash-4.2$
Step 9. Configure Passwordless Authentication in PostgreSQL Database Server: Use OS command ssh-keygen to generate public and private keys. Once keys are generated use OS command ssh-copy-id to transfer public key to Barman Server.
[root@test-machine02 ~]# su - postgres
Last login: Wed Jun 16 17:44:29 +03 2021 on pts/2
-bash-4.2$
-bash-4.2$
-bash-4.2$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/var/lib/pgsql/.ssh/id_rsa):
Created directory '/var/lib/pgsql/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /var/lib/pgsql/.ssh/id_rsa.
Your public key has been saved in /var/lib/pgsql/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:jiouqVEgyZz+dmsHM+H0yzn4GgdhYnqBkbJ3Es/ralw postgres@test-machine02
The key's randomart image is:
+---[RSA 2048]----+
| .. |
|+o= |
|=* B o |
|+.= Bo. |
| +.+ooo S |
| .o E=.+ |
|.o = oB.+ |
|o.= o++* |
|o+oooo+.. |
+----[SHA256]-----+
-bash-4.2$
-bash-4.2$ ls -ltr .ssh
total 8
-rw-r--r--. 1 postgres postgres 405 Jun 22 10:12 id_rsa.pub
-rw-------. 1 postgres postgres 1679 Jun 22 10:12 id_rsa
-bash-4.2$
-bash-4.2$ ssh-copy-id -i ~/.ssh/id_rsa.pub barman@test-machine01
/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/var/lib/pgsql/.ssh/id_rsa.pub"
/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
barman@test-machine01's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh 'barman@test-machine01'"
and check to make sure that only the key(s) you wanted were added.
-bash-4.2$
-bash-4.2$ ssh 'barman@test-machine01'
Last login: Tue Jun 22 10:32:10 2021
-bash-4.2$
-bash-4.2$ exit
logout
Connection to test-machine01 closed.
-bash-4.2$
Step 10. Configure Global Configuration File in Barman Server: The main configuration file (set to /etc/barman.conf by default) contains general options such as main directory, system user, log file, and so on. Edit /etc/barman.conf to set global parameters. Set the below parameters and save the file.
[root@test-machine01 ~]#
[root@test-machine01 ~]# ls -ltr /etc/barman.conf
-rw-r--r--. 1 root root 3195 Nov 4 2020 /etc/barman.conf
[root@test-machine01 ~]# cp /etc/barman.conf /etc/barman_bkp.conf
[root@test-machine01 ~]# vi /etc/barman.conf
compression = gzip
immediate_checkpoint = true
basebackup_retry_times = 3
reuse_backup = link
:wq!
[root@test-machine01 ~]#
Step 11. Configure Server configuration files in Barman Server: Server configuration files, one for each server to be backed up by Barman, is located in the /etc/barman.d directory and must have a .conf suffix. Please note user=barman is the same role we created in Step 6. Set the below parameters and save the file.
root@test-machine01 ~]#
[root@test-machine01 ~]# cd /etc/barman.d
[root@test-machine01 barman.d]#
[root@test-machine01 barman.d]# ls -ltr
total 12
-rw-r--r--. 1 root root 1492 Nov 4 2020 streaming-server.conf-template
-rw-r--r--. 1 root root 1565 Nov 4 2020 ssh-server.conf-template
-rw-r--r--. 1 root root 947 Nov 4 2020 passive-server.conf-template
[root@test-machine01 barman.d]# vi test-machine02.conf
[test-machine02]
description = "test-machine02 backup config"
ssh_command = ssh postgres@test-machine02
conninfo = host=test-machine02 user=barman port=5432 dbname=postgres password=barman
backup_options = concurrent_backup
backup_method = rsync
archiver = on
:wq!
[root@test-machine01 barman.d]# ls -ltr
total 16
-rw-r--r--. 1 root root 1492 Nov 4 2020 streaming-server.conf-template
-rw-r--r--. 1 root root 1565 Nov 4 2020 ssh-server.conf-template
-rw-r--r--. 1 root root 947 Nov 4 2020 passive-server.conf-template
-rw-r--r--. 1 root root 226 Jun 22 11:50 test-machine02.conf
[root@test-machine01 barman.d]#
Step 12. Verify Server setup in Barman Server: Use command barman list-server to Server list configured in barman. Command barman show-server test-machine02 to get server configuration setting. And command barman check test-machine02 to validate the setup. Please note we are getting an error “WAL archive: FAILED (please make sure WAL shipping is setup)“.
[root@test-machine01 barman.d]# su - barman
Last login: Tue Jun 22 11:59:11 +03 2021 on pts/0
-bash-4.2$
-bash-4.2$ barman list-server
test-machine02 - test-machine02 backup config
-bash-4.2$
-bash-4.2$ barman show-server test-machine02
Server test-machine02:
active: True
archive_command: None
archive_mode: None
archiver: True
archiver_batch_size: 0
-bash-4.2$ barman check test-machine02
Server test-machine02:
WAL archive: FAILED (please make sure WAL shipping is setup)
PostgreSQL: OK
superuser or standard user with backup privileges: OK
wal_level: OK
directories: OK
retention policy settings: OK
backup maximum age: OK (no last_backup_maximum_age provided)
compression settings: OK
failed backups: OK (there are 0 failed backups)
minimum redundancy requirements: OK (have 0 backups, expected at least 0)
ssh: OK (PostgreSQL server)
systemid coherence: OK (no system Id stored on disk)
archive_mode: OK
archive_command: OK
continuous archiving: OK
archiver errors: OK
-bash-4.2$
Step 13. Configure WAL Shipping in PostgreSql Database Server: Edit postgresql.conf and modify archive_command to copy WAL Files in barman server. As archive_command is a static parameter postgresql service needs to be restart.
[root@test-machine02 ~]# cd /var/lib/pgsql/13/data/
[root@test-machine02 data]# vi postgresql.conf
archive_command = 'test ! -f barman@test-machine01:/var/lib/barman/test-machine02/incoming/%f && rsync -a %p barman@test-machine01:/var/lib/barman/test-machine02/incoming/%f'
:wq!
[root@test-machine02 system]# systemctl restart postgresql-13
[root@test-machine02 system]#
[root@test-machine02 ~]#
[root@test-machine02 ~]# su - postgres
Last login: Tue Jun 22 10:34:26 +03 2021 from test-machine01 on pts/3
-bash-4.2$ psql
psql (13.2)
Type "help" for help.
postgres=# show archive_command;
archive_command
------------------------------------------------------------------------------------------------------------------------------------------------------------
test ! -f barman@test-machine01:/var/lib/barman/test-machine02/incoming/%f && rsync -a %p barman@test-machine01:/var/lib/barman/test-machine02/incoming/%f
(1 row)
postgres=#
Step 14. Force Switch WAL in Barman Server: Use barman command switch-xlog –force to perform force switch of WAL File. And run barman check test-machine02 again to verify settings. Now it should be all OK.
[root@test-machine01 barman.d]# su - barman
Last login: Tue Jun 22 11:59:11 +03 2021 on pts/0
-bash-4.2$
-bash-4.2$ barman switch-xlog --force --archive test-machine02
The WAL file 00000001000000000000002E has been closed on server 'test-machine02'
Waiting for the WAL file 00000001000000000000002E from server 'test-machine02' (max: 30 seconds)
Processing xlog segments from file archival for test-machine02
00000001000000000000002E
-bash-4.2$
-bash-4.2$ barman check test-machine02
Server test-machine02:
PostgreSQL: OK
superuser or standard user with backup privileges: OK
wal_level: OK
directories: OK
retention policy settings: OK
backup maximum age: OK (no last_backup_maximum_age provided)
compression settings: OK
failed backups: OK (there are 0 failed backups)
minimum redundancy requirements: OK (have 0 backups, expected at least 0)
ssh: OK (PostgreSQL server)
systemid coherence: OK (no system Id stored on disk)
archive_mode: OK
archive_command: OK
continuous archiving: OK
archiver errors: OK
-bash-4.2$
Reference: http://docs.pgbarman.org/release/2.12/#installation
This document is just for learning purpose and always validate in the LAB environment first before applying in the LIVE environment.
Hope so you like this article!
Please share your valuable feedback/comments/subscribe and follow us below and don’t forget to click on the bell icon to get the most recent update. Click here to understand more about our pursuit.
Related Articles
- Backups from the Standby Server using pgBackRest in PostgreSQL
- Traditional Barman Setup With WAL Streaming in PostgreSQL
- Traditional Barman Setup With WAL archiving via archive_command in PostgreSQL
- Streaming Backup With Fallback WAL Archiving in PostgreSQL
- Configure Streaming Backup and WAL Streaming using BARMAN in PostgreSQL