perl-lib-path /usr/lib/perl5 perl-bin-path /usr/bin backrest-repo-path /var/lib/backrest postgres-cluster-demo demo backrest-config-demo /etc/{[backrest-exe]}.conf postgres-config-demo /etc/postgresql/9.4/{[postgres-cluster-demo]}/postgresql.conf db-path /var/lib/postgresql/9.4/{[postgres-cluster-demo]} cmd-backup-last ls -1 /var/lib/backrest/backup/demo | tail -3 | head -1 test-table-data Very important data pg_dropcluster {[dash]}-stop 9.4 {[postgres-cluster-demo]} apt-get remove -y libdbd-pg-perl libdbi-perl libnet-daemon-perl libplrpc-perl root chmod 777 /home/vagrant root rm {[backrest-config-demo]} root chmod 777 /etc root rm -rf {[backrest-repo-path]} root rm -rf {[perl-lib-path]}/BackRest root rm {[perl-bin-path]}/{[backrest-exe]} root rm -rf /home/vagrant/backrest-release-{[version]} root mkdir /home/vagrant/backrest-release-{[version]} vagrant cp -r /backrest/bin /home/vagrant/backrest-release-{[version]} vagrant cp -r /backrest/lib /home/vagrant/backrest-release-{[version]} vagrant
Introduction

This user guide is intended to be followed sequentially from beginning to end — each section depends on the last. For example the Backup section relies on setup that is performed in the Quick Start section. Once you have up and running it possible to skip around but it is recommended to follow the user guide in order the first time through.

Although the examples are targeted at Ubuntu and 9.4 they will also work fine on Debian and it should be fairly easy to apply this guide to any Unix distribution and version. The only OS-specific commands are those to create, start, stop, and drop clusters. The commands will be the same on any Unix system though the locations to install Perl libraries and executables may vary. Configuring archiving is different on versions <= 8.4 and configuration information can be found in the documentation.

A somewhat novel approach is taken to documentation in this user guide. Each command is run on a virtual machine when the documentation is built from the XML source. This means you can have a high confidence that the commands work correctly in the order presented. Output is captured and displayed below the command when appropriate. If the output is not included it is because it was deemed not relevant or was considered a distraction from the narrative.

All commands are intended to be run as an unprivileged user that has sudo privileges for both the root and postgres users. It's also possible to run the commands directly as their respective users without modification though in that case you can also strip off the sudo commands if you like.

Concepts

The following concepts are defined as they are relevant to , , and this user guide.

Backup

A backup is a consistent copy of a database cluster that can be restored to recover from a hardware failure, to perform Point-In-Time Recovery, or to bring up a new replica.

Full Backup: copies the entire contents of the database cluster to the backup server. The first backup of the database cluster is always a Full Backup. is always able to restore a full backup directly. The full backup does not depend on any files outside of the full backup for consistency.

Differential Backup: copies only those database cluster files that have changed since the last full backup. restores a differential backup by copying all of the files in the chosen differential backup and the appropriate unchanged files from the previous full backup. The advantage of a differential backup is that it requires less disk space than a full backup, however, the differential backup and the full backup must both be valid to restore the differential backup.

Incremental Backup: copies only those database cluster file that have changed since the last backup (which can be another incremental backup, a differential backup, or a full backup). As an incremental backup only includes those files changed since the prior backup, they are generally much smaller than full or differential backups. As with the differential backup, the incremental backup depends on other backups to be valid to restore the incremental backup. Since the incremental backup includes only those files since the last backup, all prior incremental backups back to the prior differential, the prior differential backup, and the prior full backup must all be valid to perform a restore of the incremental backup. If no differential backup exists then all prior incremental backups back to the prior full backup, which must exist, and the full backup itself must be valid to restore the incremental backup.

Restore

A restore is the act of copying a backup to a system where it will be started as a live database cluster. A restore requires the backup files and one or more WAL segments in order to work correctly.

Write Ahead Log (WAL)

WAL is the mechanism by which ensures that no committed changes are lost. Transactions are written sequentially to the WAL and a transaction is considered to be committed when those writes are flushed to disk. Afterwards, a background process writes the changes into the main database cluster files (also known as the heap). In the event of a crash, the WAL is replayed to make the database consistent.

