Comprehensive Guide to PostgreSQL Backup and Recovery with pg_rman
Installing pg_rman
Download pg_rman from the official repository: https://github.com/ossc-db/pg_rman/releases
Select the appropriate version for your system. This guide uses version REL_13_STABLE on CentOS 7.6.
The installation process is straightforward and follows these steps:
# yum -y install zlib-devel
# unzip pg_rman-REL_13_STABLE.zip
# make
# make install
Backup Strategy
Before implementing backups, define your strategy:
- Perform a full backup daily, retaining 7 days of backups
- Store backups in /postgresql/backup directory
Backup Implementation
Create a backup script with the following content:
#!/bin/bash
export PGPORT=5432
export PGDATA=/postgresql/pgdata
export PGHOME=/postgresql/pg13
export LD_LIBRARY_PATH=$PGHOME/lib:/lib64:$LD_LIBRARY_PATH
export PATH=$PGHOME/bin:$PATH
export PGHOST=$PGDATA
export PGUSER=postgres
export PGDATABASE=postgres
if [ $UID -ne 2000 ]
then
echo "please run as pgsql user!"
fi
backup_path="/postgresql/backup"
ini_file="/postgresql/backup/pg_rman.ini"
if [ ! -f ${ini_file} ]
then
pg_rman init -B ${backup_path}
fi
pg_rman backup --backup-mode=full --backup-path=${backup_path}
pg_rman validate --backup-mode=full --backup-path=${backup_path}
retention_date=`date +%Y-%m-%d -d '7 day ago'`
echo ${retention_date}
pg_rman delete DATE ${retention_date} --backup-path=${backup_path}
# Schedule the backup task
crontab -e
Testing Backup and Recovery Scenarios
Scenario 1: Recovery from Full Backup
After performing a full backup at 11:00, a table was created at 14:17 with some data, followed by an archive switch at 14:19. Additional data was inserted at 14:20 before the next archive switch.
$ pg_rman backup --backup-mode=full -B /postgresql/backup
$ pg_rman validate -B /postgresql/backup
Expected result: The restored database should not contain the table created after the backup time.
After copying the backup to a recovery server and performing:
$ pg_rman restore -B /postgresql/backup
$ chmod 700 pgdata/
$ pg_ctl start -l /postgresql/pg_log/pg.log
Verification confirmed that the table created after the backup was not present, as expected.
Scenario 2: Recovery with Archive Logs
When archive logs from the primary server are copied to the recovery server, the system can recover to the latest point covered by those archives.
After copying the archive file 000000010000000000000017 to the recovery server and performing:
$ pg_rman restore -B /postgresql/backup --hard-copy
The database recovered to include the table with IDs 1, 2, and 3, but not ID 4, which was inserted after the last archive switch.
Scenario 3: Point-in-Time Recovery
To recover to a specific timestamp before the latest archive:
$ pg_rman restore -B /postgresql/backup --hard-copy --recovery-target-time='2023-12-21 14:18:30'
This successfully restored the database to contain only the table entries with IDs 1 and 2, excluding ID 3 wich was inserted after the specified time.
Scenario 4: Recovery Between Two Full Backups
When multiple full backups are available, the system can recover to any point between them:
$ pg_rman restore -B /postgresql/backup --hard-copy --recovery-target-time='2023-12-21 14:18:30'
The system correctly identified and used both available backups to perform the recovery, successfully restoring to the specified timestamp.
Scenario 5: Recovery with Unarchived WAL Files
To recover data that exists in WAL files but hasn't been archived yet:
- Identify the relevant WAL file (e.g., 00000001000000000000001A)
- Copy it to the recovery server's archive directory
- Perform the restore operation
$ pg_rman restore -B /postgresql/backup --hard-copy
$ pg_ctl start
The recovery process successfully included data from the unarchived WAL file, demonstrating that point-in-time recovery is possible even with unarchived changes.
Note: After recovery, the database remains in read-only mode as long as the recovery.signal file exists in the data directory. To make the database writable, this file must be removed.