Kamis, 18 Juli 2024

POSTGRES-pgbackrest remote

 Written by Granthana Biswas

https://www.cybertec-postgresql.com/en/remote-backup-and-restore-with-pgbackrest/

In my previous post about pgBackRest, we saw how to install and setup pgBackRest and make a backup of a PostgreSQL database with it. It was a very basic single server setup,  only intended to get the hang of the tool. Such setups are not used in a production environment, as it is not recommended (or rather does not serve the purpose) to perform the backup on the same server where the database is running. So: let's get familiar with how remote backup servers are set up with pgBackRest, and how a full and incremental backup is performed from the backup server and restored on the database server.

We need two servers. Let's call ours:

  1. pgbackup
  2. db1

INSTALLING PGBACKREST:

We need to install pgBackRest on the database and the backup server. Make sure you install the same version on both.

For the database server, please follow the installation steps from my previous post. The steps are slightly different for the backup server, since it is a better practice to create a separate user to own the pgBackRest repository.

Create a pgbackrest user on the backup server

Install required Perl package and pgBackRest from a package or manually on pgbackup as below

Create pgBackRest configuration files, directories and repository on pgbackup

Now we are ready to proceed with enabling communication between the database and the backup server. For that, pgBackRest requires a passwordless SSH connection.

We can do that by generating an SSH authentication key file using the

command.

On the pgbackup server as pgbackrest user:

On db1 as postgres user:

Exchange the public keys generated between the servers

On pgbackup:

On db1:

Test the passwordless connection as follows:

You can find more details about passwordless SSH connection here.

CONFIGURATION

On the pgbackup server, configure the pgBackRest configuration file with the database host and path, along with the repository path where the backups will be stored. We have added the database host as 'pg1-host' so that we can add more database hosts later as 'pg2-host' and so on. The same goes for pg1-path.

The Start Fast Option (--start-fast) has been used to force a checkpoint to start the backup quickly. Otherwise, the backup will start after the next regular checkpoint.

On server db1, configure the pgBackRest configuration file with the database path and backup host as follows:

On db1, update postgresql.conf to have the following changes:

Now restart PostgreSQL to reflect the configuration changes.

Create a stanza on the pgbackup server and check if it's working. Getting no result means the stanza has been created successfully.

Check if the stanza configuration is correct on db1:

REMOTE BACKUP

With the setup ready, let's take our first remote backup from pgbackup. The default backup is incremental, but the first backup is always a full backup.

You will find directories and files being created in the backup location.

RESTORE

Taking a full backup was easy. Let's see some examples of incremental backup and recovery with a target:

Create a test database on db1:

Make another backup. By default, this will be incremental since it's the second backup and we are not specifying the type:

To restore, stop PostgreSQL on db1 and run pgbackrest with the restore command:

If you check the database now, you will not find the test database. This is because the backup was restored from the first full backup.

To restore the data up to the incremental backup, run the restore command with recovery_target in --recovery-option.

Let's check the database now:

I hope this exercise could help to get you started with remote backup and pgBackRest! To learn more commands, please visit the pgBackRest official site here.

Stay tuned for more tutorials and exercises with pgBackRest. If you are not sure how to use PostgreSQL efficiently or how to setup your systems in general consider checking out our support services.


Tidak ada komentar:

Posting Komentar