This tutorial requires that you have already deployed two Instances running a PostgreSQL database. They will be called pgsql
and pgsql-backup
in this tutorial.
Backing up PostgreSQL databases with Barman
- backup
- barman
- postgresql
Barman is an open-source administration tool designed for PostgreSQL server disaster recovery. Developed in Python, Barman offers the capability to efficiently conduct remote backups across multiple servers in diverse business environments.
Setting itself apart with its unique methodology, Barman uses PostgreSQL logs for backup creation, offering a sophisticated alternative to traditional SQL dump processes.
Before you start
To complete the actions presented below, you must have:
- A Scaleway account logged into the console
- Owner status or IAM permissions allowing you to perform actions in the intended Organization
- An SSH key
- Two Instances running a PostgreSQL database on Ubuntu Bionic or a later version
sudo
privileges or access to the root user
Installing Barman
As you will back up your databases with Barman, it is recommended to host the application on a different instance than your databases. Ideally in a geographically distant data center. In this tutorial, we install Barman on a second server to migrate the database from one to the other server for backup and redundancy purposes.
Barman is available in the official APT repositories of Ubuntu. Connect to the second server, called pgsql-backup
and install the tool:
- Update the APT package cache and upgrade the already installed packages to their latest version:
sudo apt-get update && sudo apt-get upgrade -y
- Install Barman:
sudo apt-get install barman
Configuring Barman
For Barman to copy data between the two servers, some pre-work is required:
Creating a dedicated PostgreSQL user on pgsql
As Barman has been installed on the second server (pgsql-backup
), it is required to create a database user for the tool on the first server (pgsql
).
-
Connect on the server
pgsql
and log into thepostgres
account:sudo -i -u postgres -
Create the user and enter twice the desired password. When asked if the account should have superuser privileges, type y and press Enter:
createuser --interactive -P barman -
Enable the connection to the server from
pgsql-backup
by editing the file /etc/postgresql/9.5/postgresql.conf. Change the value oflisten_addresses
to the new server, or use an asterisk to enable the connection from any IP.listen_addresses = 'localhost, pgsql-backup'NoteNote that the path to the
postgresql.conf
depends on your PostgreSQL version. Edit9.5
to the corresponding version number of your PostgreSQL installation. -
Restart the service
sudo service postgresql restartTest the connection from
pgsql-backup
by running an SQL query:psql -c 'SELECT version()' -U barman -h pgsql postgres
Creating the .pgpass file on pgsql-backup
During the installation of Barman on the server pgsql-backup
, a system user account called barman was created.
-
Set a password for the user:
sudo passwd barman -
Enter the user account:
sudo -i -u barman -
Create the .pgpass file for the barman user:
echo "pgsql:5432:*:barman:password" >> ~/.pgpassNoteThe data format for the .pgpass file is
hostname:port:database:username:password
. If an asterisk is used in any of the first four fields, it will correspond to everything. The username refers to the PostgreSQL user that was created previously, not to the Linux user account. You may refer to the official documentation for more information about this file.The file .pgpass in a user’s home directory can contain passwords to be used if the connection requires a password (and no password has been specified otherwise).
Setting up key-based authentication
To perform backups without manual authentication, generate an SSH key that will be used for authentication:
To perform backups without user intervention, we need to set up and copy SSH keys for passwordless authentication. Barman makes use of this method to copy data through rsync.
-
Connect to the server
pgsql-backup
, switch to user barman, and generate the keysssh-keygen -t rsa -
Copy the key to the user account postgres on
pgsql
:ssh-copy-id postgres@pgsqlThe following message will appear if the transfer of the key has been successful:
Number of key(s) added: 1Now try logging into the machine, with: "ssh 'postgres@pgsql'"and check to make sure that only the key(s) you wanted were added. -
Barman also requires access to the Postgres account on the server
pgsql-backup
. Copy the key into the directory of the postgres user and test the connection:ssh-copy-id postgres@localhostssh postgres@localhost -C true -
Once this is done, log in as postgres user on
pgsql
and generate an SSH key:ssh-keygen -t rsa -
Copy the generated key to the list of authorized keys of the user barman on
pgsql-backup
:ssh-copy-id barman@pgsql-backup -
Test the connection to the server:
ssh barman@pgsql-backup -C true
Configuring Barman
The configuration of Barman is done on the server pgsql-backup
.
-
Create the directory
/etc/barman.d
:mkdir /etc/barman.d -
Open the file
/etc/barman.conf
and remove the leading semicolon (;) from the following line:;configuration_files_directory = /etc/barman.d -
Enter the directory
/etc/barman.d
and create a filepgsql.conf
with the following content:[pgsql]description = "Old PostgreSQL server"conninfo = host=10.55.66.77 user=barman dbname=Our_Databasessh_command = ssh postgres@pgsqlretention_policy = RECOVERY WINDOW OF 2 WEEKSThe information in this file is self-explanatory. The retention_policy defines the time frame during which backups are kept. This can be set to your needs. For more information about this file, you may refer to the official documentation.
Configuring PostgreSQL
-
Connect to
pgsql
and add this line to the file/etc/postgresql/9.5/main/pg_hba.conf
:host all all 10.55.66.88/32 trustWhere 10.55.66.77 should be replaced with the IP of
pgsql-backup
. -
Open the file
/etc/postgresql/9.5/main/postgresql.conf
and edit the values as shown:wal_level = archivearchive_mode = onarchive_command = 'rsync -a %p barman@10.55.66.77:/var/lib/barman/pgsql/incoming/%f' -
Restart the PostgreSQL service:
sudo systemctl restart postgresql -
Check if the connection to the server
pgsql
works from Barman, by running the commandbarman check pgsql
after logging into the barman user account onpgsql-backup
:barman@pgsql-backup:~$ barman check pgsqlServer pgsql:PostgreSQL: OKarchive_mode: OKwal_level: OKarchive_command: OKcontinuous archiving: OKdirectories: OKretention policy settings: OKbackup maximum age: OK (no last_backup_maximum_age provided)compression settings: OKminimum redundancy requirements: OK (have 0 backups, expected at least 0)ssh: OK (PostgreSQL server)not in recovery: OK
If all values are OK, you are ready to make your first backup.
Performing Backups
- Start a backup with the command
barman backup pgsql
. You will see an output like the following:barman@pgsql-backup:~$ barman backup pgsqlStarting backup for server pgsql in /var/lib/barman/pgsql/base/20180911T115925Backup start at xlog location: 0/2000028 (000000010000000000000002, 00000028)Copying files.Copy done.Asking PostgreSQL server to finalize the backup.Backup size: 20.7 MiBBackup end at xlog location: 0/20000C0 (000000010000000000000002, 000000C0)Backup completed - To view a list of all backups available of
pgsql
, use the commandbarman list-backup pgsql - To view the details about a backup, use the following command:
barman show-backup pgsql backup_id
The backup_id is visible from the backup list and in the form like this example 20180911T115925.
Restoring Backups
It is possible to import the backup in the PostgreSQL server running on pgsql-backup
.
- Stop the PostgreSQL service:
sudo systemctl stop postgresql
- Load the backup into the database with the following command:
barman recover --remote-ssh-command "ssh postgres@localhost" pgsql 20180911T115925 /var/lib/postgresql/9.5/main
You will see an output like the following:
Starting remote restore for server pgsql using backup 20180911T115925Destination directory: /var/lib/postgresql/9.5/mainCopying the base backup.Copying required WAL segments.Generating archive status filesYour PostgreSQL server has been successfully prepared for recovery!
- Once the backup has been imported, restart the PostgreSQL service:
sudo systemctl start postgresql
Your database has now successfully migrated to your new database server.
Automatizing Backups
It is possible to run Barman automatically to back up the content of your database regularly. This can be done easily by setting up a cron job on pgsql-backup
.
- Switch into the barman user:
sudo -i -u barman
- Edit the crontab of the user:
crontab -e
- To run a backup daily at 3:30, add the following line to the crontab and save it:
30 03 * * * /usr/bin/barman backup pgsql
For more information about Barman, refer to the official documentation.