backup and-recovery2

50
www.consistentstate.com [email protected] PG West, 2009 by Kevin Kempter PostgreSQL Backup and Recovery Except where otherwise noted, this work is licensed under http://creativecommons.org/licenses/by/3.0/

Upload: command-prompt-inc

Post on 19-May-2015

1.965 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PostgreSQL Backup and Recovery

Except where otherwise noted, this work is licensed underhttp://creativecommons.org/licenses/by/3.0/

Page 2: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Session Topics

● Overview● Backup Options● Restore Options● Point in Time Recovery (PITR)● Warm Standby

Page 3: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Backup Options

● pg_dump● pg_dumpall

Page 4: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dump

● A PostgreSQL database backup/dump utility

● Creates consistent backups (even if the db is in use)

● Non-blocking

● Multiple Output File Formats

● Highly Flexible Options

Page 5: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dump

● Syntax:

pg_dump [options] dbname

● Connection Options:

pg_dump -h <host> -p <port> -U <user> -W

pg_dump --host=<host> --port=<port> --username=<user> --password

● Environment Variables:

PGDATABASE PGHOST PGPORT PGUSER

Page 6: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dump

● -s (--schema-only)

● -t (--table=<tablename>)

● -T (--exclude-table=<tablename>)

● -v (--verbose)

● -F <format> (--format=<format>)

● Common Options:

● -a (--data-only)

● -c (--clean)

● -C (--create)

● -d (--inserts)

● -N (--exclude-schema=<schema>)

● -n (--schema=<schema>)

Page 7: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dump

● File Format Options:

● Plain

● Creates a plain text SQL file (default)

-F p (--format=plain)

● Custom

● Create a custom compressed format (compatible with pg_restore)

-F c (--format=custom)

● Tar

● Creates a tar format file (also compatible with pg_restore)

-F t (--format=tar)

Page 8: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dump Examples

● $ pg_dump -C --inserts prod1_db > prod1_db.sql

Creates a dump of insert statements including a create database statement

● $ pg_dump --data-only --table=customer -F c prod1_db > prod1_db.fc.dmp

Dump the customer table in a custom format from the prod1_db database

● $ pg_dump -S prod1_db > prod1_db.ddl_only.sql

Creates a DDL only dump of the prod1_db database

● $ pg_dump --schema=gold -F t prod1_db > prod1_db.gold_schema.dmp

Creates a dump of the gold schema in the prod1_db database in a tar format

Page 9: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dumpall

● A PostgreSQL cluster (instance) backup/dump utility

● Creates consistent backups (even if the db is in use)

● Non-blocking

● Multiple Output File Formats

● Highly Flexible Options

Page 10: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dumpall

● Syntax:

pg_dumpall [options]

● Connection Options:

pg_dump -h <host> -p <port> -U <user> -W

pg_dump --host=<host> --port=<port> --username=<user> --password

● Environment Variables:

PGDATABASE PGHOST PGPORT PGUSER

Page 11: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dumpall

● -r (--roles-only)

● -s (--schema-only)

● -S (--superuser=<username>)

● -v (--verbose)

● -t (--tablespaces-only)

● --disable-triggers

● Common Options:

● -a (--data-only)

● -c (--clean)

● -D (--column-inserts) (--attribute-inserts)

● -d (--inserts)

● -g (--globals-only)

● -o (--oids)

Page 12: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_dumpall Examples

● $ pg_dumpall -g > prod1_db.global_structures.sql

Creates a cluster dump containing only the cluster global structures

● $ pg_dumpall --tablespaces-only > prod1_db.tablespaces.sql

Dump the cluster tablespaces

● $ pg_dumpall -r > prod1_db.roles_only.sql

Creates a dump of only the cluster roles

● $ pg_dumpall -o -S gold_user > prod1_db.oids.dmp

Creates a dump of the cluster including oid's as the superuser 'gold_user'

Page 13: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Restore Options

● pg_restore● psql

Page 14: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_restore

● Syntax:

pg_restore [options] [filename]

● Connection Options:

pg_dump -h <host> -p <port> -U <user> -W

pg_dump --host=<host> --port=<port> --username=<user> --password

● Environment Variables:

