Para utilizar a ferramenta Barman, você precisa ter um ambiente em PostgreSQL. Para ambientes de produção, utilize o PostgreSQL em Linux. |
Idealmente, não precisaríamos realizar backups/cópias de segurança. Porém, é notório o risco que se corre caso não haja um backup, especialmente de ambientes de produção; podem ocorrer falhas humanas, falhas de hardware, falhas de sistema, desastres naturais, corrompimento de dados, dentre outros fatores que podem comprometer o ambiente físico ou lógico onde se armazenam dados essenciais para o funcionamento adequado de um ambiente.
No contexto de um banco de dados, é importante que o backup seja realizado com frequência; com isto em mente, a ferramenta Barman (Backup and Recovery Manager) foi desenvolvida para apoiar na recuperação de desastres de servidores PostgreSQL, sendo escrita em Python e de código aberto. Para mais detalhes, consulte a documentação da ferramenta.
O uso do Barman tem como propósito principal diminuir o MTTR (Mean Time To Repair) - Tempo médio para reparo entre falhas - e manter o banco de dados com backup íntegro, mesmo em caso de falha total no servidor principal do banco de dados.
Nesta página, foram utilizadas duas VMs, sendo uma nomeada como vm-postgresql01 e a outra como vm-barman.
Caso o usuário postgres não possua senha, configure uma para o mesmo.
VMPOSTGRESQL
Altere o arquivo /etc/hosts e inclua o servidor vmbarman:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 10.0.4.149 vm-barman.xxxx.xxxx.oraclevcn.com vm-barman 10.0.4.178 vm-postgresql01.xxxx.xxxx.oraclevcn.com vm-postgresql01 |
Logo após na vmpostgresql01 crie o usuário barman e streaming_barman:
createuser -s -P barman
createuser -s -P streaming_barman
No arquivo de configuração do PostgreSQL, altere os seguintes parâmetros:
vim /var/lib/pgsql/12/data/postgresql.conf
Original | Após alteração |
---|---|
# listen_addresses = 'localhost' | listen_addresses = '*' |
# max_wal_senders = 10 | max_wal_senders = 2 |
# max_replication_slots = 10 | max_replication_slots = 2 |
No arquivo de pg_hba.conf, altere o seguinte:
vim /var/lib/pgsql/12/data/pg_hba.conf
Original | Após alteração |
---|---|
# IPv4 local connections: | # IPv4 local connections: |
host all all 127.0.0.1/32 ident | host all all 0.0.0.0/0 md5 |
host replication streaming_barman 10.0.4.149/32 trust |
Verifique com a área de segurança de rede, qual o melhor formato de configuração para melhor segurança do ambiente, de acordo com a política de cada empresa. |
Reinicie o PostgreSQL:
systemctl restart postgresql-12.service
VMBARMAN
Altere o arquivo /etc/hosts e inclua o servidor vmpostgresql:
vim /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 |
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 |
10.0.0.166 vmbarman.subnetxxx.vcnxxx.oraclevcn.com vmbarman |
10.0.0.194 vmpostgresql.subnetxxx.vcnxxx.oraclevcn.com vmpostgresql |
Após instalar o PostgreSQL e configurar, instale o barman:
sudo dnf install -y barman
Crie o arquivo de senhas (/var/lib/barman), e de a permissão necessária:
vim .pgpass
vm-postgresql01:5432:postgres:barman:Barman@123
vm-postgresql01:5432:postgres:streaming_barman:Barman@123
## Permissão no arquivo
chmod 0600 .pgpass
Configurar barman.conf:
vim /etc/barman.conf
compression = gzip
Realizar a cópia do arquivo: streaming-server.conf-template, para acesso ao servidor do postgresql:
cp streaming-server.conf-template vm-postgresql.conf
Alterar o arquivo:
[vm-postgresql]
conninfo = host=vm-postgresql user=barman dbname=postgres
streaming_conninfo = host=vm-postgresql user=streaming_barman
streaming_backup_name = barman_streaming_backup
create_slot = auto
streaming_archiver_name = barman_receive_wal
streaming_archiver_batch_size = 50
path_prefix = "/usr/pgsql-12/bin"
Para realizar um backup no Servidor Barman, utilize o seguinte comando:
[barman@vmbarman ~]$ barman backup vmpostgresql
WARNING: No backup strategy set for server 'vmpostgresql' (using default 'exclusive_backup').
WARNING: The default backup strategy will change to 'concurrent_backup' in the future. Explicitly set 'backup_options' to silence this warning.
Starting backup using rsync-exclusive method for server vmpostgresql in /var/lib/barman/vmpostgresql/base/20220304T120404
Backup start at LSN: 0/C000028 (00000001000000000000000C, 00000028)
Starting backup copy via rsync/SSH for 20220304T120404
Copy done (time: 1 second)
Asking PostgreSQL server to finalize the backup.
Backup size: 24.5 MiB
Backup end at LSN: 0/C000100 (00000001000000000000000C, 00000100)
Backup completed (start time: 2022-03-04 12:04:05.098223, elapsed time: 2 seconds)
Processing xlog segments from file archival for vmpostgresql
00000001000000000000000B
00000001000000000000000C
00000001000000000000000C.00000028.backup
Liste o backups realizados com o seguinte comando:
[barman@vmbarman barman.d]$ barman list-backup vmpostgresql
vmpostgresql 20220304T120404 - Fri Mar 4 12:04:06 2022 - Size: 24.5 MiB - WAL Size: 96.5 KiB
vmpostgresql 20220303T210358 - Thu Mar 3 21:04:00 2022 - Size: 24.5 MiB - WAL Size: 32.2 KiB
vmpostgresql 20220303T210310 - Thu Mar 3 21:03:11 2022 - Size: 24.5 MiB - WAL Size: 32.2 KiB
vmpostgresql 20220303T210206 - Thu Mar 3 21:02:07 2022 - Size: 24.5 MiB - WAL Size: 32.2 KiB
Posso listar os detalhes de um backup específico:
[barman@vm-barman ~]$ barman show-backup vm-postgresql01 20220607T193746
Backup 20220607T193746:
Server Name : vm-postgresql01
System Id : 7106500250670266068
Status : DONE PostgreSQL Version : 120011
PGDATA directory : /var/lib/pgsql/12/data
Base backup information:
Disk usage : 23.6 MiB (23.6 MiB with WALs)
Incremental size : 23.6 MiB (-0.00%)
Timeline : 1
Begin WAL : 000000010000000000000009
End WAL : 000000010000000000000009
WAL number : 1
WAL compression ratio: 99.90%
Begin time : 2022-06-07 19:37:46.705044+00:00
End time : 2022-06-07 19:37:47.295758+00:00
Copy time : less than one second
Estimated throughput : 40.6 MiB/s
Begin Offset : 40
End Offset : 0
Begin LSN : 0/9000028
End LSN : 0/A000000
WAL information:
No of files : 0
Disk usage : 0 B
Last available : 000000010000000000000009
Catalog information:
Retention Policy : not enforced
Previous Backup : 20220607T193735
Next Backup : - (this is the latest base backup)