backup recovery - postgresql database
DESCRIPTION
A quick synopsis of Backup-Recovery in PostgreSQL - Logical Backup, Physical Backup and PITRTRANSCRIPT
Backup-Recovery in PostgreSQL
Logical Backup, Physical Backup and PITR
15/7/2013
Co
nfi
den
tial
info
rmat
ion
, fo
r in
tern
al u
se o
nly
CONFIDENTIAL
© 2012 EnterpriseDB. All rights reserved.
Available Backup and Recovery Tools
Physical and Logical Backup
Logical Backup using pg_dump
Instance level logical backup using pg_dumpall
Table level, Schema level or DB level logical backup
Different file formats available for pg_dump backup
pg_restore to restore binary backup with pg_dump
Physical Backups• pg_basebackup
• Compressed backup
• Recovery with WAL and archived WAL
• Recover to the point of crash
• Point in Time Recover to recover from data corruption due to a bad query/batch2
Co
nfi
den
tial
info
rmat
ion
, fo
r in
tern
al u
se o
nly
Online Backup
Enable Archiving• archive_command = 'cp %p /mnt/server/archivedir/%f‘
• wal_level = archive
• Archive_mode = on
Take a backup• SELECT pg_start_backup('label');
• Take a backup (file system level copy)
• SELECT pg_stop_backup();
Alternative to Manual backup: Use pg_basebackup for taking a backup• pg_basebackup -D /opt/PostgresPlus/9.2AS/data -h 192.168.160.147 -W -p 5444 -Fp --xlog-
method=stream
3
Co
nfi
den
tial
info
rmat
ion
, fo
r in
tern
al u
se o
nly
Recovery: Restore the server
Stop the server, if it's running. Change the pg_hba.conf to disallow user’s login during recovery
Keep a backup of original server or at least pg_xlog
Restore the database files from your file system backup. Be sure about the ownership and soft links
4
Co
nfi
den
tial
info
rmat
ion
, fo
r in
tern
al u
se o
nly
Recovery: Prepare the Server
Remove any files present in pg_xlog/;
If you have unarchived WAL segment files that you saved (step 2), copy them into pg_xlog/
Create a recovery command file recovery.conf in the cluster data directory:• restore_command = 'cp /mnt/server/archivedir/%f "%p" '
If this is a point in time recovery to recover from a bad batch operation or a bad command which has corrupted the data then add recovery_target_time to a timestamp which is just before the bad command/batch was fired• recovery_target_time = '18-JUL-13 23:07:05.39855'
5
Co
nfi
den
tial
info
rmat
ion
, fo
r in
tern
al u
se o
nly
Start the Recovery
Start the server• The server will go into recovery mode and proceed to read through the archived
WAL files it needs
• Should the recovery be terminated because of an external error, the server can simply be restarted and it will continue recovery
• Upon completion of the recovery process, the server will rename recovery.conf to recovery.done
Inspect the contents of the database to ensure you have recovered to the desired state. If not, restart the whole process
6