On this article i will show howto backup and restore with barman

Follow me for the steps

spec

OS : Debian 8.x
Postgresql-version : 9.5.6
Barman-version : 2.2

Install Barman

Create repo file with name pgdg.list

# vi /etc/apt/sources.list.d/pgdg.list
deb http://apt.postgresql.org/pub/repos/apt/ jessie-pgdg main

And then import sign key repository

# wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | \
  apt-key add -
# apt-get update

Installing barman

# apt-get install -y barman

Setup barman for backuping database Postgresql

I use type backup streaming protocol for this section, streaming protocol no need any ssh for the process

For this method need some steps configured on masterDB

At masterDB

Create user barman with superuser privileges to make connection from barman to database server

postgres=# create role barman with password 'password123' ; alter role barman login ; alter role barman superuser ;
CREATE ROLE
ALTER ROLE
ALTER ROLE

next create user stream_barman with replication privileges to stream/replicate xlog from database server

postgres=# create role stream_barman with password 'password123'; alter role stream_barman login; alter role stream_barman replication;
CREATE ROLE
ALTER ROLE
ALTER ROLE

add the users to file pg_hba.conf

$ vi /home/postgres/project/data/pg_hba.conf
host     replication     barman_streaming 192.168.34.186/32	 md5
host     all		 barman           192.168.34.186/32	 md5

Edit file postgresql.conf and add this variable to file

max_wal_senders = 5
max_replication_slots = 3

Restart service postgresql

$ /opt/pgsql-9.5/bin/pg_ctl -D /home/postgres/project/data/ -m fast restart

At Barman server

Barman configuration file

The main configuration in barman is /etc/barman.conf and for file server configuration is on dir /etc/barman.d

This is my configuration file /etc/barman.conf

# vi /etc/barman.conf

; Main configuration file

[barman]

barman_user = barman

; Directory of configuration files. Place your sections in separate files with .conf extension

configuration_files_directory = /etc/barman.d

; Main directory
barman_home = /var/lib/barman

; Log location
log_file = /var/log/barman/barman.log

; Log level (see https://docs.python.org/3/library/logging.html#levels)
log_level = INFO

; Default compression level: possible values are None (default), bzip2, gzip, pigz, pygzip or pybzip2
compression = gzip

; Immediate checkpoint for backup command - default false
immediate_checkpoint = true

; Number of retries of data copy during base backup after an error - default 0
basebackup_retry_times = 3

; Number of seconds of wait after a failed copy, before retrying - default 30
basebackup_retry_sleep = 30

last_backup_maximum_age = 1 DAYS

And my file configuration server

# vi /etc/barman.d/serverdb.conf

[master]
description = "PostgreSQL Master server"
conninfo = host=192.168.34.187 user=barman dbname=postgres password=password123
streaming_conninfo = host=192.168.34.187 user=stream_barman dbname=postgres password=password123
backup_method = postgres
retention_policy_mode = auto
streaming_archiver = on
slot_name = barman

; PATH setting for this server
path_prefix = "/opt/pgsql-9.5/bin/"


#note
make sure postgresql version on barman same with postgresql master & slave

After that run command barman check name_server to check the configuration

$ barman check master
Server master:
	WAL archive: FAILED (please make sure WAL shipping is setup)
	PostgreSQL: OK
	is_superuser: OK
	PostgreSQL streaming: OK
	wal_level: OK
	replication slot: FAILED (replication slot 'barman' doesn't exist. Please execute 'barman receive-wal --create-slot master')
	directories: OK
	retention policy settings: OK
	backup maximum age: FAILED (interval provided: 1 day, latest backup age: No available backups)
	compression settings: OK
	failed backups: OK (there are 0 failed backups)
	minimum redundancy requirements: OK (have 0 backups, expected at least 0)
	pg_basebackup: OK
	pg_basebackup compatible: OK
	pg_basebackup supports tablespaces mapping: OK
	pg_receivexlog: OK
	pg_receivexlog compatible: OK
	receive-wal running: FAILED (See the Barman log file for more details)
	archiver errors: OK

If the result like above, run command barman receive-wal –create-slot name_server to create replication-slot and then run command barman cron to start stream WAL archive and don’t forget to run barman switch-xlog –force –archive to verification WAL archive configuration

$ barman receive-wal --create-slot master

Creating physical replication slot 'barman' on server 'master'
Replication slot 'barman' created

$ barman cron
Starting WAL archiving for server master
Starting streaming archiver for server master

$ barman switch-xlog --force --archive master
The xlog file 000000010000000000000006 has been closed on server 'master'
Waiting for the xlog file 000000010000000000000006 from server 'master' (max: 30 seconds)
Processing xlog segments from streaming for master

Run again barman check to check the configuration

$ barman check master
Server master:
	PostgreSQL: OK
	is_superuser: OK
	PostgreSQL streaming: OK
	wal_level: OK
	replication slot: OK
	directories: OK
	retention policy settings: OK
	backup maximum age: FAILED (interval provided: 1 day, latest backup age: No available backups)
	compression settings: OK
	failed backups: OK (there are 0 failed backups)
	minimum redundancy requirements: OK (have 0 backups, expected at least 0)
	pg_basebackup: OK
	pg_basebackup compatible: OK
	pg_basebackup supports tablespaces mapping: OK
	pg_receivexlog: OK
	pg_receivexlog compatible: OK
	receive-wal running: OK
	archiver errors: OK

OK the configuration is true , so ready to run first backup for postgresql

To start backup run command barman backup name_server

$ barman backup master
Starting backup using postgres method for server master in /var/lib/barman/master/base/20170731T170253
Backup start at xlog location: 0/14001410 (000000010000000000000014, 00001410)
Starting backup copy via pg_basebackup for 20170731T170253
Copy done (time: 2 seconds)
Finalising the backup.
Backup size: 118.2 MiB
Backup end at xlog location: 0/16000000 (000000010000000000000015, 00000000)
Backup completed (start time: 2017-07-31 17:02:53.961223, elapsed time: 2 seconds)
Processing xlog segments from streaming for master
	000000010000000000000014

For scheduling backup can using crontab

$ crontab -e 
00 01 * * * /usr/bin/barman backup master

Done

Source :
Digital_Ocean-Barman
Barman Manual

For the next article i will show simulation restoring the database after crash

CMIWW

Advertisements