In one of our previous posts—pgBackRest: A Great Backup Solution and a Wonderful Year of Growth—we discussed the installation of pgBackRest and the backup, quick restore command. It would be ideal to have a restoration activity scheduled as per business needs so that backups are validated and provide critical metrics on recovery time. In this blog post, we will check out some restoration scenarios concerning the pgBackRest backup tool.

Scenarios

  • Restoring the backup on a different host
  • Restoring a single database from the Postgres cluster
  • Restoring the backup to a specific point in time
  • Restoring the backup on a different host to start as a streaming standby
  • Restoring only the delta changes

To proceed with testing the above-mentioned restoration scenarios, we need the following:

  • pgBackRest of the same version installed on source and target machines
  • Repository information i.e, the place where we store our backups could be S3, GCS, Azure buckets, or dedicated storage.
  • The target server should have access to the repository host via TLS or SSH

For testing the above-mentioned scenarios, I have a PostgreSQL primary server, two replica servers, a repository server, and a spare server.

My repository information: a FULL and INCR  backup is available in the repository.

Restoring the backup on a different host

In this scenario, we will test the backup by restoring it to the spare server. My spare server’s pgBackRest conf has the information about the repository host, repository path, repository host user, and required PostgreSQL version installed and access to the repository.

pgBackRest can be used entirely by command line parameters but having a configuration file has more convenience. Below is my spare server pgBackRest configuration file.

I will go ahead and restore the backup available in my repository onto the spare server. By default, it restores the latest available backup set available in the repository.

We need to make sure the archive_command is altered to a different path or disabled to prevent Postgres from writing archives to the existing repo upon starting on the spare server.

Restoring a single database from the Postgres cluster

In this scenario, we will restore only a single database on the spare server. I have below mentioned databases on my primary database.

Primary server:

We will restore only the Percona database to my spare server.

Start the Postgres cluster on the spare server and check the Percona database.

After restoring the specific database you will still get to see the other user databases as well. However, when a user will try to connect to them (i.e. apart from the database specified with –db-include option) the connection will not happen and the user will get a FATAL error like this: “FATAL:  relation mapping file “base/xx/pg_filenode.map” contains invalid data”.

Restoring the backup to a specific point in time

Point-in-time recovery is possible with the pgBackRest, Consider a table/database has been dropped and needs to get restored. In this situation, we need the timestamp of the drop event, pgBackRest backup, and archives.

On my primary node, I have a table named pitr in the Percona database. This table has dropped at timestamp 2022-11-04 14:24:32.231309+05:30.

By using the pgBackRest restore option with the recovery type command we can achieve Point-In-Time-Recovery, by default this type restores the archive to the end of the stream, In this scenario, we will specify the exact time stamp to restore the table, xid and lsn can also be provided to the type command.

Start the Postgres cluster and check the pitr table.

Restoring the backup on a different host to start as a streaming standby

This approach will be useful in case we need to rebuild a crashed standby node or add a new standby to the existing Postgres cluster. 

Primary server:

Two replica servers are connected to the primary server.

Let’s go ahead and add another replica using the pgBackRest. 

The replica server is up and running, Check the pg_stat_replication on the primary node.

Restoring only the delta changes

The delta option allows pgBackRest to automatically determine which files in the database cluster directory can be preserved and which ones need to be restored from the backup, Consider the situation as we already have a backup restored on a spare server and now we will perform the delta restore on the spare server again to get the delta data that has been backed up since the last restore.

 

Overall, pgBackRest is an incredibly popular open-source database backup tool available for PostgreSQL. We also encourage you to try our product Percona Distribution for PostgreSQL, as pgBackRest tool is part of our distribution for backups.

Subscribe
Notify of
guest

6 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Abdul Sayeed

Hi @Bhargav, Congratulations on your first blog. I have gone through and it has much data to play-around with pgbackrest utility. It would be helpful for DBAs on routine backup operations. Thank you for your efforts.

Max

It is a common use case in enterprises to refresh a pre-production system from production on a regularly basis, e.g. daily. The backups done so far on the pre-production system should also be kept to be able to do a point-in-time restore just before such a refresh if needed.

I tried to implement this by doing a “Restoring the backup on a different host” on a regular basis. I always got ““ERROR: [045]: WAL file already exists in the repo1 archive with a different checksum” from “archive-push” afterwards when I used the same repo1.
I had to recreate the pgbackrest stanza with a different name to get a new empty repo1 to work around this error. This also means that I have to reconfigure my pgbackrest settings to use the old stanza before a restore. Not that good.

Any idea on this?

Ninad

Remove old archives from the target node.

Đại Nguyễn

Anyone can explain what is delta in pgBackRest and give me use case to use delta option ? , i still don’t understand delta option although i have read some documents

Thank you so much

Ninad

Delta means changes from the previous backup.

Woody

The –delta option allows you to start the restore with a full database directory (failed or user-broken or whatever) and use a file list with checksums to remove files which aren’t in the backup and restore those which are changed. This saves you restoring files which are not changed. It is analogous to an rsync with –delete to recover a broken directory structure – the end result is the same but time and data transfer is saved.