WAL is conceptually infinite but in practice is broken up into individual 16MB files called segments. WAL segments follow the naming convention 0000000100000A1E000000FE where the first 8 hexadecimal digits represent the timeline and the next 16 digits are the WAL segment sequence number.

A valid backup will always include at least one WAL segment even if no writes were made to the database between backups.

Installation

is written in Perl which is included with Ubuntu by default. A few additional modules are required which are all available as packages.

Install required Perl modules apt-get install libdbd-pg-perl libdbi-perl libnet-daemon-perl libplrpc-perl root

No Debian/Ubuntu packages are currently available for but it is easy to download the source and install manually.

Download version <id>{[version]}</id> of <backrest/> wget -O - https://github.com/pgmasters/backrest/archive/release/{[version]}.tar.gz | tar zxv -C ~ vagrant Install <backrest/> cp -r ~/backrest-release-{[version]}/lib/BackRest {[perl-lib-path]} root find /usr/lib/perl5/BackRest -type f -exec chmod 644 {} + root find /usr/lib/perl5/BackRest -type d -exec chmod 755 {} + root cp ~/backrest-release-{[version]}/bin/{[backrest-exe]} {[perl-bin-path]}/{[backrest-exe]} root chmod 755 {[perl-bin-path]}/{[backrest-exe]} root

should now be properly installed but it is best to check. If any dependencies were missed then you will get an error when running from the command line.

Make sure the installation worked {[backrest-exe]} vagrant
Quick Start

The Quick Start section will cover basic configuration of and and introduce the backup, restore, and info commands.

Setup Demo Cluster

You'll need to create a demo cluster to run the example commands in this user guide. This step is optional, but you may need to adjust commands in the user guide to work with your environment if you choose not to create the demo cluster.

Create and start the demo cluster pg_createcluster 9.4 {[postgres-cluster-demo]} {[dash]}-start root
Configure Cluster Stanza

Demo describes the purpose of this cluster accurately so that will also make a good stanza name.

needs to know where the base data directory for the cluster is located. The path can be requested from directly but in a recovery scenario the process will not be available. During backups the value supplied to will be compared against the path that is running on and they must be equal or the backup will return an error. Make sure that db-path is exactly equal to data_directory in postgresql.conf.

By default Ubuntu stores clusters in /var/lib/postgresql/[VERSION]/[CLUSTER-NAME] so it is easy to determine the correct path for the data directory.

Configure the <postgres/> cluster data directory demo db-path {[db-path]}
Create the Repository

For this simple configuration the repository will be stored on the same host as the server. This is the simplest configuration and is useful in cases where external backup software is employed to backup the database host.

Create the <backrest/> repository mkdir {[backrest-repo-path]} root chmod 750 {[backrest-repo-path]} root chown postgres:postgres {[backrest-repo-path]} root

The repository path must be configured so knows where to find it.

Configure the <backrest/> repository path global:general repo-path {[backrest-repo-path]}
Configure Archiving

Backing up a running cluster requires WAL archiving to be enabled.

Configure <postgres/> settings '{[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} archive-push %p' on archive

The wal_level setting must be set to archive at a minimum but hot_standby and logical also work fine for backups. Setting wal_level to hot_standy is a good idea even if you do not currently run a hot standby since one can be added later without restarting the primary cluster.

The cluster must be restarted after making these changes and before performing a backup.

Restart the {[postgres-cluster-demo]} cluster pg_ctlcluster 9.4 {[postgres-cluster-demo]} restart
Perform a Backup

To perform a backup of the cluster run with the backup command.

Backup the {[postgres-cluster-demo]} cluster {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} backup

By default will attempt to perform an incremental backup. However, an incremental backup must be based on a full backup and since no full backup existed ran a full backup instead.

The type option can be used to specify and full or differential backup.