PGDATABASE PGHOST PGPORT PGUSER

Page 15: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_restore

● --disable-triggers

● -P function-name (args)

--function=function-name(args)

● -s (--schema-only)

● -S (--superuser=<username>)

● -v (--verbose)

● -t <table> (--table=<table>)

● -T <trigger> (--trigger=<trigger>)

● -F <format> (--format=<format>)

(c or t only)

● Common Options:

● -d <dbname> (--dbname=<dbname>)

● -a (--data-only)

● -c (--clean)

● -C (--create)

● -i (--ignore-version)

● -I (--index=<index>)

● -l (--list)

● -L <filename> (--use-list=<list-file>)

● -n (--schema=<schema-name>)

Page 16: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

pg_restore Examples

● $ pg_restore -a -F c -d prod2_db prod1_db.fc.dmp

Restores data only from a custom formatted file into database prod2_db

● $ pg_restore -c --schema=gold_partners -v -F t -d prod2_db prod_dump.tar.dmp

Cleans (removes data & structures first) then restores the gold_partners schema From a tar formatted file into the prod2_db database

● $ pg_restore --schema-only -d qa1_db -F c prod1_db.fc.dmp

Restores the schema only (DDL) from a custom formatted file into the qa1_db database

Page 17: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Create a TOC file from a dump

● $ pg_dump -Fc db1 > db1.fc.dmp

● $ pg_restore -Fc -l db1.dmp > db1.lst

Page 18: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Examining the TOC FileThe TOC Header

; Archive created at Sat Mar 28 15:02:28 2009

; dbname: dbmon

; TOC Entries: 16

; Compression: 0

; Dump Version: 1.10-0

; Format: TAR

; Integer: 4 bytes

; Offset: 8 bytes

; Dumped from database version: 8.3.5

; Dumped by pg_dump version: 8.3.5

;

Page 19: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Examining the TOC FileThe TOC File Contents

; Selected TOC Entries:

;

3; 2615 2200 SCHEMA - public postgres

1745; 0 0 COMMENT - SCHEMA public postgres

1746; 0 0 ACL - public postgres

1469; 1259 352554 TABLE public dbmon_thresh postgres

1467; 1259 352545 TABLE public vac_density_hist postgres

1468; 1259 352552 SEQUENCE public dbmon_thresh_dbmon_thresh_id_seq postgres

1747; 0 0 SEQUENCE OWNED BY public dbmon_thresh_dbmon_thresh_id_seq postgres

1748; 0 0 SEQUENCE SET public dbmon_thresh_dbmon_thresh_id_seq postgres

1736; 2604 352557 DEFAULT public dbmon_thresh_id postgres

1741; 0 352554 TABLE DATA public dbmon_thresh postgres

1740; 0 352545 TABLE DATA public vac_density_hist

Page 20: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Restore via a TOC File

● $ createdb dbmon2

● $ pg_restore -L db.lst -Ft dbmon-dg.dmp -d dbmon2

Page 21: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Restoring with psql

● $ pg_dump prod1_db > prod1_db.sql

● $ psql -ef prod1_db.sql > load_db.log 2>&1

● $ pg_dump prod1_db | psql -h qa_server

● $ pg_dumpall -g | psql -h dev_server -p 5433 > load_dev.log 2>&1

● $ pg_dump prod1_db | psql -e test_db > load_test_db.log 2>&1

Page 22: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Backup/Restore

Summary

Page 23: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR

● PostgreSQL has built-in facilities for Point in Time Recovery

● PITR Backups

– Archiving the WAL segments

– Making Base Backups

● PITR Recovery

– Restore the last Base Backup

– Prepare the recovered system data directory

– Create a recovery.conf file

– Start the postmaster

Page 24: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR BackupsEnable WAL Archiving

● Enable / set the following parameters in the postgresql.conf file:

– archive_mode = on

– archive_command = 'cp %p /stage/wal/%f'

Can be any valid shell command (including scripts)

– archive_timeout = 0

● Special tags

– %p = full path (absolute path) and the filename of the WAL segment to be archived

– %f = only the filename of the WAL segment

– %% = insert a % character in the command string.

Page 25: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR BackupsEnable WAL Archiving - Example

