What is Barman?
Barman is an open source backup / restore management tool in PostgreSQL.
What can you do with Barman?
- You can create backups on the same server.
- You can create a backup to a different backup server.
- You can create multiple PostgreSQL cluster backups.
- You can back up multiple PostgreSQL Clusters running in different major versions of PostgreSQL. So you can collect your backups on a central backup server.
- With Barman, you can create backups in 2 different ways:
-rsync / ssh
In this article, we will create a backup to the / pg_backup directory on the PostgreSQL server.
For Debian and Derivatives:
sudo apt update
sudo apt install barman
For Redhat and Derivatives:
sudo yum update
sudo yum install barman
Configure PostgreSQL For Barman
In PostgreSQL, create the barman user with the superuser right for backup operations and the streaming_barman user with the replication right for streaming operations.
createuser --superuser barman
createuser --replication streaming_barman
Or by connecting with psql, we can create it as follows:
CREATE USER barman SUPERUSER;
CREATE USER streaming_barman REPLICATION;
We edit the pg_hba.conf file so that the user can connect to the database.
local all barman trust
local replication streaming_barman trust
PostgreSQL service requires reload operation for a change in the pg_hba.conf file to take effect. By connecting with psql, we can do it with the following command.
We can check the access of the barman user we created with the command below.
su - barman
psql -c 'SELECT version()' -U barman -d postgres
We can check the access of the streaming_barman user as follows.
su - barman
psql -U streaming_barman -c "IDENTIFY_SYSTEM" replication=1
Barman has 2 types of configuration files:
/etc/barman.conf -> It is a global configuration file. It contains general backup configurations such as log file, backup user, backup directory.
Edit the barman.conf file as follows:
active = true
barman_user = barman
barman_home = Backup_Dizini
log_file = Backup_Log_Dosyası
log_level = INFO
compression = gzip
retention_policy = REDUNDANCY 3
immediate_checkpoint = true
last_backup_maximum_age = 4 DAYS
minimum_redundancy = 1
/etc/barman.d ->Server configuration files are located under /etc/barman.d directory by default. Template configuration files are available under this directory for the backup methods(streaming and rsync).
You can create a configuration file for each PostgreSQL cluster you want to backup with Barman using these template files.
First of all, copy the streaming-server.conf-template file from the template files with the name of the cluster data directory and edit the file.
We create slots with the command of Barman:
su - barman
barman receive-wal --create-slot 12_data
To check the streaming settings of WAL files;
su - barman
barman switch-wal --force --archive 12_data
Check Barman Configuration
You can check the Barman configuration as follows.
barman check 12_data
Barman Create Backup
You can create the first backup with the following command.
barman backup 12_data --wait
You can check the general status of the server as follows.
barman status 12_data
Restore PostgreSQL Cluster With Barman
You can recover the backup to a directory on the server accessible by the barman user as follows.
barman recover <Server_Name> <Backup_ID> <Recovery_Directory>
Barman Point in Time Recovery
For Point in Time Restore, we can specify the time with the target-time parameter.
barman recover 12_data 20200113T230114 /db/pg_data/12/data --target-time "Mon Jan 13 23:20:29 +03 2020"
In this article I mentioned backup with basic level configurations. For more detailed information, you can use the barman’s web page, https://www.pgbarman.org/.