Differential backup of the {[postgres-cluster-demo]} cluster {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=diff backup {[dash]}-log-level-console=info

This time there was no warning because a full backup already existed. Like incremental backups, differential backups must be based on a full backup. An incremental backup can be performed by running the backup command with {[dash]}-type=incr.

Use the info command to get information about backups.

Get info for the {[postgres-cluster-demo]} cluster {[backrest-exe]} info (oldest|latest) backup label

The oldest and newest backups are shown in the info output. The oldest backup will always be a full backup (indicated by an F at the end of the label) but the newest backup can be full, differential (ends with D), or incremental (ends with I).

More information about the backup command can be found in the Backup section.

Restore a Backup

Backups can protect you from a number of disaster scenarios, the most common of which are hardware failure and data corruption. The easiest way to simulate data corruption is to remove an important cluster file.

Stop the {[postgres-cluster-demo]} cluster and delete the <file>pg_control</file> file pg_ctlcluster 9.4 {[postgres-cluster-demo]} stop rm {[db-path]}/global/pg_control

Starting the cluster without this important file will result in an error.

Attempt to start the corrupted {[postgres-cluster-demo]} cluster pg_ctlcluster 9.4 {[postgres-cluster-demo]} start 1

To restore a backup of the cluster run with the restore command. The cluster needs to be stopped (in this case it is already stopped) and all files must be removed from the data directory.

Remove old files from {[postgres-cluster-demo]} cluster find {[db-path]} -mindepth 1 -delete Restore the {[postgres-cluster-demo]} cluster and start <postgres/> {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} restore pg_ctlcluster 9.4 {[postgres-cluster-demo]} start

This time the cluster started successfully since the restore replaced the missing pg_control file.

More information about the restore command can be found in the Restore section.

Backup

The Backup section introduces additional backup command features.

Fast Start Option

By default will wait for the next regularly scheduled checkpoint before starting a backup. Depending on the checkpoint_timeout and checkpoint_segments settings in it may be quite some time before a checkpoint completes and the backup can begin.

Incremental backup of the {[postgres-cluster-demo]} cluster with the regularly scheduled checkpoint {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=incr backup {[dash]}-log-level-console=info backup begins after the next regular checkpoint completes

By setting start-fast on the command-line or in {[backrest-config-demo]} an immediate checkpoint is requested and the backup will start more quickly. This is convenient for testing and for ad-hoc backups. For instance, if a backup is being taken at the beginning of a release window it makes no sense to wait for a checkpoint. Since regularly scheduled backups generally only happen once per day it is unlikely that enabling the start-fast in {[backrest-config-demo]} will negatively affect performance, however for high-volume transactional systems you may want to pass {[dash]}-start-fast on the command-line instead.

Enable the <br-option>start-fast</br-option> option global:backup start-fast y Incremental backup of the {[postgres-cluster-demo]} cluster with an immediate checkpoint {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=incr backup {[dash]}-log-level-console=info backup begins after the requested immediate checkpoint completes
Automatic Stop Option

Sometimes will exit unexpectedly and the backup in progress on the cluster will not be properly stopped. exits as quickly as possible when an error occurs so that the cause can be reported accurately and is not masked by another problem that could happen during a more extensive cleanup.

Here an error in intentionally caused by removing repository permissions.

Revoke write privileges in the <backrest/> repository and attempt a backup chmod 550 {[backrest-repo-path]}/temp root {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=incr backup {[dash]}-log-level-console=info ERROR: 199

Even when the permissions are fixed will still be unable to perform a backup because the cluster is stuck in backup mode.

Restore write privileges in the <backrest/> repository and attempt a backup chmod 750 {[backrest-repo-path]}/temp root {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=incr backup {[dash]}-log-level-console=info ERROR: 132

Enabling the stop-auto option allows to stop the current backup if it detects that no other backup process is running.

Enable the <br-option>stop-auto</br-option> option global:backup stop-auto y

Now will stop the old backup and start a new one so the process completes successfully.

Perform an incremental backup {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=incr backup {[dash]}-log-level-console=info cluster is already in backup mode|backup begins after the requested immediate checkpoint completes

Although useful this feature may not be appropriate when another third-party backup solution is being used to take online backups as will not recognize that the other software is running and may terminate a backup started by that software. However, it would be unusual to run more than one third-party backup solution at the same time so this is not likely to be a problem.

Note that pg_dump and pg_base_backup do not take online backups so are not affected. It is safe to run them in conjunction with .

Restore

The Restore section introduces additional restore command features.

Point-in-Time Recovery (PITR)

The restore example in Quick Start performed default recovery, which is to play all the way to the end of the WAL stream. In the case of a hardware failure this is probably the most appropriate action but for data corruption scenarios (whether machine or human in origin) there is a better alternative called Point-in-Time Recovery (PITR).

PITR allows the WAL to be played from the last backup to a specified time, transaction id, or recovery point. For common recovery scenarios time-based recovery is arguably the most useful. A common recovery scenario is to restore a table or data was accidentally dropped or deleted. Recovering a dropped table is more dramatic so that's the example given here but deleted data would be recovered in exactly the same way.

Create table with very important data psql -c "begin; create table important_table (message text); insert into important_table values ('{[test-table-data]}'); commit; select * from important_table"; {[test-table-data]}

It is important to represent the time as reckoned by and to include timezone offsets. This reduces the possibility of unintended timezone conversions and an unexpected recovery result.

Get time from <postgres/> psql -Atc "select current_timestamp" time-recovery-timestamp

Now that the time has been recorded the table is dropped. In practice finding the exact time that the table was dropped is a lot harder than in this example. It may not be possible to find the exact time, but some forensic work should be able to get you close.

Drop the important table psql -c "begin; drop table important_table; commit; select * from important_table;" does not exist 1

Now the restore can be performed with time-based recovery to {[time-recovery-timestamp]} to bring back the missing table.

Stop <postgres/>, restore the {[postgres-cluster-demo]} cluster to <id>{[time-recovery-timestamp]}</id>, and display <file>recovery.conf</file> pg_ctlcluster 9.4 {[postgres-cluster-demo]} stop rm /var/log/postgresql/postgresql-9.4-{[postgres-cluster-demo]}.log {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} --type=time "--target={[time-recovery-timestamp]}" --delta restore cat /var/lib/postgresql/9.4/{[postgres-cluster-demo]}/recovery.conf

The recovery.conf file has been automatically generated by so can be started immediately. Once has finished recovery the table will exist again and can be queried.

Start <postgres/> and check that the important table exists pg_ctlcluster 9.4 {[postgres-cluster-demo]} start psql -c "select * from important_table" {[test-table-data]}

The log also contains valuable information. It will indicate the time and transaction where the recovery stopped and also give the time of the last transaction to be applied.

Examine the <postgres/> log output cat /var/log/postgresql/postgresql-9.4-{[postgres-cluster-demo]}.log recovery stopping before|last completed transaction|starting point-in-time recovery

This example was rigged to give the correct result. If a backup after the required time is chosen then will not be able to recover the lost table. can only play forward, not backward. To demonstrate this the important table must be dropped (again).

Drop the important table (again) psql -c "begin; drop table important_table; commit; select * from important_table;" does not exist 1

Now take a new backup and attempt the recovery from the new backup.

Perform a backup then attempt recovery from that backup {[cmd-backup-last]} backup-last {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} {[dash]}-type=incr backup pg_ctlcluster 9.4 {[postgres-cluster-demo]} stop rm /var/log/postgresql/postgresql-9.4-{[postgres-cluster-demo]}.log {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} --type=time "--target={[time-recovery-timestamp]}" --delta restore pg_ctlcluster 9.4 {[postgres-cluster-demo]} start psql -c "select * from important_table" does not exist 1

Looking at the log output it's not obvious that recovery failed to restore the table. The key is to look for the presence of the "recovery stopping before..." and "last completed transaction..." log messages. If they are not present then the recovery to the specified point-in-time was not successful.

Examine the <postgres/> log output to discover the recovery was not successful cat /var/log/postgresql/postgresql-9.4-{[postgres-cluster-demo]}.log starting point-in-time recovery

Using an earlier backup will allow to play forward to the correct time again. The default behavior for restore is to use the most recent backup but an earlier backup can be specified with the --set option.

Stop <postgres/>, YADA YADA! pg_ctlcluster 9.4 {[postgres-cluster-demo]} stop rm /var/log/postgresql/postgresql-9.4-{[postgres-cluster-demo]}.log {[backrest-exe]} {[dash]}-stanza={[postgres-cluster-demo]} --type=time "--target={[time-recovery-timestamp]}" --set={[backup-last]} --delta restore pg_ctlcluster 9.4 {[postgres-cluster-demo]} start psql -c "select * from important_table"

Now the the log output will contain the expected "recovery stopping before..." and "last completed transaction..." messages showing that the recovery was successful.

Examine the <postgres/> log output for log messages indicating success cat /var/log/postgresql/postgresql-9.4-{[postgres-cluster-demo]}.log recovery stopping before|last completed transaction|starting point-in-time recovery