● mkdir /stage/wal

● chown postgres:postgres /stage/wal

(or other postgres cluster owner)

● Re-start the Server

Page 26: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR BackupsCreate Transactions - Example

● Execute SQL commands / transactions

– Enable access, turn on applications, etc

● This should force the creation of multiple archived WAL files in the /stage/wal directory

● WAL segments are copied when:

– The WAL segment is full (see checkpoint_segments)

– Number of seconds specified in archive_timeout has passed

Page 27: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR BackupsCreate Base Backup - Example

● Execute pg_start_backup

– $ psql pitr_test

– # select pg_start_backup ('tag')

● Archive the cluster data directory (and any related tablespaces)

– $ tar -czvf /backups/pitr/<date>.data.tar.gz ./data

– $ rsync

– $ other copy methods

● Execute pg_stop_backup

– $ psql pitr_test

– # select pg_stop_backup ()

Page 28: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR BackupsCreate More Transactions - Example

● Execute SQL commands / transactions – The application, user connections, etc will continue to

generate transactions (and archived WAL segments)

● Verify the creation of additional archived WAL files in the /stage/wal directory

Page 29: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recovery

(1) If available copy the original cluster data directory to an alternate location

if space is an issue at least copy the old pg_xlog dir it may contain additional unarchived WAL segments

(2) Ensure the postmaster is not running

Page 30: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recovery

If your backup was an rsync to a second server then skip steps 3 & 4

(3) Remove the cluster data directory and any tablespace directories

(4) Restore your last system backup – make sure permissions are retained

– If you're using tablespaces then verify that the symbolic links in pg_tblspc/ were restored

Page 31: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recovery

(5) Remove any wal segments from the pg_xlog dir that were restored from the backup

– If you didn't backup pg_xlog then create it, make sure you re-establish it as a symbolic link if needed (if you had moved the tx logs to another disk)

– If needed also re-create the pg_xlog/archive_status directory

(6) Copy the files from the original pg_xlog dir (if available) into the new pg_slog dir (do a copy as opposed to a move in case you need to start over).

Page 32: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recovery

(7) Create a recovery command file named recovery.conf in the cluster data directory.

(8) [Optional] Temporarily modify pg_hba.conf to prevent ordinary users from connecting until the recovery is complete

(9) Start the server.

The server will go into recovery mode via the recovery.conf file. Once the recovery is complete then the server will become available and rename the recovery.conf file to recovery.done If an error interrupts the recovery (or stops the server) then simply re-starting the server will restart the recovery

Page 33: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recovery

(10) Verify the recovery.

If the database was not recovered properly (or to a state that you desire) then go back to step 1

(11) restore the pg_hba.conf to its original state and run a pg_ctl reload (if it was modified for the recovery)

Page 34: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recoveryrecovery.conf

● Recovery settings are placed in the file 'recovery.conf'

● restore_command (string)

must return nonzero

– restore_command = 'cp /stage/wal/%f %p'

– restore_command = '/usr/local/bin/restore_shell.sh %p %f'

Page 35: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recoveryrecovery.conf

● recovery_target_time (timestamp)

– specifies the time stamp up to which recovery will proceed.

– recovery_target_time and recovery_target_xid are mutually exclusive

– The default is to recover to the end of the WAL log.

Page 36: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recoveryrecovery.conf

● recovery_target_xid (string)

– specifies the transaction ID up to which recovery will proceed.

– Although transaction IDs are assigned sequentially at transaction start, transactions can complete in a different numeric order.

– All transactions committed before the specified XID will be recovered

Page 37: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recoveryrecovery.conf

● recovery_target_timeline (string)

– Specifies recovering into a particular timeline for complex re-recovery scenarios. See the online docs for more info.

● log_restartpoints (boolean)

– If true, each restart point will be logged. This can be helpful to track the progress of a long recovery. The default is false.

Page 38: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

PITR Recovery

Summary

Page 39: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm Standby

● The PostgreSQL built-in facilities for PITR can be leveraged to create a 'warm standby' server

● A Warm Standby server consists of the following:

– A server with a running postmaster (in recovery mode)

– A recovery command that keeps the server in recovery mode

– A continous stream of archived WAL segments from the “master”

