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
-streaming
In this article, we will create a backup to the / pg_backup directory on the PostgreSQL server.
Barman Installation
For Debian and Derivatives:
1 2 | sudo apt update sudo apt install barman |
For Redhat and Derivatives:
1 2 | 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.
1 2 | createuser --superuser barman createuser --replication streaming_barman |
Or by connecting with psql, we can create it as follows:
1 2 | 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.
1 2 3 4 | vim /var/lib/pgsql/12/data/pg_hba.conf >> 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.
1 | SELECT pg_reload_conf(); |
We can check the access of the barman user we created with the command below.
1 2 | su - barman psql -c 'SELECT version()' -U barman -d postgres |
We can check the access of the streaming_barman user as follows.
1 2 | su - barman psql -U streaming_barman -c "IDENTIFY_SYSTEM" replication=1 |
Configure Barman
Barman has 2 types of configuration files:
- barman.conf
- barman.d
/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:
1 2 3 4 5 6 7 8 9 10 11 12 13 | vim /etc/barman.conf >> [barman] 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:
1 2 | su - barman barman receive-wal --create-slot 12_data |
To check the streaming settings of WAL files;
1 2 | su - barman barman switch-wal --force --archive 12_data |
Check Barman Configuration
You can check the Barman configuration as follows.
1 | barman check 12_data |
Barman Create Backup
You can create the first backup with the following command.
1 | barman backup 12_data --wait |
You can check the general status of the server as follows.
1 | barman status 12_data |
Barman Restore
Restore PostgreSQL Cluster With Barman
You can recover the backup to a directory on the server accessible by the barman user as follows.
1 | 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.
1 | 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/.
Hi,
After the below section , there is no content about what you have edited in the streaming-server.conf file. Plz help
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.
Don’t forget to add/enable in
postgres.conf
listen_addresses= ‘…’
if you’re going to do use barmat from a remote machine (as is recommended)!
Took me an hour to figure that out