Table of Contents
BARMAN (Backup and Recovery Manager) is an open-source administration tool for the backup and disaster recovery of PostgreSQL servers. It allows performing remote backups of multiple servers. Following are the Barman features-
- Open Source Tool.
- Remote backup and restore of multiple Servers.
- Backup catalogs.
- Incremental backup is possible.
- You can define retention policies.
- Archiving and compression of Wal files and backup files.
- Support both rsync or postgresql protocols.
The PostgreSQL backup methods in a nutshell
Before moving on to the Barman setup, let’s review the types of backups available for PostgreSQL, and how they can be used. Our article is about effective backups and provides a broader overview of backup strategies.
The backup method offered by PostgreSQL is divided into two types:
- Logical backups
- Physical backups
Logical backups are similar to snapshots of a database. Using PostgreSQL’s pg_dump or pg_dumpall utility, these can be created. A logical backup consists of:
- Databases can be backed up individually or all at once
- It is possible to backup just the schemas, just the data, individual tables, or the whole database (schemas and data).
- Using a proprietary binary format or a plain SQL script, create a backup file
- With PostgreSQL, the utility pg_restore is available for restoring databases
- Do not offer point-in-time recovery (PITR)
Whereas, backups taken physically deal with binary format only and make file level backups.
Physical backups:
- Physical backups offer point-in-time recovery
- Contents of the PostgreSQL data directory and the WAL (Write Ahead Log) files are backed up with Physical backups.
- It takes larger amounts of disk space
- pg_start_backup and pg_stop_backup commands are used and thus it makes physical backups a little more complex as compared to Logical backups
- Unlike Logical backups, individual databases, schemas only, etc are not backed up in physical backup, it’s all or nothing.
How Barman Backups Work
Barman does a standardized way of backup, unlike what the PostgreSQL DBA’s would traditionally do such as writing backup scripts and scheduling cron jobs to implement physical backups.
Barman was written in Python and offers a simple, intuitive method of physical backup and restoration for your PostgreSQL instance.
Barman or Backup and Recovery Manager is a free, open-source PostgreSQL backup tool. Some benefits of using Barman are:
- Barman is totally free and is a well-maintained application along with professional support available from the vendor
- Writing and testing complex scripts and cron jobs are not required while using Barman
- Multiple PostgreSQL instances can be backed up into one central location
- Restoring to the same PostgreSQL or different instance is possible
- Compression mechanism is used to minimize the network traffic and disk space
Configuring BARMAN
Requirement:
- One Server for BARMAN with internet access.
- SSH-Key for passwordless authentication for Barman and Postgres users.
Warning: You should not run any commands, queries, or configurations from this tutorial on a production server. This tutorial will involve changing configurations and restarting PostgreSQL instances. Doing so in a live environment without proper planning and authorization would mean an outage for your application.
Barman Architecture
Installation and configuration
Step 1. We will first set up our database environment by installing EDB Postgres on main-db-server
Make sure you have installed EDB Postgres on the main-db-server and that you have allowed access from the barman-backup-server.
Step 2. Verify and Install PostgreSQL Repository
Next we will install a repository that is required in the case of EnterpriseDB Postgres and verify the same.
Install :-
# yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
Verify:-
# yum repolist
Step 3. Install PostgreSQL Binary on the Barman server
# yum install -y postgresql15
Step 4. Install python 3.6 and verify
# yum install -y https://repo.ius.io/ius-release-el7.rpm
# yum update
# yum install -y python36u python36u-libs python36u-devel python36u-pip
Step 5. Install Barman and Verify
Now we’ll install Barman on the backup server, which will both control and store our backups.
# yum install barman -y
# id barman
# barman –version
Step 6. Change the password for user barman and Postgres in both servers
# passwd barman
# passwd postgres
(output omitting)
Step 7. Create user barman as superuser in database cluster
postgres=# create user barman superuser password ‘Admin’;
Step 8. Configure a passwordless connection between the Postgres server and the barman server
Let’s establish SSH keys for a secure passwordless connection between the main-db-server and the barman-backup-server in this section.
This is to ensure PostgreSQL and Barman can “talk” to each other during backups and restores.
On Barman Server
# su – barman
$ ssh-keygen -t rsa
$ ls -ltr .ssh
$ ssh-copy-id -i ~/.ssh/id_rsa.pub postgres@192.168.1.20
$ ssh ‘postgres@192.168.1.20’
On Postgres Server
# su – barman
$ ssh-keygen -t rsa
$ ls -ltr .ssh
$ ssh-copy-id -i ~/.ssh/id_rsa.pub barman@192.168.1.21
$ ssh ‘barman@192.168.1.21’
We will now configure Barman to back up your main PostgreSQL server.
The main configuration file for BARMAN is /etc/barman.conf. The file contains a section for global parameters and separate sections for each server that you want to back up. The default file contains a section for a sample PostgreSQL server called main, which is commented out. You can use it as a guide to set up other servers you want to back up.
Step 9. Take the backup of the existing configuration file.
# ls -ltr /etc/barman.conf
# cp /etc/barman.conf /etc/barman_bkp.conf
# vi /etc/barman.conf
compression = gzip
immediate_checkpoint = true
basebackup_retry_times = 3
reuse_backup = link
Step 10. Configure the server configuration file on the barman server.
In the conninfo parameter of the configuration file, we will specify the host, user, port, dbname, and password. You should also provide the ssh_command that the server uses for connectivity.
# cd /etc/barman.d
# vi test-pgsql.conf
[test-pgsql]
description = “test-pgsql backup config”
ssh_command = ssh postgres@192.168.1.20
conninfo = host=192.168.1.20 user=barman port=5488 dbname=edb password=Admin
backup_options = concurrent_backup
backup_method = rsync
archiver = on
Step 11. Ensure that the archive command is specified in the postgresql.conf file on the Postgres server.
# vi postgresql.conf
archive_command = ‘test ! -f barman@192.168.1.21:/var/lib/barman/test-pgsql/incoming/%f && rsync -a %p barman@192.168.1.21:/var/lib/barman/test-pgsql/incoming/%f’
# systemctl restart edb-as-96.service
Step 12. In the barman server, force the switch of WAL
# barman switch-xlog –force –archive 192.168.1.20
Step 13. Verify server setup in barman server
It is important to make sure the Barman server is configured correctly.
# barman list-server
# barman show-server test-pgsql
# barman check test-pgsql
Step 14. Barman Backup Location
So where does the backup get saved? To find the answer, list the contents of the /var/lib/barman directory
I’ve attached a few backups taken from Barman’s server
How will you take the Full backup using Barman? Here’s a sample backup I’ve attached.
# barman backup test-pgsql
Here’s a sample backup of the Incremental backup I’ve attached.
An incremental backup is one in which successive copies of the data contain only the portion that has changed since the preceding backup copy was made.
Final Thoughts
Database backups are a crucial component of any disaster recovery strategy. They allow organizations to recover from unexpected events, such as hardware failures, data corruption, and cyber attacks, by providing a copy of the data that can be used to restore the database to a previous state. Barman ensures effective recovery solutions for PostgreSQL databases, allowing businesses to maintain continuity in the face of disruptions. By implementing a robust backup and recovery plan, organizations can protect themselves from the negative impacts of disasters and ensure the continued operation of their databases.
How can Ashnik help you?
We at Ashnik can help you by offering consulting services, technical services, migration services, managed services, and training programs in several open-source technologies and share with you our experience of helping several enterprises across SEA and India. We can help you put together database platforms and high-speed data pipelines, re-architect your applications using DevOps automation, Kubernetes, and microservices architecture and facilitate multi-cloud or hybrid adoption.
It is very important for the database server to be resilient, scalable, flexible, and secure while still supporting collaboration. Ashnik also provides you with the ability to monitor and secure your PostgreSQL database, in a meaningful way. We are ready to help you make timely actions through our expertise in open-source services, support, and solutions.
Get in touch today for a free consultation with our team of experts!
Found this article useful? You may like to check out how Ashnik helped simplify data migration and automation for a multinational mobile advertising company.