How To:Use Amanda to Back Up PostgreSQL
This article is a part of the How Tos collection.
The ampgsql(8) application uses the continuous WAL archiving feature of PostgreSQL (8.0+) to provide online, incremental, full-database backups.
NOTE: | Tablespaces are not currently supported. |
Setup
Amanda Server Configuration
You need to add the ampgsql application and a corresponding dumptype to your amanda.conf(5)
define application app_ampgsql { comment "ampgsql" plugin "ampgsql" property "TMPDIR" "/tmp" } define dumptype dt_ampgsql { program "APPLICATION" application "app_ampgsql" }
More information about application properties can be found in the man page (ampgsql(8))
NOTE: | The directory specified by TMPDIR needs to have enough free space to store an entire copy of the database |
You can then add a disklist(5) entry for the server you want to backup. For example:
foo.example.com bar dt_ampgsql
PostgreSQL Server Configuration
First, create a directory for PostgreSQL to archive WAL files to, commonly a sibling of the postgres data directory. This example uses /var/pgsql/archive
. Make sure that the user postmaster (the PostgreSQL server) runs as can create files in /var/pgsql/archive
, and that the Amanda user has at least read and execute permissions on the directory, and preferably also write permissions.
You need to edit your server configuration (usually postgresql.conf
) to enable continuous archiving. Add the following line:
archive_command = 'test ! -f /var/pgsql/archive/%f && cp %p /var/pgsql/archive/%f'
For PostgreSQL 8.3 and newer, you also need to add
archive_mode = on
NOTE: | Amanda will need access to superuser privileges. You can create a new role using the createuser program or CREATE ROLE. |
Amanda Client Configuration
On the client, you need to add the connection information to your amanda-client.conf(5)
property "PG-DATADIR" "/var/pgsql/data" property "PG-ARCHIVEDIR" "/var/pgsql/archive" property "PG-HOST" "/tmp" property "PG-USER" "amandabackup" property "PG-PASSFILE" "/etc/amanda/pg_passfile"
PG-DATADIR
should be the data/cluster directory for your PostgreSQL serverPG-ARCHIVEDIR
should be the directory that yourarchive_command
copies files toPG-HOST
can either be a hostname or a directory. TCP and UNIX sockets are used to connect to the server, respectivelyPG-USER
determines the user Amanda will connect as. It must have superuser privelegesPG-PASSFILE
is the credentials file that Amanda will use to connect.
The credentials file will need to have a line that matches the connection parameters. Based on the example, the following would be appropriate:
/tmp:*:*:amandabackup:my_backup_password
NOTE: | The credentials file needs to be owned by the user Amanda will run as and must have read (and perhaps write) access for that user only. Otherwise it will be ignored. |
More information about application properties can be found in the man page (ampgsql(8))
Restore
Using amfetchdump or amrestore
amfetchdump(8) and amrestore(8) extract the backup images from the tape, but do not run ampgsql(8) to extract the files from the backup images.
First, you need to extract the base image. The following example assumes that the base backup image is named example.com.foo.20090312161303.0
- tar --extract --file example.com.foo.20090312161303.0
- mkdir data archive
- tar --extract --directory data --file data_dir.tar
- tar --extract --directory archive --file archive_dir.tar
Next, you need to extract each incremental. The following example assumes that there are two incremental backup images, named example.com.foo.20090313161303.1
(a level 1 incremental) and example.com.foo.20090314161303.2
(a level 2 incremental)
- tar --extract --directory archive example.com.foo.20090313161303.1
- tar --extract --directory archive example.com.foo.20090314161303.2
Using amrecover
After running amrecover(8), you should have a newly-created archive
directory and, if you restored a base backup, a data
directory.
Common steps
The archive
directory will contain any archived WAL log files. It may be empty. The data
directory will contain a complete backup of the database/cluster directory (directories like pg_xlog and pg_clog).
You need to move the contents of the data
directory to an appropriate location. Afterwards, you may need to change the ownership and permissions of the files. they should be owned by the user that PostgreSQL runs as and only be accessible to that user (0600 for files, 0700 for directories).
Then put a recovery.conf
file in the (relocated) data
directory. This is usually simple:
restore_command = 'cp /var/pgsql/archive/%f "%p"'
See the PostgreSQL manual for more options.
Once that's done, the database server should recover once you start it. You can monitor its log messages for progress information.
If you encounter problems, once thing to try is removing the data/pg_xlog
directory before starting the database server for recovery.