– A failover mechanism

Page 40: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbyOverview

● Configure WAL archiving on the master

● Make the WAL segments available to the warm standby server

● Setup recovered cluster on the warm standby server

● Setup the recovery command scripts

● Setup failover mechanism

Page 41: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup WAL Archiving

● Enable WAL archiving in the postgresql.conf file (on the master)

– archive_mode = on

– archive_command = 'cp %p /stage/wal/%f'

– archive_timeout = 0

● Create a directory for the archived WAL segments

– $ mkdir /stage/wal

– $ chmod postgres:postgres /stage/wal

Page 42: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbyMake WAL segments available to the warm standby server

● NFS (not the best choice)

● rsync

● scp

● Other methods

Page 43: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup recovered cluster on the warm standby server

Use the same method as described in the PITR recovery steps

Page 44: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup recovery command scripts

● Pseudocode for a suitable restore_command is:triggered = false;

while (!NextWALFileReady() && !triggered)

{

sleep(100000L); /* wait for ~0.1 sec */

if (CheckForExternalTrigger())

triggered = true;

}

if (!triggered)

CopyWALFileForRecovery();

http://www.postgresql.org/docs/8.3/static/warm-standby.html

Page 45: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup recovery command scripts – pg_standby

* pg_standby.c

* Production-ready example of how to create a Warm Standby

* database server using continuous archiving as a

* replication mechanism

*

* We separate the parameters for archive and next WAL file

* so that we can check the archive exists, even if the

* WAL file doesn't (yet).

*

* This program will be executed once in full for each file

* requested by the warm standby server.

*

* It is designed to cater to a variety of needs, as well

* providing a customizable section.

*

* Original author:

Simon Riggs [email protected]

* Current maintainer: Simon Riggs

● PostgreSQL contrib pg_standby

Page 46: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup recovery command scripts – recovery.conf file

● Using a shell script– restore_command = '/home/postgres/wst/bin/shell_script.sh %p %f'

● Using the pg_standby contrib– restore_command =

pg_standby [options] archivelocation %f %p %r [RESTARTWALFILE]

%r = “The size of the WAL archive can be minimized by using the %r option of the restore_command. This option specifies the last archive file name that needs to be kept to allow the recovery to restart correctly. This can be used to truncate the archive once files are no longer required, if the archive is writable from the standby server.” **

** http://www.postgresql.org/docs/8.3/static/warm-standby.html

Page 47: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup recovery command scripts – recovery.conf file

● pg_standby [options] **

** http://www.postgresql.org/docs/8.3/static/pgstandby.html

Option Default Use cp or copy command to restore WAL files from archive.

-c yes Remove files from archivelocation so that no more than this many WAL files before the current one are kept in the archive. Zero (the default) means not to remove any files from archivelocation.

-d no Use ln command to restore WAL files from archive. Link is more efficient than copy, but the default is copy since link will not work in all scenarios

-k <numfiles> 0 Remove files from archivelocation so that no more than this many WAL files before the current one are kept in the archive. Zero (the default) means not to remove any files from archivelocation. This parameter will be silently ignored if restartwalfile is specified

-l no Use ln command to restore WAL files from archive. Link is more efficient than copy, but the default is copy since link will not work in all scenarios.

Page 48: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm StandbySetup recovery command scripts – recovery.conf file

● pg_standby [options] **

** http://www.postgresql.org/docs/8.3/static/pgstandby.html

Option Default Use cp or copy command to restore WAL files from archive.

-r <max retries> 3 Set the maximum number of times to retry the copy or link command if it fails. After each failure, we wait for sleeptime * num_retries so that the wait time increases progressively.

-s <sleep time> 5 Set the number of seconds (up to 60) to sleep between tests to see if the WAL file to be restored is available in the archive yet.

-t <trigger file> none Specify a trigger file whose presence should cause recovery to end whether or not the next WAL file is available.

-w <max wait time> 0 Set the maximum number of seconds to wait for the next WAL file, after which recovery will end and the standby will come up. A setting of zero (the default) means wait forever.

Page 49: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

Warm Standby

Summary

Page 50: Backup and-recovery2

[email protected]

PG West, 2009by Kevin Kempter

End