Point-in-time recovery is something that comes up as soon as any types of backups are mentioned. In this blog post we are going to tell you how point-in-time recovery works with PostgreSQL.
What is Point-in-Time Recovery in PostgreSQL?
In general, point-in-time recovery (or PITR for short) refers to the recovery of data when an administrator recovers a set of data or a particular setting from a time in the past. PostgreSQL in particular comes with the ability to do incremental backups and point-in-time recovery out of the box. PostgreSQL has these files called “WAL” files - WAL stands for “write ahead log” and it’s the cornerstone of PostgreSQL’s ability to do point-in-time recovery. To perform point-in-time recovery in PostgreSQL, start from the restoration of a full backup, then progressively fetch and apply the archived WAL files up to a specified timestamp. To do that, follow these steps:
- Setup WAL archiving: create the directory in which the WAL files will be stored, then make sure it’s owned by the ‘postgres’ user.
- Make sure to enable the archive mode in the PostgreSQL configuration (archive_mode=on), set the wal_lever to archive and set the archive_command to such a command that enables to copy the files into their destination. It can be a one-liner or a call to a complex script or a binary.
- Restart PostgreSQL.
- Take a base backup:
- pg_basebackup -Uusername -h127.0.0.1 --progress -D /basebackup/
- Figure out a point at which you want to restore your data and stop the PostgreSQL server.
- Remove all of the data directory, restore the base backup and then remove any existing WAL files:
- rm -rf /var/lib/postgresql/10/main/*
- cp -r /basebackup/* /var/lib/postgresql/10/main/
- rm -rf /var/lib/postgresql/10/main/pg_wal/*
- Prepare the recovery.conf file:
- vim /var/lib/postgresql/10/main/recovery.conf
- restore_command = ‘cp /wal_archive/%f “%p”’
- recovery_target_lsn = ‘3/72658818’
- In this example, a particular LSN used was a recovery target (i.e the recovery is stopping at that point)
- Finally, make sure that all of the files in the PostgreSQL data directory have a correct owner and start PostgreSQL.
Once you do that, the server will be in recovery mode. Switch it to operate normally (run SELECT pg_wal_replay_resume()) and tell recovery.conf how PostgreSQL should behave after the recovery using recovery_target_action. You have three options, but since promote is the one that will end the recovery mode as soon as the recovery itself completes, we will suggest you use that. You can also shutdown the server after the recovery. After the recovery completes, your recovery.conf file will be renamed to recovery.done.
Performing Point-in-Time Recovery with Backup Ninja
Point-in-time recovery for PostgreSQL can also be done with Backup Ninja. First, schedule your PITR backups:
Once you have enabled PITR and configured the rest of your settings, you can also schedule the backup as a last step of the process:
Backup Ninja also allows you to see all of the backups that have been completed already:
If you want to restore any of the backups that you see, simply click the “Restore” button:
Point-in-time recovery is something that concerns database administrators that deal with all kinds of databases, be it MySQL or PostgreSQL. If you are using PostgreSQL and want to use point-in-time recovery to help your application or database, Backup Ninja can prove to be a very good choice.