2010-09-20 22:08:53 +02:00
|
|
|
<!-- doc/src/sgml/backup.sgml -->
|
2006-03-10 20:10:50 +01:00
|
|
|
|
2000-06-30 18:14:21 +02:00
|
|
|
<chapter id="backup">
|
|
|
|
<title>Backup and Restore</title>
|
|
|
|
|
2001-11-12 20:19:39 +01:00
|
|
|
<indexterm zone="backup"><primary>backup</></>
|
|
|
|
|
2000-06-30 18:14:21 +02:00
|
|
|
<para>
|
2004-12-28 20:08:58 +01:00
|
|
|
As with everything that contains valuable data, <productname>PostgreSQL</>
|
2000-06-30 18:14:21 +02:00
|
|
|
databases should be backed up regularly. While the procedure is
|
2007-12-17 05:30:05 +01:00
|
|
|
essentially simple, it is important to have a clear understanding of
|
2000-06-30 18:14:21 +02:00
|
|
|
the underlying techniques and assumptions.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-08-03 22:32:36 +02:00
|
|
|
There are three fundamentally different approaches to backing up
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</> data:
|
2000-06-30 18:14:21 +02:00
|
|
|
<itemizedlist>
|
|
|
|
<listitem><para><acronym>SQL</> dump</para></listitem>
|
|
|
|
<listitem><para>File system level backup</para></listitem>
|
2006-12-01 04:29:15 +01:00
|
|
|
<listitem><para>Continuous archiving</para></listitem>
|
2000-06-30 18:14:21 +02:00
|
|
|
</itemizedlist>
|
2010-08-16 01:04:49 +02:00
|
|
|
Each has its own strengths and weaknesses; each is discussed in turn
|
|
|
|
in the following sections.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="backup-dump">
|
2000-06-30 18:14:21 +02:00
|
|
|
<title><acronym>SQL</> Dump</title>
|
|
|
|
|
|
|
|
<para>
|
2014-08-25 21:34:51 +02:00
|
|
|
The idea behind this dump method is to generate a file with SQL
|
2000-06-30 18:14:21 +02:00
|
|
|
commands that, when fed back to the server, will recreate the
|
|
|
|
database in the same state as it was at the time of the dump.
|
2001-11-21 06:53:41 +01:00
|
|
|
<productname>PostgreSQL</> provides the utility program
|
2004-04-22 09:02:36 +02:00
|
|
|
<xref linkend="app-pgdump"> for this purpose. The basic usage of this
|
2000-06-30 18:14:21 +02:00
|
|
|
command is:
|
|
|
|
<synopsis>
|
|
|
|
pg_dump <replaceable class="parameter">dbname</replaceable> > <replaceable class="parameter">outfile</replaceable>
|
|
|
|
</synopsis>
|
2010-02-03 18:25:06 +01:00
|
|
|
As you see, <application>pg_dump</> writes its result to the
|
2000-06-30 18:14:21 +02:00
|
|
|
standard output. We will see below how this can be useful.
|
2014-08-25 21:34:51 +02:00
|
|
|
While the above command creates a text file, <application>pg_dump</>
|
2015-09-11 03:22:21 +02:00
|
|
|
can create files in other formats that allow for parallelism and more
|
2014-08-25 21:34:51 +02:00
|
|
|
fine-grained control of object restoration.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2001-11-21 06:53:41 +01:00
|
|
|
<application>pg_dump</> is a regular <productname>PostgreSQL</>
|
2000-06-30 18:14:21 +02:00
|
|
|
client application (albeit a particularly clever one). This means
|
2010-02-03 18:25:06 +01:00
|
|
|
that you can perform this backup procedure from any remote host that has
|
2000-06-30 18:14:21 +02:00
|
|
|
access to the database. But remember that <application>pg_dump</>
|
2004-12-28 20:08:58 +01:00
|
|
|
does not operate with special permissions. In particular, it must
|
2013-11-08 21:08:11 +01:00
|
|
|
have read access to all tables that you want to back up, so in order
|
|
|
|
to back up the entire database you almost always have to run it as a
|
2013-11-12 16:23:23 +01:00
|
|
|
database superuser. (If you do not have sufficient privileges to back up
|
|
|
|
the entire database, you can still back up portions of the database to which
|
2013-11-08 21:08:11 +01:00
|
|
|
you do have access using options such as
|
|
|
|
<option>-n <replaceable>schema</replaceable></option>
|
|
|
|
or <option>-t <replaceable>table</replaceable></option>.)
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2000-12-10 21:47:34 +01:00
|
|
|
To specify which database server <application>pg_dump</> should
|
2000-06-30 18:14:21 +02:00
|
|
|
contact, use the command line options <option>-h
|
|
|
|
<replaceable>host</></> and <option>-p <replaceable>port</></>. The
|
|
|
|
default host is the local host or whatever your
|
|
|
|
<envar>PGHOST</envar> environment variable specifies. Similarly,
|
|
|
|
the default port is indicated by the <envar>PGPORT</envar>
|
|
|
|
environment variable or, failing that, by the compiled-in default.
|
|
|
|
(Conveniently, the server will normally have the same compiled-in
|
|
|
|
default.)
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2008-01-23 21:21:37 +01:00
|
|
|
Like any other <productname>PostgreSQL</> client application,
|
2000-06-30 18:14:21 +02:00
|
|
|
<application>pg_dump</> will by default connect with the database
|
2002-11-11 21:14:04 +01:00
|
|
|
user name that is equal to the current operating system user name. To override
|
2001-05-17 23:12:49 +02:00
|
|
|
this, either specify the <option>-U</option> option or set the
|
|
|
|
environment variable <envar>PGUSER</envar>. Remember that
|
|
|
|
<application>pg_dump</> connections are subject to the normal
|
|
|
|
client authentication mechanisms (which are described in <xref
|
2000-06-30 18:14:21 +02:00
|
|
|
linkend="client-authentication">).
|
|
|
|
</para>
|
|
|
|
|
2010-08-16 01:04:49 +02:00
|
|
|
<para>
|
|
|
|
An important advantage of <application>pg_dump</> over the other backup
|
|
|
|
methods described later is that <application>pg_dump</>'s output can
|
|
|
|
generally be re-loaded into newer versions of <productname>PostgreSQL</>,
|
|
|
|
whereas file-level backups and continuous archiving are both extremely
|
|
|
|
server-version-specific. <application>pg_dump</> is also the only method
|
|
|
|
that will work when transferring a database to a different machine
|
|
|
|
architecture, such as going from a 32-bit to a 64-bit server.
|
|
|
|
</para>
|
|
|
|
|
2000-06-30 18:14:21 +02:00
|
|
|
<para>
|
|
|
|
Dumps created by <application>pg_dump</> are internally consistent,
|
2010-02-03 18:25:06 +01:00
|
|
|
meaning, the dump represents a snapshot of the database at the time
|
|
|
|
<application>pg_dump</> began running. <application>pg_dump</> does not
|
2000-06-30 18:14:21 +02:00
|
|
|
block other operations on the database while it is working.
|
|
|
|
(Exceptions are those operations that need to operate with an
|
2007-12-17 05:30:05 +01:00
|
|
|
exclusive lock, such as most forms of <command>ALTER TABLE</command>.)
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
2001-03-19 17:19:26 +01:00
|
|
|
<sect2 id="backup-dump-restore">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Restoring the Dump</title>
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<para>
|
2014-08-25 21:34:51 +02:00
|
|
|
Text files created by <application>pg_dump</> are intended to
|
2000-06-30 18:14:21 +02:00
|
|
|
be read in by the <application>psql</application> program. The
|
|
|
|
general command form to restore a dump is
|
|
|
|
<synopsis>
|
|
|
|
psql <replaceable class="parameter">dbname</replaceable> < <replaceable class="parameter">infile</replaceable>
|
|
|
|
</synopsis>
|
2010-02-03 18:25:06 +01:00
|
|
|
where <replaceable class="parameter">infile</replaceable> is the
|
|
|
|
file output by the <application>pg_dump</> command. The database <replaceable
|
2000-06-30 18:14:21 +02:00
|
|
|
class="parameter">dbname</replaceable> will not be created by this
|
2006-09-19 21:04:51 +02:00
|
|
|
command, so you must create it yourself from <literal>template0</>
|
|
|
|
before executing <application>psql</> (e.g., with
|
|
|
|
<literal>createdb -T template0 <replaceable
|
|
|
|
class="parameter">dbname</></literal>). <application>psql</>
|
2010-02-03 18:25:06 +01:00
|
|
|
supports options similar to <application>pg_dump</> for specifying
|
2006-09-19 21:04:51 +02:00
|
|
|
the database server to connect to and the user name to use. See
|
|
|
|
the <xref linkend="app-psql"> reference page for more information.
|
2014-08-25 21:34:51 +02:00
|
|
|
Non-text file dumps are restored using the <xref
|
|
|
|
linkend="app-pgrestore"> utility.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Before restoring an SQL dump, all the users who own objects or were
|
2006-09-19 21:04:51 +02:00
|
|
|
granted permissions on objects in the dumped database must already
|
2010-02-03 18:25:06 +01:00
|
|
|
exist. If they do not, the restore will fail to recreate the
|
2006-09-19 21:04:51 +02:00
|
|
|
objects with the original ownership and/or permissions.
|
|
|
|
(Sometimes this is what you want, but usually it is not.)
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
2003-03-18 01:02:11 +01:00
|
|
|
<para>
|
2006-09-19 21:04:51 +02:00
|
|
|
By default, the <application>psql</> script will continue to
|
2010-02-03 18:25:06 +01:00
|
|
|
execute after an SQL error is encountered. You might wish to run
|
|
|
|
<application>psql</application> with
|
|
|
|
the <literal>ON_ERROR_STOP</> variable set to alter that
|
2010-02-19 01:15:25 +01:00
|
|
|
behavior and have <application>psql</application> exit with an
|
2006-09-19 21:04:51 +02:00
|
|
|
exit status of 3 if an SQL error occurs:
|
|
|
|
<programlisting>
|
2010-02-03 18:25:06 +01:00
|
|
|
psql --set ON_ERROR_STOP=on dbname < infile
|
2006-09-19 21:04:51 +02:00
|
|
|
</programlisting>
|
2010-02-03 18:25:06 +01:00
|
|
|
Either way, you will only have a partially restored database.
|
2007-12-17 05:30:05 +01:00
|
|
|
Alternatively, you can specify that the whole dump should be
|
2006-09-19 21:04:51 +02:00
|
|
|
restored as a single transaction, so the restore is either fully
|
|
|
|
completed or fully rolled back. This mode can be specified by
|
|
|
|
passing the <option>-1</> or <option>--single-transaction</>
|
|
|
|
command-line options to <application>psql</>. When using this
|
2010-02-03 18:25:06 +01:00
|
|
|
mode, be aware that even a minor error can rollback a
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
restore that has already run for many hours. However, that might
|
2006-09-19 21:04:51 +02:00
|
|
|
still be preferable to manually cleaning up a complex database
|
|
|
|
after a partially restored dump.
|
2003-03-18 01:02:11 +01:00
|
|
|
</para>
|
|
|
|
|
2000-06-30 18:14:21 +02:00
|
|
|
<para>
|
|
|
|
The ability of <application>pg_dump</> and <application>psql</> to
|
2001-11-18 23:27:00 +01:00
|
|
|
write to or read from pipes makes it possible to dump a database
|
2007-12-17 05:30:05 +01:00
|
|
|
directly from one server to another, for example:
|
2000-06-30 18:14:21 +02:00
|
|
|
<programlisting>
|
|
|
|
pg_dump -h <replaceable>host1</> <replaceable>dbname</> | psql -h <replaceable>host2</> <replaceable>dbname</>
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
2001-03-13 15:08:18 +01:00
|
|
|
|
2002-11-11 21:14:04 +01:00
|
|
|
<important>
|
|
|
|
<para>
|
|
|
|
The dumps produced by <application>pg_dump</> are relative to
|
|
|
|
<literal>template0</>. This means that any languages, procedures,
|
2007-12-17 05:30:05 +01:00
|
|
|
etc. added via <literal>template1</> will also be dumped by
|
2002-11-11 21:14:04 +01:00
|
|
|
<application>pg_dump</>. As a result, when restoring, if you are
|
|
|
|
using a customized <literal>template1</>, you must create the
|
|
|
|
empty database from <literal>template0</>, as in the example
|
|
|
|
above.
|
|
|
|
</para>
|
|
|
|
</important>
|
2001-03-13 15:08:18 +01:00
|
|
|
|
2004-04-22 09:02:36 +02:00
|
|
|
<para>
|
2006-09-19 21:04:51 +02:00
|
|
|
After restoring a backup, it is wise to run <xref
|
2010-04-03 09:23:02 +02:00
|
|
|
linkend="sql-analyze"> on each
|
2009-08-07 22:54:31 +02:00
|
|
|
database so the query optimizer has useful statistics;
|
2010-04-03 09:23:02 +02:00
|
|
|
see <xref linkend="vacuum-for-statistics">
|
|
|
|
and <xref linkend="autovacuum"> for more information.
|
2009-08-07 22:54:31 +02:00
|
|
|
For more advice on how to load large amounts of data
|
2006-09-19 21:04:51 +02:00
|
|
|
into <productname>PostgreSQL</> efficiently, refer to <xref
|
2004-04-22 09:02:36 +02:00
|
|
|
linkend="populate">.
|
|
|
|
</para>
|
2000-06-30 18:14:21 +02:00
|
|
|
</sect2>
|
|
|
|
|
2001-03-19 17:19:26 +01:00
|
|
|
<sect2 id="backup-dump-all">
|
2004-01-11 06:46:58 +01:00
|
|
|
<title>Using <application>pg_dumpall</></title>
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<para>
|
2006-12-01 04:29:15 +01:00
|
|
|
<application>pg_dump</> dumps only a single database at a time,
|
|
|
|
and it does not dump information about roles or tablespaces
|
|
|
|
(because those are cluster-wide rather than per-database).
|
|
|
|
To support convenient dumping of the entire contents of a database
|
|
|
|
cluster, the <xref linkend="app-pg-dumpall"> program is provided.
|
2000-06-30 18:14:21 +02:00
|
|
|
<application>pg_dumpall</> backs up each database in a given
|
2006-12-01 04:29:15 +01:00
|
|
|
cluster, and also preserves cluster-wide data such as role and
|
|
|
|
tablespace definitions. The basic usage of this command is:
|
2000-06-30 18:14:21 +02:00
|
|
|
<synopsis>
|
|
|
|
pg_dumpall > <replaceable>outfile</>
|
|
|
|
</synopsis>
|
2003-08-01 03:01:52 +02:00
|
|
|
The resulting dump can be restored with <application>psql</>:
|
|
|
|
<synopsis>
|
2005-06-21 06:02:34 +02:00
|
|
|
psql -f <replaceable class="parameter">infile</replaceable> postgres
|
2003-08-01 03:01:52 +02:00
|
|
|
</synopsis>
|
|
|
|
(Actually, you can specify any existing database name to start from,
|
2010-02-03 18:25:06 +01:00
|
|
|
but if you are loading into an empty cluster then <literal>postgres</>
|
2007-12-17 05:30:05 +01:00
|
|
|
should usually be used.) It is always necessary to have
|
2003-08-01 03:01:52 +02:00
|
|
|
database superuser access when restoring a <application>pg_dumpall</>
|
2006-12-01 04:29:15 +01:00
|
|
|
dump, as that is required to restore the role and tablespace information.
|
2010-02-03 18:25:06 +01:00
|
|
|
If you use tablespaces, make sure that the tablespace paths in the
|
2006-12-01 04:29:15 +01:00
|
|
|
dump are appropriate for the new installation.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
2007-12-17 05:30:05 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
<application>pg_dumpall</> works by emitting commands to re-create
|
|
|
|
roles, tablespaces, and empty databases, then invoking
|
|
|
|
<application>pg_dump</> for each database. This means that while
|
|
|
|
each database will be internally consistent, the snapshots of
|
2015-09-11 03:22:21 +02:00
|
|
|
different databases are not synchronized.
|
2014-08-25 21:34:51 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Cluster-wide data can be dumped alone using the
|
|
|
|
<application>pg_dumpall</> <option>--globals-only</> option.
|
|
|
|
This is necessary to fully backup the cluster if running the
|
|
|
|
<application>pg_dump</> command on individual databases.
|
2007-12-17 05:30:05 +01:00
|
|
|
</para>
|
2000-06-30 18:14:21 +02:00
|
|
|
</sect2>
|
|
|
|
|
2001-03-19 17:19:26 +01:00
|
|
|
<sect2 id="backup-dump-large">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Handling Large Databases</title>
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Some operating systems have maximum file size limits that cause
|
|
|
|
problems when creating large <application>pg_dump</> output files.
|
|
|
|
Fortunately, <application>pg_dump</> can write to the standard
|
|
|
|
output, so you can use standard Unix tools to work around this
|
|
|
|
potential problem. There are several possible methods:
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<formalpara>
|
|
|
|
<title>Use compressed dumps.</title>
|
|
|
|
<para>
|
2003-03-24 15:32:51 +01:00
|
|
|
You can use your favorite compression program, for example
|
2007-02-01 01:28:19 +01:00
|
|
|
<application>gzip</application>:
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
pg_dump <replaceable class="parameter">dbname</replaceable> | gzip > <replaceable class="parameter">filename</replaceable>.gz
|
|
|
|
</programlisting>
|
|
|
|
|
2007-02-01 01:28:19 +01:00
|
|
|
Reload with:
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
gunzip -c <replaceable class="parameter">filename</replaceable>.gz | psql <replaceable class="parameter">dbname</replaceable>
|
|
|
|
</programlisting>
|
|
|
|
|
2007-02-01 01:28:19 +01:00
|
|
|
or:
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
cat <replaceable class="parameter">filename</replaceable>.gz | gunzip | psql <replaceable class="parameter">dbname</replaceable>
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</formalpara>
|
|
|
|
|
|
|
|
<formalpara>
|
2003-03-24 15:32:51 +01:00
|
|
|
<title>Use <command>split</>.</title>
|
2000-06-30 18:14:21 +02:00
|
|
|
<para>
|
2003-03-24 15:32:51 +01:00
|
|
|
The <command>split</command> command
|
2010-02-03 18:25:06 +01:00
|
|
|
allows you to split the output into smaller files that are
|
2000-06-30 18:14:21 +02:00
|
|
|
acceptable in size to the underlying file system. For example, to
|
|
|
|
make chunks of 1 megabyte:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
pg_dump <replaceable class="parameter">dbname</replaceable> | split -b 1m - <replaceable class="parameter">filename</replaceable>
|
|
|
|
</programlisting>
|
|
|
|
|
2007-02-01 01:28:19 +01:00
|
|
|
Reload with:
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<programlisting>
|
2001-09-10 09:11:28 +02:00
|
|
|
cat <replaceable class="parameter">filename</replaceable>* | psql <replaceable class="parameter">dbname</replaceable>
|
2000-06-30 18:14:21 +02:00
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
</formalpara>
|
|
|
|
|
2001-03-13 15:08:18 +01:00
|
|
|
<formalpara>
|
2007-12-17 05:30:05 +01:00
|
|
|
<title>Use <application>pg_dump</>'s custom dump format.</title>
|
2001-03-13 15:08:18 +01:00
|
|
|
<para>
|
2004-12-28 20:08:58 +01:00
|
|
|
If <productname>PostgreSQL</productname> was built on a system with the
|
|
|
|
<application>zlib</> compression library installed, the custom dump
|
|
|
|
format will compress data as it writes it to the output file. This will
|
|
|
|
produce dump file sizes similar to using <command>gzip</command>, but it
|
|
|
|
has the added advantage that tables can be restored selectively. The
|
|
|
|
following command dumps a database using the custom dump format:
|
2001-03-13 15:08:18 +01:00
|
|
|
|
|
|
|
<programlisting>
|
2005-01-22 23:56:36 +01:00
|
|
|
pg_dump -Fc <replaceable class="parameter">dbname</replaceable> > <replaceable class="parameter">filename</replaceable>
|
2001-03-13 15:08:18 +01:00
|
|
|
</programlisting>
|
|
|
|
|
2004-12-28 20:08:58 +01:00
|
|
|
A custom-format dump is not a script for <application>psql</>, but
|
2007-12-17 05:30:05 +01:00
|
|
|
instead must be restored with <application>pg_restore</>, for example:
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
pg_restore -d <replaceable class="parameter">dbname</replaceable> <replaceable class="parameter">filename</replaceable>
|
|
|
|
</programlisting>
|
|
|
|
|
2004-02-17 10:07:16 +01:00
|
|
|
See the <xref linkend="app-pgdump"> and <xref
|
|
|
|
linkend="app-pgrestore"> reference pages for details.
|
2001-03-13 15:08:18 +01:00
|
|
|
</para>
|
|
|
|
</formalpara>
|
|
|
|
|
2007-12-17 05:30:05 +01:00
|
|
|
<para>
|
|
|
|
For very large databases, you might need to combine <command>split</>
|
|
|
|
with one of the other two approaches.
|
|
|
|
</para>
|
|
|
|
|
2013-03-24 16:27:20 +01:00
|
|
|
<formalpara>
|
|
|
|
<title>Use <application>pg_dump</>'s parallel dump feature.</title>
|
|
|
|
<para>
|
|
|
|
To speed up the dump of a large database, you can use
|
|
|
|
<application>pg_dump</application>'s parallel mode. This will dump
|
|
|
|
multiple tables at the same time. You can control the degree of
|
|
|
|
parallelism with the <command>-j</command> parameter. Parallel dumps
|
|
|
|
are only supported for the "directory" archive format.
|
|
|
|
|
|
|
|
<programlisting>
|
|
|
|
pg_dump -j <replaceable class="parameter">num</replaceable> -F d -f <replaceable class="parameter">out.dir</replaceable> <replaceable class="parameter">dbname</replaceable>
|
|
|
|
</programlisting>
|
|
|
|
|
|
|
|
You can use <command>pg_restore -j</command> to restore a dump in parallel.
|
|
|
|
This will work for any archive of either the "custom" or the "directory"
|
|
|
|
archive mode, whether or not it has been created with <command>pg_dump -j</command>.
|
|
|
|
</para>
|
|
|
|
</formalpara>
|
2000-06-30 18:14:21 +02:00
|
|
|
</sect2>
|
|
|
|
</sect1>
|
|
|
|
|
2000-09-29 22:21:34 +02:00
|
|
|
<sect1 id="backup-file">
|
2006-12-01 04:29:15 +01:00
|
|
|
<title>File System Level Backup</title>
|
2000-06-30 18:14:21 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
An alternative backup strategy is to directly copy the files that
|
2010-02-03 18:25:06 +01:00
|
|
|
<productname>PostgreSQL</> uses to store the data in the database;
|
|
|
|
<xref linkend="creating-cluster"> explains where these files
|
|
|
|
are located. You can use whatever method you prefer
|
|
|
|
for doing file system backups; for example:
|
2002-11-11 21:14:04 +01:00
|
|
|
|
2000-06-30 18:14:21 +02:00
|
|
|
<programlisting>
|
|
|
|
tar -cf backup.tar /usr/local/pgsql/data
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
There are two restrictions, however, which make this method
|
|
|
|
impractical, or at least inferior to the <application>pg_dump</>
|
|
|
|
method:
|
|
|
|
|
|
|
|
<orderedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
The database server <emphasis>must</> be shut down in order to
|
|
|
|
get a usable backup. Half-way measures such as disallowing all
|
2004-04-22 09:02:36 +02:00
|
|
|
connections will <emphasis>not</emphasis> work
|
2007-12-17 05:30:05 +01:00
|
|
|
(in part because <command>tar</command> and similar tools do not take
|
|
|
|
an atomic snapshot of the state of the file system,
|
|
|
|
but also because of internal buffering within the server).
|
|
|
|
Information about stopping the server can be found in
|
2010-02-03 18:25:06 +01:00
|
|
|
<xref linkend="server-shutdown">. Needless to say, you
|
2004-04-22 09:02:36 +02:00
|
|
|
also need to shut down the server before restoring the data.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-01-19 21:12:30 +01:00
|
|
|
If you have dug into the details of the file system layout of the
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
database, you might be tempted to try to back up or restore only certain
|
2000-06-30 18:14:21 +02:00
|
|
|
individual tables or databases from their respective files or
|
|
|
|
directories. This will <emphasis>not</> work because the
|
2010-02-03 18:25:06 +01:00
|
|
|
information contained in these files is not usable without
|
|
|
|
the commit log files,
|
2001-08-25 20:52:43 +02:00
|
|
|
<filename>pg_clog/*</filename>, which contain the commit status of
|
2000-06-30 18:14:21 +02:00
|
|
|
all transactions. A table file is only usable with this
|
|
|
|
information. Of course it is also impossible to restore only a
|
2001-08-25 20:52:43 +02:00
|
|
|
table and the associated <filename>pg_clog</filename> data
|
2003-03-24 15:32:51 +01:00
|
|
|
because that would render all other tables in the database
|
2004-08-03 22:32:36 +02:00
|
|
|
cluster useless. So file system backups only work for complete
|
2007-12-17 05:30:05 +01:00
|
|
|
backup and restoration of an entire database cluster.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</orderedlist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2003-11-04 10:55:39 +01:00
|
|
|
An alternative file-system backup approach is to make a
|
|
|
|
<quote>consistent snapshot</quote> of the data directory, if the
|
2004-01-19 21:12:30 +01:00
|
|
|
file system supports that functionality (and you are willing to
|
|
|
|
trust that it is implemented correctly). The typical procedure is
|
|
|
|
to make a <quote>frozen snapshot</> of the volume containing the
|
|
|
|
database, then copy the whole data directory (not just parts, see
|
|
|
|
above) from the snapshot to a backup device, then release the frozen
|
|
|
|
snapshot. This will work even while the database server is running.
|
|
|
|
However, a backup created in this way saves
|
2010-02-03 18:25:06 +01:00
|
|
|
the database files in a state as if the database server was not
|
2003-11-04 10:55:39 +01:00
|
|
|
properly shut down; therefore, when you start the database server
|
2010-02-03 18:25:06 +01:00
|
|
|
on the backed-up data, it will think the previous server instance
|
|
|
|
crashed and will replay the WAL log. This is not a problem; just
|
|
|
|
be aware of it (and be sure to include the WAL files in your backup).
|
2010-11-25 00:41:28 +01:00
|
|
|
You can perform a <command>CHECKPOINT</command> before taking the
|
|
|
|
snapshot to reduce recovery time.
|
2003-11-04 10:55:39 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
If your database is spread across multiple file systems, there might not
|
|
|
|
be any way to obtain exactly-simultaneous frozen snapshots of all
|
2005-03-23 20:38:53 +01:00
|
|
|
the volumes. For example, if your data files and WAL log are on different
|
|
|
|
disks, or if tablespaces are on different file systems, it might
|
2007-12-17 05:30:05 +01:00
|
|
|
not be possible to use snapshot backup because the snapshots
|
|
|
|
<emphasis>must</> be simultaneous.
|
2004-12-13 19:05:10 +01:00
|
|
|
Read your file system documentation very carefully before trusting
|
2010-02-03 18:25:06 +01:00
|
|
|
the consistent-snapshot technique in such situations.
|
2008-04-09 04:52:04 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If simultaneous snapshots are not possible, one option is to shut down
|
|
|
|
the database server long enough to establish all the frozen snapshots.
|
2011-05-07 20:31:00 +02:00
|
|
|
Another option is to perform a continuous archiving base backup (<xref
|
2008-04-09 04:52:04 +02:00
|
|
|
linkend="backup-base-backup">) because such backups are immune to file
|
|
|
|
system changes during the backup. This requires enabling continuous
|
|
|
|
archiving just during the backup process; restore is done using
|
|
|
|
continuous archive recovery (<xref linkend="backup-pitr-recovery">).
|
2004-01-19 21:12:30 +01:00
|
|
|
</para>
|
|
|
|
|
2005-03-17 06:03:37 +01:00
|
|
|
<para>
|
|
|
|
Another option is to use <application>rsync</> to perform a file
|
2005-03-17 16:38:46 +01:00
|
|
|
system backup. This is done by first running <application>rsync</>
|
|
|
|
while the database server is running, then shutting down the database
|
2015-03-18 20:48:59 +01:00
|
|
|
server long enough to do an <command>rsync --checksum</>.
|
|
|
|
(<option>--checksum</> is necessary because <command>rsync</> only
|
|
|
|
has file modification-time granularity of one second.) The
|
|
|
|
second <application>rsync</> will be quicker than the first,
|
2005-03-23 20:38:53 +01:00
|
|
|
because it has relatively little data to transfer, and the end result
|
|
|
|
will be consistent because the server was down. This method
|
2005-03-17 16:38:46 +01:00
|
|
|
allows a file system backup to be performed with minimal downtime.
|
2005-03-17 06:03:37 +01:00
|
|
|
</para>
|
|
|
|
|
2004-01-19 21:12:30 +01:00
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Note that a file system backup will typically be larger
|
|
|
|
than an SQL dump. (<application>pg_dump</application> does not need to dump
|
2001-05-17 23:50:18 +02:00
|
|
|
the contents of indexes for example, just the commands to recreate
|
2008-01-23 21:21:37 +01:00
|
|
|
them.) However, taking a file system backup might be faster.
|
2000-06-30 18:14:21 +02:00
|
|
|
</para>
|
2004-08-03 22:32:36 +02:00
|
|
|
</sect1>
|
|
|
|
|
2006-03-03 23:02:08 +01:00
|
|
|
<sect1 id="continuous-archiving">
|
2011-02-01 23:00:26 +01:00
|
|
|
<title>Continuous Archiving and Point-in-Time Recovery (PITR)</title>
|
2004-08-03 22:32:36 +02:00
|
|
|
|
2004-08-04 01:42:59 +02:00
|
|
|
<indexterm zone="backup">
|
2006-03-03 23:02:08 +01:00
|
|
|
<primary>continuous archiving</primary>
|
2004-08-04 01:42:59 +02:00
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<indexterm zone="backup">
|
|
|
|
<primary>point-in-time recovery</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
2004-11-14 07:10:12 +01:00
|
|
|
<indexterm zone="backup">
|
|
|
|
<primary>PITR</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
<para>
|
2004-11-14 07:10:12 +01:00
|
|
|
At all times, <productname>PostgreSQL</> maintains a
|
|
|
|
<firstterm>write ahead log</> (WAL) in the <filename>pg_xlog/</>
|
2010-02-03 18:25:06 +01:00
|
|
|
subdirectory of the cluster's data directory. The log records
|
2004-11-14 07:10:12 +01:00
|
|
|
every change made to the database's data files. This log exists
|
|
|
|
primarily for crash-safety purposes: if the system crashes, the
|
|
|
|
database can be restored to consistency by <quote>replaying</> the
|
|
|
|
log entries made since the last checkpoint. However, the existence
|
2004-08-03 22:32:36 +02:00
|
|
|
of the log makes it possible to use a third strategy for backing up
|
2004-12-13 19:05:10 +01:00
|
|
|
databases: we can combine a file-system-level backup with backup of
|
2010-02-03 18:25:06 +01:00
|
|
|
the WAL files. If recovery is needed, we restore the file system backup and
|
|
|
|
then replay from the backed-up WAL files to bring the system to a
|
|
|
|
current state. This approach is more complex to administer than
|
2004-11-14 07:10:12 +01:00
|
|
|
either of the previous approaches, but it has some significant
|
|
|
|
benefits:
|
2004-08-03 22:32:36 +02:00
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
We do not need a perfectly consistent file system backup as the starting point.
|
2004-08-03 22:32:36 +02:00
|
|
|
Any internal inconsistency in the backup will be corrected by log
|
|
|
|
replay (this is not significantly different from what happens during
|
2010-02-03 18:25:06 +01:00
|
|
|
crash recovery). So we do not need a file system snapshot capability,
|
2004-08-03 22:32:36 +02:00
|
|
|
just <application>tar</> or a similar archiving tool.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Since we can combine an indefinitely long sequence of WAL files
|
2004-11-14 07:10:12 +01:00
|
|
|
for replay, continuous backup can be achieved simply by continuing to archive
|
2004-08-03 22:32:36 +02:00
|
|
|
the WAL files. This is particularly valuable for large databases, where
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
it might not be convenient to take a full backup frequently.
|
2004-08-03 22:32:36 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
It is not necessary to replay the WAL entries all the
|
2004-08-03 22:32:36 +02:00
|
|
|
way to the end. We could stop the replay at any point and have a
|
|
|
|
consistent snapshot of the database as it was at that time. Thus,
|
|
|
|
this technique supports <firstterm>point-in-time recovery</>: it is
|
|
|
|
possible to restore the database to its state at any time since your base
|
|
|
|
backup was taken.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-11-14 07:10:12 +01:00
|
|
|
If we continuously feed the series of WAL files to another
|
|
|
|
machine that has been loaded with the same base backup file, we
|
2006-12-01 04:29:15 +01:00
|
|
|
have a <firstterm>warm standby</> system: at any point we can bring up
|
2004-11-14 07:10:12 +01:00
|
|
|
the second machine and it will have a nearly-current copy of the
|
|
|
|
database.
|
2004-08-03 22:32:36 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
|
|
|
|
2010-02-22 18:15:10 +01:00
|
|
|
<note>
|
|
|
|
<para>
|
|
|
|
<application>pg_dump</application> and
|
|
|
|
<application>pg_dumpall</application> do not produce file-system-level
|
|
|
|
backups and cannot be used as part of a continuous-archiving solution.
|
|
|
|
Such dumps are <emphasis>logical</> and do not contain enough
|
2010-08-16 01:04:49 +02:00
|
|
|
information to be used by WAL replay.
|
2010-02-22 18:15:10 +01:00
|
|
|
</para>
|
|
|
|
</note>
|
|
|
|
|
2004-08-03 22:32:36 +02:00
|
|
|
<para>
|
2004-12-13 19:05:10 +01:00
|
|
|
As with the plain file-system-backup technique, this method can only
|
2004-08-03 22:32:36 +02:00
|
|
|
support restoration of an entire database cluster, not a subset.
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
Also, it requires a lot of archival storage: the base backup might be bulky,
|
2004-08-03 22:32:36 +02:00
|
|
|
and a busy system will generate many megabytes of WAL traffic that
|
|
|
|
have to be archived. Still, it is the preferred backup technique in
|
|
|
|
many situations where high reliability is needed.
|
|
|
|
</para>
|
2000-06-30 18:14:21 +02:00
|
|
|
|
2004-08-04 01:42:59 +02:00
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
To recover successfully using continuous archiving (also called
|
|
|
|
<quote>online backup</> by many database vendors), you need a continuous
|
2004-08-04 01:42:59 +02:00
|
|
|
sequence of archived WAL files that extends back at least as far as the
|
2007-12-17 05:30:05 +01:00
|
|
|
start time of your backup. So to get started, you should set up and test
|
2004-08-04 01:42:59 +02:00
|
|
|
your procedure for archiving WAL files <emphasis>before</> you take your
|
|
|
|
first base backup. Accordingly, we first discuss the mechanics of
|
|
|
|
archiving WAL files.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect2 id="backup-archiving-wal">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Setting Up WAL Archiving</title>
|
2004-08-04 01:42:59 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
In an abstract sense, a running <productname>PostgreSQL</> system
|
|
|
|
produces an indefinitely long sequence of WAL records. The system
|
2004-11-14 07:10:12 +01:00
|
|
|
physically divides this sequence into WAL <firstterm>segment
|
2007-12-17 05:30:05 +01:00
|
|
|
files</>, which are normally 16MB apiece (although the segment size
|
|
|
|
can be altered when building <productname>PostgreSQL</>). The segment
|
2004-11-14 07:10:12 +01:00
|
|
|
files are given numeric names that reflect their position in the
|
|
|
|
abstract WAL sequence. When not using WAL archiving, the system
|
|
|
|
normally creates just a few segment files and then
|
|
|
|
<quote>recycles</> them by renaming no-longer-needed segment files
|
2010-02-03 18:25:06 +01:00
|
|
|
to higher segment numbers. It's assumed that segment files whose
|
|
|
|
contents precede the checkpoint-before-last are no longer of
|
2004-11-14 07:10:12 +01:00
|
|
|
interest and can be recycled.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
When archiving WAL data, we need to capture the contents of each segment
|
2004-08-04 01:42:59 +02:00
|
|
|
file once it is filled, and save that data somewhere before the segment
|
|
|
|
file is recycled for reuse. Depending on the application and the
|
|
|
|
available hardware, there could be many different ways of <quote>saving
|
|
|
|
the data somewhere</>: we could copy the segment files to an NFS-mounted
|
2004-11-14 07:10:12 +01:00
|
|
|
directory on another machine, write them onto a tape drive (ensuring that
|
2006-12-01 04:29:15 +01:00
|
|
|
you have a way of identifying the original name of each file), or batch
|
2004-08-04 01:42:59 +02:00
|
|
|
them together and burn them onto CDs, or something else entirely. To
|
2010-02-03 18:25:06 +01:00
|
|
|
provide the database administrator with flexibility,
|
2007-12-17 05:30:05 +01:00
|
|
|
<productname>PostgreSQL</> tries not to make any assumptions about how
|
2004-08-04 01:42:59 +02:00
|
|
|
the archiving will be done. Instead, <productname>PostgreSQL</> lets
|
|
|
|
the administrator specify a shell command to be executed to copy a
|
|
|
|
completed segment file to wherever it needs to go. The command could be
|
2006-08-18 01:04:10 +02:00
|
|
|
as simple as a <literal>cp</>, or it could invoke a complex shell
|
2004-11-15 07:32:15 +01:00
|
|
|
script — it's all up to you.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-05-03 11:14:17 +02:00
|
|
|
To enable WAL archiving, set the <xref linkend="guc-wal-level">
|
2016-03-01 02:01:54 +01:00
|
|
|
configuration parameter to <literal>replica</> or higher,
|
2010-05-03 11:14:17 +02:00
|
|
|
<xref linkend="guc-archive-mode"> to <literal>on</>,
|
2007-09-27 00:36:30 +02:00
|
|
|
and specify the shell command to use in the <xref
|
|
|
|
linkend="guc-archive-command"> configuration parameter. In practice
|
|
|
|
these settings will always be placed in the
|
|
|
|
<filename>postgresql.conf</filename> file.
|
|
|
|
In <varname>archive_command</>,
|
2010-02-03 18:25:06 +01:00
|
|
|
<literal>%p</> is replaced by the path name of the file to
|
|
|
|
archive, while <literal>%f</> is replaced by only the file name.
|
2007-12-17 05:30:05 +01:00
|
|
|
(The path name is relative to the current working directory,
|
2006-11-04 19:20:27 +01:00
|
|
|
i.e., the cluster's data directory.)
|
2010-02-03 18:25:06 +01:00
|
|
|
Use <literal>%%</> if you need to embed an actual <literal>%</>
|
2004-08-04 01:42:59 +02:00
|
|
|
character in the command. The simplest useful command is something
|
2007-02-01 01:28:19 +01:00
|
|
|
like:
|
2004-08-04 01:42:59 +02:00
|
|
|
<programlisting>
|
2011-06-20 22:27:32 +02:00
|
|
|
archive_command = 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' # Unix
|
2010-09-13 03:35:46 +02:00
|
|
|
archive_command = 'copy "%p" "C:\\server\\archivedir\\%f"' # Windows
|
2004-08-04 01:42:59 +02:00
|
|
|
</programlisting>
|
|
|
|
which will copy archivable WAL segments to the directory
|
2007-12-17 05:30:05 +01:00
|
|
|
<filename>/mnt/server/archivedir</>. (This is an example, not a
|
2008-01-23 21:21:37 +01:00
|
|
|
recommendation, and might not work on all platforms.) After the
|
|
|
|
<literal>%p</> and <literal>%f</> parameters have been replaced,
|
|
|
|
the actual command executed might look like this:
|
|
|
|
<programlisting>
|
2011-06-18 01:13:03 +02:00
|
|
|
test ! -f /mnt/server/archivedir/00000001000000A900000065 && cp pg_xlog/00000001000000A900000065 /mnt/server/archivedir/00000001000000A900000065
|
2008-01-23 21:21:37 +01:00
|
|
|
</programlisting>
|
|
|
|
A similar command will be generated for each new file to be archived.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The archive command will be executed under the ownership of the same
|
|
|
|
user that the <productname>PostgreSQL</> server is running as. Since
|
|
|
|
the series of WAL files being archived contains effectively everything
|
|
|
|
in your database, you will want to be sure that the archived data is
|
|
|
|
protected from prying eyes; for example, archive into a directory that
|
|
|
|
does not have group or world read access.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
It is important that the archive command return zero exit status if and
|
2010-02-03 18:25:06 +01:00
|
|
|
only if it succeeds. Upon getting a zero result,
|
2008-03-28 16:00:28 +01:00
|
|
|
<productname>PostgreSQL</> will assume that the file has been
|
|
|
|
successfully archived, and will remove or recycle it. However, a nonzero
|
|
|
|
status tells <productname>PostgreSQL</> that the file was not archived;
|
|
|
|
it will try again periodically until it succeeds.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
2004-11-08 19:01:28 +01:00
|
|
|
<para>
|
|
|
|
The archive command should generally be designed to refuse to overwrite
|
|
|
|
any pre-existing archive file. This is an important safety feature to
|
|
|
|
preserve the integrity of your archive in case of administrator error
|
|
|
|
(such as sending the output of two different servers to the same archive
|
|
|
|
directory).
|
2011-06-18 01:13:03 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2004-11-08 19:01:28 +01:00
|
|
|
It is advisable to test your proposed archive command to ensure that it
|
|
|
|
indeed does not overwrite an existing file, <emphasis>and that it returns
|
2011-06-18 01:13:03 +02:00
|
|
|
nonzero status in this case</>.
|
|
|
|
The example command above for Unix ensures this by including a separate
|
|
|
|
<command>test</> step. On some Unix platforms, <command>cp</> has
|
|
|
|
switches such as <option>-i</> that can be used to do the same thing
|
|
|
|
less verbosely, but you should not rely on these without verifying that
|
|
|
|
the right exit status is returned. (In particular, GNU <command>cp</>
|
|
|
|
will return status zero when <option>-i</> is used and the target file
|
|
|
|
already exists, which is <emphasis>not</> the desired behavior.)
|
2004-11-08 19:01:28 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
While designing your archiving setup, consider what will happen if
|
2007-12-17 05:30:05 +01:00
|
|
|
the archive command fails repeatedly because some aspect requires
|
2004-11-08 19:01:28 +01:00
|
|
|
operator intervention or the archive runs out of space. For example, this
|
2007-12-17 05:30:05 +01:00
|
|
|
could occur if you write to tape without an autochanger; when the tape
|
2004-11-08 19:01:28 +01:00
|
|
|
fills, nothing further can be archived until the tape is swapped.
|
|
|
|
You should ensure that any error condition or request to a human operator
|
2007-12-17 05:30:05 +01:00
|
|
|
is reported appropriately so that the situation can be
|
|
|
|
resolved reasonably quickly. The <filename>pg_xlog/</> directory will
|
2004-11-08 19:01:28 +01:00
|
|
|
continue to fill with WAL segment files until the situation is resolved.
|
2010-02-03 18:25:06 +01:00
|
|
|
(If the file system containing <filename>pg_xlog/</> fills up,
|
|
|
|
<productname>PostgreSQL</> will do a PANIC shutdown. No committed
|
|
|
|
transactions will be lost, but the database will remain offline until
|
2007-12-17 05:30:05 +01:00
|
|
|
you free some space.)
|
2004-11-08 19:01:28 +01:00
|
|
|
</para>
|
|
|
|
|
2004-08-04 01:42:59 +02:00
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
The speed of the archiving command is unimportant as long as it can keep up
|
2004-11-08 19:01:28 +01:00
|
|
|
with the average rate at which your server generates WAL data. Normal
|
|
|
|
operation continues even if the archiving process falls a little behind.
|
|
|
|
If archiving falls significantly behind, this will increase the amount of
|
|
|
|
data that would be lost in the event of a disaster. It will also mean that
|
|
|
|
the <filename>pg_xlog/</> directory will contain large numbers of
|
|
|
|
not-yet-archived segment files, which could eventually exceed available
|
|
|
|
disk space. You are advised to monitor the archiving process to ensure that
|
|
|
|
it is working as you intend.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2005-11-05 00:14:02 +01:00
|
|
|
In writing your archive command, you should assume that the file names to
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
be archived can be up to 64 characters long and can contain any
|
2004-08-04 01:42:59 +02:00
|
|
|
combination of ASCII letters, digits, and dots. It is not necessary to
|
2010-02-03 18:25:06 +01:00
|
|
|
preserve the original relative path (<literal>%p</>) but it is necessary to
|
|
|
|
preserve the file name (<literal>%f</>).
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
2004-11-14 07:10:12 +01:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Note that although WAL archiving will allow you to restore any
|
2006-12-01 04:29:15 +01:00
|
|
|
modifications made to the data in your <productname>PostgreSQL</> database,
|
2004-11-14 07:10:12 +01:00
|
|
|
it will not restore changes made to configuration files (that is,
|
2004-11-17 19:29:02 +01:00
|
|
|
<filename>postgresql.conf</>, <filename>pg_hba.conf</> and
|
2004-12-28 20:08:58 +01:00
|
|
|
<filename>pg_ident.conf</>), since those are edited manually rather
|
|
|
|
than through SQL operations.
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
You might wish to keep the configuration files in a location that will
|
2004-12-28 20:08:58 +01:00
|
|
|
be backed up by your regular file system backup procedures. See
|
|
|
|
<xref linkend="runtime-config-file-locations"> for how to relocate the
|
|
|
|
configuration files.
|
2004-11-14 07:10:12 +01:00
|
|
|
</para>
|
2006-08-18 01:04:10 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
The archive command is only invoked on completed WAL segments. Hence,
|
2007-12-17 05:30:05 +01:00
|
|
|
if your server generates only little WAL traffic (or has slack periods
|
2006-08-18 01:04:10 +02:00
|
|
|
where it does so), there could be a long delay between the completion
|
|
|
|
of a transaction and its safe recording in archive storage. To put
|
|
|
|
a limit on how old unarchived data can be, you can set
|
|
|
|
<xref linkend="guc-archive-timeout"> to force the server to switch
|
|
|
|
to a new WAL segment file at least that often. Note that archived
|
2010-02-03 18:25:06 +01:00
|
|
|
files that are archived early due to a forced switch are still the same
|
2006-08-18 01:04:10 +02:00
|
|
|
length as completely full files. It is therefore unwise to set a very
|
|
|
|
short <varname>archive_timeout</> — it will bloat your archive
|
|
|
|
storage. <varname>archive_timeout</> settings of a minute or so are
|
|
|
|
usually reasonable.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Also, you can force a segment switch manually with
|
2010-02-03 18:25:06 +01:00
|
|
|
<function>pg_switch_xlog</> if you want to ensure that a
|
2007-11-28 23:35:54 +01:00
|
|
|
just-finished transaction is archived as soon as possible. Other utility
|
2006-09-19 17:18:41 +02:00
|
|
|
functions related to WAL management are listed in <xref
|
|
|
|
linkend="functions-admin-backup-table">.
|
2006-08-18 01:04:10 +02:00
|
|
|
</para>
|
2007-11-28 23:35:54 +01:00
|
|
|
|
|
|
|
<para>
|
Introduce wal_level GUC to explicitly control if information needed for
archival or hot standby should be WAL-logged, instead of deducing that from
other options like archive_mode. This replaces recovery_connections GUC in
the primary, where it now has no effect, but it's still used in the standby
to enable/disable hot standby.
Remove the WAL-logging of "unlogged operations", like creating an index
without WAL-logging and fsyncing it at the end. Instead, we keep a copy of
the wal_mode setting and the settings that affect how much shared memory a
hot standby server needs to track master transactions (max_connections,
max_prepared_xacts, max_locks_per_xact) in pg_control. Whenever the settings
change, at server restart, write a WAL record noting the new settings and
update pg_control. This allows us to notice the change in those settings in
the standby at the right moment, they used to be included in checkpoint
records, but that meant that a changed value was not reflected in the
standby until the first checkpoint after the change.
Bump PG_CONTROL_VERSION and XLOG_PAGE_MAGIC. Whack XLOG_PAGE_MAGIC back to
the sequence it used to follow, before hot standby and subsequent patches
changed it to 0x9003.
2010-04-28 18:10:43 +02:00
|
|
|
When <varname>wal_level</> is <literal>minimal</> some SQL commands
|
2007-11-28 23:35:54 +01:00
|
|
|
are optimized to avoid WAL logging, as described in <xref
|
2010-04-20 02:26:06 +02:00
|
|
|
linkend="populate-pitr">. If archiving or streaming replication were
|
|
|
|
turned on during execution of one of these statements, WAL would not
|
|
|
|
contain enough information for archive recovery. (Crash recovery is
|
2010-05-03 11:14:17 +02:00
|
|
|
unaffected.) For this reason, <varname>wal_level</> can only be changed at
|
2010-04-20 02:26:06 +02:00
|
|
|
server start. However, <varname>archive_command</> can be changed with a
|
2007-12-17 05:30:05 +01:00
|
|
|
configuration file reload. If you wish to temporarily stop archiving,
|
|
|
|
one way to do it is to set <varname>archive_command</> to the empty
|
|
|
|
string (<literal>''</>).
|
|
|
|
This will cause WAL files to accumulate in <filename>pg_xlog/</> until a
|
|
|
|
working <varname>archive_command</> is re-established.
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
2004-08-04 01:42:59 +02:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="backup-base-backup">
|
|
|
|
<title>Making a Base Backup</title>
|
|
|
|
|
|
|
|
<para>
|
2012-06-17 15:18:02 +02:00
|
|
|
The easiest way to perform a base backup is to use the
|
|
|
|
<xref linkend="app-pgbasebackup"> tool. It can create
|
|
|
|
a base backup either as regular files or as a tar archive. If more
|
|
|
|
flexibility than <xref linkend="app-pgbasebackup"> can provide is
|
|
|
|
required, you can also make a base backup using the low level API
|
|
|
|
(see <xref linkend="backup-lowlevel-base-backup">).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
It is not necessary to be concerned about the amount of time it takes
|
|
|
|
to make a base backup. However, if you normally run the
|
|
|
|
server with <varname>full_page_writes</> disabled, you might notice a drop
|
|
|
|
in performance while the backup runs since <varname>full_page_writes</> is
|
|
|
|
effectively forced on during backup mode.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
To make use of the backup, you will need to keep all the WAL
|
|
|
|
segment files generated during and after the file system backup.
|
|
|
|
To aid you in doing this, the base backup process
|
|
|
|
creates a <firstterm>backup history file</> that is immediately
|
|
|
|
stored into the WAL archive area. This file is named after the first
|
|
|
|
WAL segment file that you need for the file system backup.
|
|
|
|
For example, if the starting WAL file is
|
|
|
|
<literal>0000000100001234000055CD</> the backup history file will be
|
|
|
|
named something like
|
|
|
|
<literal>0000000100001234000055CD.007C9330.backup</>. (The second
|
|
|
|
part of the file name stands for an exact position within the WAL
|
|
|
|
file, and can ordinarily be ignored.) Once you have safely archived
|
|
|
|
the file system backup and the WAL segment files used during the
|
|
|
|
backup (as specified in the backup history file), all archived WAL
|
|
|
|
segments with names numerically less are no longer needed to recover
|
|
|
|
the file system backup and can be deleted. However, you should
|
|
|
|
consider keeping several backup sets to be absolutely certain that
|
|
|
|
you can recover your data.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The backup history file is just a small text file. It contains the
|
|
|
|
label string you gave to <xref linkend="app-pgbasebackup">, as well as
|
|
|
|
the starting and ending times and WAL segments of the backup.
|
|
|
|
If you used the label to identify the associated dump file,
|
|
|
|
then the archived history file is enough to tell you which dump file to
|
|
|
|
restore.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Since you have to keep around all the archived WAL files back to your
|
|
|
|
last base backup, the interval between base backups should usually be
|
|
|
|
chosen based on how much storage you want to expend on archived WAL
|
|
|
|
files. You should also consider how long you are prepared to spend
|
|
|
|
recovering, if recovery should be necessary — the system will have to
|
|
|
|
replay all those WAL segments, and that could take awhile if it has
|
|
|
|
been a long time since the last base backup.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="backup-lowlevel-base-backup">
|
|
|
|
<title>Making a Base Backup Using the Low Level API</title>
|
|
|
|
<para>
|
|
|
|
The procedure for making a base backup using the low level
|
|
|
|
APIs contains a few more steps than
|
|
|
|
the <xref linkend="app-pgbasebackup"> method, but is relatively
|
|
|
|
simple. It is very important that these steps are executed in
|
|
|
|
sequence, and that the success of a step is verified before
|
|
|
|
proceeding to the next step.
|
2016-04-20 20:40:04 +02:00
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
Low level base backups can be made in a non-exclusive or an exclusive
|
|
|
|
way. The non-exclusive method is recommended and the exclusive one is
|
|
|
|
deprecated and will eventually be removed.
|
|
|
|
</para>
|
|
|
|
<sect3 id="backup-lowlevel-base-backup-nonexclusive">
|
|
|
|
<title>Making a non-exclusive low level backup</title>
|
|
|
|
<para>
|
|
|
|
A non-exclusive low level backup is one that allows other
|
|
|
|
concurrent backups to be running (both those started using
|
|
|
|
the same backup API and those started using
|
|
|
|
<xref linkend="app-pgbasebackup">.
|
|
|
|
</para>
|
|
|
|
<para>
|
2004-08-04 01:42:59 +02:00
|
|
|
<orderedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Ensure that WAL archiving is enabled and working.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-04-20 20:40:04 +02:00
|
|
|
Connect to the server (it does not matter which database) as a user with
|
|
|
|
rights to run pg_start_backup (superuser, or a user who has been granted
|
|
|
|
EXECUTE on the function) and issue the command:
|
|
|
|
<programlisting>
|
|
|
|
SELECT pg_start_backup('label', false, false);
|
|
|
|
</programlisting>
|
|
|
|
where <literal>label</> is any string you want to use to uniquely
|
|
|
|
identify this backup operation. The connection
|
|
|
|
calling <function>pg_start_backup</> must be maintained until the end of
|
|
|
|
the backup, or the backup will be automatically aborted.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
By default, <function>pg_start_backup</> can take a long time to finish.
|
|
|
|
This is because it performs a checkpoint, and the I/O
|
|
|
|
required for the checkpoint will be spread out over a significant
|
|
|
|
period of time, by default half your inter-checkpoint interval
|
|
|
|
(see the configuration parameter
|
|
|
|
<xref linkend="guc-checkpoint-completion-target">). This is
|
|
|
|
usually what you want, because it minimizes the impact on query
|
|
|
|
processing. If you want to start the backup as soon as
|
|
|
|
possible, change the second parameter to <literal>true</>.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The third parameter being <literal>false</> tells
|
|
|
|
<function>pg_start_backup</> to initiate a non-exclusive base backup.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Perform the backup, using any convenient file-system-backup tool
|
|
|
|
such as <application>tar</> or <application>cpio</> (not
|
|
|
|
<application>pg_dump</application> or
|
|
|
|
<application>pg_dumpall</application>). It is neither
|
|
|
|
necessary nor desirable to stop normal operation of the database
|
|
|
|
while you do this. See section
|
|
|
|
<xref linkend="backup-lowlevel-base-backup-data"> for things to
|
|
|
|
consider during this backup.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
In the same connection as before, issue the command:
|
|
|
|
<programlisting>
|
|
|
|
SELECT * FROM pg_stop_backup(false);
|
|
|
|
</programlisting>
|
|
|
|
This terminates the backup mode and performs an automatic switch to
|
|
|
|
the next WAL segment. The reason for the switch is to arrange for
|
|
|
|
the last WAL segment file written during the backup interval to be
|
|
|
|
ready to archive.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
The <function>pg_stop_backup</> will return one row with three
|
|
|
|
values. The second of these fields should be written to a file named
|
|
|
|
<filename>backup_label</> in the root directory of the backup. The
|
|
|
|
third field should be written to a file named
|
|
|
|
<filename>tablespace_map</> unless the field is empty. These files are
|
|
|
|
vital to the backup working, and must be written without modification.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Once the WAL segment files active during the backup are archived, you are
|
|
|
|
done. The file identified by <function>pg_stop_backup</>'s first return
|
2016-05-17 17:28:18 +02:00
|
|
|
value is the last segment that is required to form a complete set of
|
|
|
|
backup files. If <varname>archive_mode</> is enabled,
|
2016-04-20 20:40:04 +02:00
|
|
|
<function>pg_stop_backup</> does not return until the last segment has
|
|
|
|
been archived.
|
|
|
|
Archiving of these files happens automatically since you have
|
|
|
|
already configured <varname>archive_command</>. In most cases this
|
|
|
|
happens quickly, but you are advised to monitor your archive
|
|
|
|
system to ensure there are no delays.
|
|
|
|
If the archive process has fallen behind
|
|
|
|
because of failures of the archive command, it will keep retrying
|
|
|
|
until the archive succeeds and the backup is complete.
|
|
|
|
If you wish to place a time limit on the execution of
|
|
|
|
<function>pg_stop_backup</>, set an appropriate
|
|
|
|
<varname>statement_timeout</varname> value, but make note that if
|
|
|
|
<function>pg_stop_backup</> terminates because of this your backup
|
|
|
|
may not be valid.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</orderedlist>
|
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
<sect3 id="backup-lowlevel-base-backup-exclusive">
|
|
|
|
<title>Making an exclusive low level backup</title>
|
|
|
|
<para>
|
|
|
|
The process for an exclusive backup is mostly the same as for a
|
|
|
|
non-exclusive one, but it differs in a few key steps. It does not allow
|
|
|
|
more than one concurrent backup to run, and there can be some issues on
|
|
|
|
the server if it crashes during the backup. Prior to PostgreSQL 9.6, this
|
|
|
|
was the only low-level method available, but it is now recommended that
|
|
|
|
all users upgrade their scripts to use non-exclusive backups if possible.
|
|
|
|
</para>
|
|
|
|
<para>
|
|
|
|
<orderedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Ensure that WAL archiving is enabled and working.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Connect to the server (it does not matter which database) as a user with
|
|
|
|
rights to run pg_start_backup (superuser, or a user who has been granted
|
|
|
|
EXECUTE on the function) and issue the command:
|
2004-08-04 01:42:59 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT pg_start_backup('label');
|
|
|
|
</programlisting>
|
|
|
|
where <literal>label</> is any string you want to use to uniquely
|
2016-04-20 20:40:04 +02:00
|
|
|
identify this backup operation.
|
2004-11-17 19:29:02 +01:00
|
|
|
<function>pg_start_backup</> creates a <firstterm>backup label</> file,
|
|
|
|
called <filename>backup_label</>, in the cluster directory with
|
2016-04-20 20:40:04 +02:00
|
|
|
information about your backup, including the start time and label string.
|
|
|
|
The function also creates a <firstterm>tablespace map</> file,
|
2015-05-12 15:29:10 +02:00
|
|
|
called <filename>tablespace_map</>, in the cluster directory with
|
2016-04-20 20:40:04 +02:00
|
|
|
information about tablespace symbolic links in <filename>pg_tblspc/</> if
|
|
|
|
one or more such link is present. Both files are critical to the
|
2015-05-12 15:29:10 +02:00
|
|
|
integrity of the backup, should you need to restore from it.
|
2004-11-14 07:10:12 +01:00
|
|
|
</para>
|
|
|
|
|
2007-06-29 17:46:21 +02:00
|
|
|
<para>
|
2009-04-07 02:31:26 +02:00
|
|
|
By default, <function>pg_start_backup</> can take a long time to finish.
|
2007-06-29 17:46:21 +02:00
|
|
|
This is because it performs a checkpoint, and the I/O
|
2009-04-07 02:31:26 +02:00
|
|
|
required for the checkpoint will be spread out over a significant
|
2007-06-29 17:46:21 +02:00
|
|
|
period of time, by default half your inter-checkpoint interval
|
|
|
|
(see the configuration parameter
|
2010-02-03 18:25:06 +01:00
|
|
|
<xref linkend="guc-checkpoint-completion-target">). This is
|
|
|
|
usually what you want, because it minimizes the impact on query
|
|
|
|
processing. If you want to start the backup as soon as
|
2009-04-07 02:31:26 +02:00
|
|
|
possible, use:
|
|
|
|
<programlisting>
|
|
|
|
SELECT pg_start_backup('label', true);
|
|
|
|
</programlisting>
|
|
|
|
This forces the checkpoint to be done as quickly as possible.
|
2007-06-29 17:46:21 +02:00
|
|
|
</para>
|
2004-08-04 01:42:59 +02:00
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-12-13 19:05:10 +01:00
|
|
|
Perform the backup, using any convenient file-system-backup tool
|
2010-02-22 18:15:10 +01:00
|
|
|
such as <application>tar</> or <application>cpio</> (not
|
|
|
|
<application>pg_dump</application> or
|
|
|
|
<application>pg_dumpall</application>). It is neither
|
2004-08-04 01:42:59 +02:00
|
|
|
necessary nor desirable to stop normal operation of the database
|
2016-04-20 20:40:04 +02:00
|
|
|
while you do this. See section
|
|
|
|
<xref linkend="backup-lowlevel-base-backup-data"> for things to
|
|
|
|
consider during this backup.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2016-04-07 03:45:32 +02:00
|
|
|
Again connect to the database as a user with rights to run
|
|
|
|
pg_stop_backup (superuser, or a user who has been granted EXECUTE on
|
|
|
|
the function), and issue the command:
|
2004-08-04 01:42:59 +02:00
|
|
|
<programlisting>
|
|
|
|
SELECT pg_stop_backup();
|
|
|
|
</programlisting>
|
2006-12-01 04:29:15 +01:00
|
|
|
This terminates the backup mode and performs an automatic switch to
|
2010-02-03 18:25:06 +01:00
|
|
|
the next WAL segment. The reason for the switch is to arrange for
|
|
|
|
the last WAL segment file written during the backup interval to be
|
|
|
|
ready to archive.
|
2005-04-18 15:11:04 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Once the WAL segment files active during the backup are archived, you are
|
2006-09-19 17:18:41 +02:00
|
|
|
done. The file identified by <function>pg_stop_backup</>'s result is
|
2009-04-07 02:31:26 +02:00
|
|
|
the last segment that is required to form a complete set of backup files.
|
2010-04-29 23:49:03 +02:00
|
|
|
If <varname>archive_mode</> is enabled,
|
2008-04-05 03:34:06 +02:00
|
|
|
<function>pg_stop_backup</> does not return until the last segment has
|
2009-04-07 02:31:26 +02:00
|
|
|
been archived.
|
2008-04-05 03:34:06 +02:00
|
|
|
Archiving of these files happens automatically since you have
|
|
|
|
already configured <varname>archive_command</>. In most cases this
|
|
|
|
happens quickly, but you are advised to monitor your archive
|
|
|
|
system to ensure there are no delays.
|
|
|
|
If the archive process has fallen behind
|
|
|
|
because of failures of the archive command, it will keep retrying
|
|
|
|
until the archive succeeds and the backup is complete.
|
|
|
|
If you wish to place a time limit on the execution of
|
|
|
|
<function>pg_stop_backup</>, set an appropriate
|
2016-04-20 20:40:04 +02:00
|
|
|
<varname>statement_timeout</varname> value, but make note that if
|
|
|
|
<function>pg_stop_backup</> terminates because of this your backup
|
|
|
|
may not be valid.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</orderedlist>
|
2016-04-20 20:40:04 +02:00
|
|
|
</para>
|
|
|
|
</sect3>
|
|
|
|
<sect3 id="backup-lowlevel-base-backup-data">
|
|
|
|
<title>Backing up the data directory</title>
|
2005-10-23 00:09:49 +02:00
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Some file system backup tools emit warnings or errors
|
2005-10-26 22:42:35 +02:00
|
|
|
if the files they are trying to copy change while the copy proceeds.
|
2010-02-03 18:25:06 +01:00
|
|
|
When taking a base backup of an active database, this situation is normal
|
|
|
|
and not an error. However, you need to ensure that you can distinguish
|
2005-10-26 22:42:35 +02:00
|
|
|
complaints of this sort from real errors. For example, some versions
|
2007-09-14 06:15:50 +02:00
|
|
|
of <application>rsync</> return a separate exit code for
|
|
|
|
<quote>vanished source files</>, and you can write a driver script to
|
|
|
|
accept this exit code as a non-error case. Also, some versions of
|
2007-09-14 15:26:22 +02:00
|
|
|
GNU <application>tar</> return an error code indistinguishable from
|
|
|
|
a fatal error if a file was truncated while <application>tar</> was
|
|
|
|
copying it. Fortunately, GNU <application>tar</> versions 1.16 and
|
2011-02-07 00:32:27 +01:00
|
|
|
later exit with 1 if a file was changed during the backup,
|
2011-11-04 20:52:37 +01:00
|
|
|
and 2 for other errors. With GNU <application>tar</> version 1.23 and
|
|
|
|
later, you can use the warning options <literal>--warning=no-file-changed
|
|
|
|
--warning=no-file-removed</literal> to hide the related warning messages.
|
2005-10-23 00:09:49 +02:00
|
|
|
</para>
|
|
|
|
|
2004-08-04 01:42:59 +02:00
|
|
|
<para>
|
2016-04-20 20:40:04 +02:00
|
|
|
Be certain that your backup includes all of the files under
|
2004-11-08 19:01:28 +01:00
|
|
|
the database cluster directory (e.g., <filename>/usr/local/pgsql/data</>).
|
2004-08-04 01:42:59 +02:00
|
|
|
If you are using tablespaces that do not reside underneath this directory,
|
2016-04-20 20:40:04 +02:00
|
|
|
be careful to include them as well (and be sure that your backup
|
2010-02-03 18:25:06 +01:00
|
|
|
archives symbolic links as links, otherwise the restore will corrupt
|
2004-08-04 01:42:59 +02:00
|
|
|
your tablespaces).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2016-04-20 20:40:04 +02:00
|
|
|
You should, however, omit from the backup the files within the
|
2010-02-03 18:25:06 +01:00
|
|
|
cluster's <filename>pg_xlog/</> subdirectory. This
|
|
|
|
slight adjustment is worthwhile because it reduces the risk
|
2004-08-04 01:42:59 +02:00
|
|
|
of mistakes when restoring. This is easy to arrange if
|
2004-11-08 19:01:28 +01:00
|
|
|
<filename>pg_xlog/</> is a symbolic link pointing to someplace outside
|
2004-08-04 01:42:59 +02:00
|
|
|
the cluster directory, which is a common setup anyway for performance
|
2011-10-11 23:33:20 +02:00
|
|
|
reasons. You might also want to exclude <filename>postmaster.pid</>
|
|
|
|
and <filename>postmaster.opts</>, which record information
|
|
|
|
about the running <application>postmaster</>, not about the
|
|
|
|
<application>postmaster</> which will eventually use this backup.
|
|
|
|
(These files can confuse <application>pg_ctl</>.)
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
2014-04-14 19:00:04 +02:00
|
|
|
<para>
|
2016-04-20 20:40:04 +02:00
|
|
|
It is often a good idea to also omit from the backup the files
|
2014-04-14 19:00:04 +02:00
|
|
|
within the cluster's <filename>pg_replslot/</> directory, so that
|
|
|
|
replication slots that exist on the master do not become part of the
|
|
|
|
backup. Otherwise, the subsequent use of the backup to create a standby
|
|
|
|
may result in indefinite retention of WAL files on the standby, and
|
|
|
|
possibly bloat on the master if hot standby feedback is enabled, because
|
|
|
|
the clients that are using those replication slots will still be connecting
|
|
|
|
to and updating the slots on the master, not the standby. Even if the
|
|
|
|
backup is only intended for use in creating a new master, copying the
|
|
|
|
replication slots isn't expected to be particularly useful, since the
|
|
|
|
contents of those slots will likely be badly out of date by the time
|
|
|
|
the new master comes on line.
|
|
|
|
</para>
|
|
|
|
|
2004-08-04 01:42:59 +02:00
|
|
|
<para>
|
2016-04-20 20:40:04 +02:00
|
|
|
The backup label
|
2015-05-12 15:29:10 +02:00
|
|
|
file includes the label string you gave to <function>pg_start_backup</>,
|
|
|
|
as well as the time at which <function>pg_start_backup</> was run, and
|
|
|
|
the name of the starting WAL file. In case of confusion it is therefore
|
2016-04-20 20:40:04 +02:00
|
|
|
possible to look inside a backup file and determine exactly which
|
2015-05-12 15:29:10 +02:00
|
|
|
backup session the dump file came from. The tablespace map file includes
|
|
|
|
the symbolic link names as they exist in the directory
|
|
|
|
<filename>pg_tblspc/</> and the full path of each symbolic link.
|
|
|
|
These files are not merely for your information; their presence and
|
|
|
|
contents are critical to the proper operation of the system's recovery
|
|
|
|
process.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2016-04-20 20:40:04 +02:00
|
|
|
It is also possible to make a backup while the server is
|
2004-11-14 07:10:12 +01:00
|
|
|
stopped. In this case, you obviously cannot use
|
2004-08-04 01:42:59 +02:00
|
|
|
<function>pg_start_backup</> or <function>pg_stop_backup</>, and
|
|
|
|
you will therefore be left to your own devices to keep track of which
|
2016-04-20 20:40:04 +02:00
|
|
|
backup is which and how far back the associated WAL files go.
|
2006-03-03 23:02:08 +01:00
|
|
|
It is generally better to follow the continuous archiving procedure above.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
2016-04-20 20:40:04 +02:00
|
|
|
</sect3>
|
2004-08-04 01:42:59 +02:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="backup-pitr-recovery">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Recovering Using a Continuous Archive Backup</title>
|
2004-08-04 01:42:59 +02:00
|
|
|
|
|
|
|
<para>
|
|
|
|
Okay, the worst has happened and you need to recover from your backup.
|
|
|
|
Here is the procedure:
|
|
|
|
<orderedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2006-06-18 17:38:37 +02:00
|
|
|
Stop the server, if it's running.
|
2004-11-17 19:29:02 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
If you have the space to do so,
|
2007-12-17 05:30:05 +01:00
|
|
|
copy the whole cluster data directory and any tablespaces to a temporary
|
2004-11-17 19:29:02 +01:00
|
|
|
location in case you need them later. Note that this precaution will
|
2004-11-14 07:10:12 +01:00
|
|
|
require that you have enough free space on your system to hold two
|
2007-12-17 05:30:05 +01:00
|
|
|
copies of your existing database. If you do not have enough space,
|
2010-02-03 18:25:06 +01:00
|
|
|
you should at least save the contents of the cluster's <filename>pg_xlog</>
|
|
|
|
subdirectory, as it might contain logs which
|
2004-11-17 19:29:02 +01:00
|
|
|
were not archived before the system went down.
|
2004-11-14 07:10:12 +01:00
|
|
|
</para>
|
2004-11-17 19:29:02 +01:00
|
|
|
</listitem>
|
|
|
|
<listitem>
|
2004-11-14 07:10:12 +01:00
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Remove all existing files and subdirectories under the cluster data
|
2004-11-17 19:29:02 +01:00
|
|
|
directory and under the root directories of any tablespaces you are using.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Restore the database files from your file system backup. Be sure that they
|
2004-08-04 01:42:59 +02:00
|
|
|
are restored with the right ownership (the database system user, not
|
2007-12-17 05:30:05 +01:00
|
|
|
<literal>root</>!) and with the right permissions. If you are using
|
|
|
|
tablespaces,
|
2006-12-01 04:29:15 +01:00
|
|
|
you should verify that the symbolic links in <filename>pg_tblspc/</>
|
2004-08-04 01:42:59 +02:00
|
|
|
were correctly restored.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-11-08 19:01:28 +01:00
|
|
|
Remove any files present in <filename>pg_xlog/</>; these came from the
|
2010-02-03 18:25:06 +01:00
|
|
|
file system backup and are therefore probably obsolete rather than current.
|
|
|
|
If you didn't archive <filename>pg_xlog/</> at all, then recreate
|
|
|
|
it with proper permissions,
|
2007-12-17 05:30:05 +01:00
|
|
|
being careful to ensure that you re-establish it as a symbolic link
|
|
|
|
if you had it set up that way before.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
If you have unarchived WAL segment files that you saved in step 2,
|
2004-11-14 07:10:12 +01:00
|
|
|
copy them into <filename>pg_xlog/</>. (It is best to copy them,
|
2010-02-03 18:25:06 +01:00
|
|
|
not move them, so you still have the unmodified files if a
|
2004-11-14 07:10:12 +01:00
|
|
|
problem occurs and you have to start over.)
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2004-11-08 19:01:28 +01:00
|
|
|
Create a recovery command file <filename>recovery.conf</> in the cluster
|
2010-02-22 12:47:30 +01:00
|
|
|
data directory (see <xref linkend="recovery-config">). You might
|
2007-12-17 05:30:05 +01:00
|
|
|
also want to temporarily modify <filename>pg_hba.conf</> to prevent
|
2010-02-03 18:25:06 +01:00
|
|
|
ordinary users from connecting until you are sure the recovery was successful.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2006-06-18 17:38:37 +02:00
|
|
|
Start the server. The server will go into recovery mode and
|
2006-09-15 23:55:07 +02:00
|
|
|
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
|
2006-06-18 17:38:37 +02:00
|
|
|
of the recovery process, the server will rename
|
2004-11-08 19:01:28 +01:00
|
|
|
<filename>recovery.conf</> to <filename>recovery.done</> (to prevent
|
2010-02-03 18:25:06 +01:00
|
|
|
accidentally re-entering recovery mode later) and then
|
2004-08-04 01:42:59 +02:00
|
|
|
commence normal database operations.
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Inspect the contents of the database to ensure you have recovered to
|
2010-02-03 18:25:06 +01:00
|
|
|
the desired state. If not, return to step 1. If all is well,
|
|
|
|
allow your users to connect by restoring <filename>pg_hba.conf</> to normal.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</orderedlist>
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
The key part of all this is to set up a recovery configuration file that
|
2004-11-14 07:10:12 +01:00
|
|
|
describes how you want to recover and how far the recovery should
|
|
|
|
run. You can use <filename>recovery.conf.sample</> (normally
|
2010-02-03 18:25:06 +01:00
|
|
|
located in the installation's <filename>share/</> directory) as a
|
2004-08-04 01:42:59 +02:00
|
|
|
prototype. The one thing that you absolutely must specify in
|
2004-11-14 07:10:12 +01:00
|
|
|
<filename>recovery.conf</> is the <varname>restore_command</>,
|
2010-02-03 18:25:06 +01:00
|
|
|
which tells <productname>PostgreSQL</> how to retrieve archived
|
2004-11-14 07:10:12 +01:00
|
|
|
WAL file segments. Like the <varname>archive_command</>, this is
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
a shell command string. It can contain <literal>%f</>, which is
|
2004-11-14 07:10:12 +01:00
|
|
|
replaced by the name of the desired log file, and <literal>%p</>,
|
2006-11-04 19:20:27 +01:00
|
|
|
which is replaced by the path name to copy the log file to.
|
2007-12-17 05:30:05 +01:00
|
|
|
(The path name is relative to the current working directory,
|
2006-11-04 19:20:27 +01:00
|
|
|
i.e., the cluster's data directory.)
|
2004-08-04 01:42:59 +02:00
|
|
|
Write <literal>%%</> if you need to embed an actual <literal>%</>
|
2004-11-14 07:10:12 +01:00
|
|
|
character in the command. The simplest useful command is
|
2007-02-01 01:28:19 +01:00
|
|
|
something like:
|
2004-08-04 01:42:59 +02:00
|
|
|
<programlisting>
|
|
|
|
restore_command = 'cp /mnt/server/archivedir/%f %p'
|
|
|
|
</programlisting>
|
|
|
|
which will copy previously archived WAL segments from the directory
|
2010-02-03 18:25:06 +01:00
|
|
|
<filename>/mnt/server/archivedir</>. Of course, you can use something
|
2004-08-04 01:42:59 +02:00
|
|
|
much more complicated, perhaps even a shell script that requests the
|
|
|
|
operator to mount an appropriate tape.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
It is important that the command return nonzero exit status on failure.
|
2013-12-03 04:31:41 +01:00
|
|
|
The command <emphasis>will</> be called requesting files that are not
|
|
|
|
present in the archive; it must return nonzero when so asked. This is not
|
|
|
|
an error condition. An exception is that if the command was terminated by
|
|
|
|
a signal (other than <systemitem>SIGTERM</systemitem>, which is used as
|
|
|
|
part of a database server shutdown) or an error by the shell (such as
|
|
|
|
command not found), then recovery will abort and the server will not start
|
|
|
|
up.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Not all of the requested files will be WAL segment
|
2008-03-28 16:00:28 +01:00
|
|
|
files; you should also expect requests for files with a suffix of
|
|
|
|
<literal>.backup</> or <literal>.history</>. Also be aware that
|
|
|
|
the base name of the <literal>%p</> path will be different from
|
|
|
|
<literal>%f</>; do not expect them to be interchangeable.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
WAL segments that cannot be found in the archive will be sought in
|
2004-11-08 19:01:28 +01:00
|
|
|
<filename>pg_xlog/</>; this allows use of recent un-archived segments.
|
2010-02-03 18:25:06 +01:00
|
|
|
However, segments that are available from the archive will be used in
|
2012-08-08 23:34:46 +02:00
|
|
|
preference to files in <filename>pg_xlog/</>.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
Normally, recovery will proceed through all available WAL segments,
|
2004-11-14 07:10:12 +01:00
|
|
|
thereby restoring the database to the current point in time (or as
|
2010-02-03 18:25:06 +01:00
|
|
|
close as possible given the available WAL segments). Therefore, a normal
|
2007-12-17 05:30:05 +01:00
|
|
|
recovery will end with a <quote>file not found</> message, the exact text
|
|
|
|
of the error message depending upon your choice of
|
2007-11-28 23:35:54 +01:00
|
|
|
<varname>restore_command</>. You may also see an error message
|
|
|
|
at the start of recovery for a file named something like
|
|
|
|
<filename>00000001.history</>. This is also normal and does not
|
2010-02-03 18:25:06 +01:00
|
|
|
indicate a problem in simple recovery situations; see
|
2007-11-28 23:35:54 +01:00
|
|
|
<xref linkend="backup-timelines"> for discussion.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If you want to recover to some previous point in time (say, right before
|
2007-12-17 05:30:05 +01:00
|
|
|
the junior DBA dropped your main transaction table), just specify the
|
2014-01-25 16:34:04 +01:00
|
|
|
required <link linkend="recovery-target-settings">stopping point</link> in <filename>recovery.conf</>. You can specify
|
2007-12-17 05:30:05 +01:00
|
|
|
the stop point, known as the <quote>recovery target</>, either by
|
2011-02-08 20:39:08 +01:00
|
|
|
date/time, named restore point or by completion of a specific transaction
|
|
|
|
ID. As of this writing only the date/time and named restore point options
|
|
|
|
are very usable, since there are no tools to help you identify with any
|
|
|
|
accuracy which transaction ID to use.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
2004-11-08 19:01:28 +01:00
|
|
|
|
2004-11-14 07:10:12 +01:00
|
|
|
<note>
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
The stop point must be after the ending time of the base backup, i.e.,
|
2008-04-05 03:34:06 +02:00
|
|
|
the end time of <function>pg_stop_backup</>. You cannot use a base backup
|
2010-02-03 18:25:06 +01:00
|
|
|
to recover to a time when that backup was in progress. (To
|
2004-11-14 07:10:12 +01:00
|
|
|
recover to such a time, you must go back to your previous base backup
|
|
|
|
and roll forward from there.)
|
|
|
|
</para>
|
2006-09-15 23:55:07 +02:00
|
|
|
</note>
|
|
|
|
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
If recovery finds corrupted WAL data, recovery will
|
|
|
|
halt at that point and the server will not start. In such a case the
|
2007-12-17 05:30:05 +01:00
|
|
|
recovery process could be re-run from the beginning, specifying a
|
2006-12-01 04:29:15 +01:00
|
|
|
<quote>recovery target</> before the point of corruption so that recovery
|
|
|
|
can complete normally.
|
2006-09-15 23:55:07 +02:00
|
|
|
If recovery fails for an external reason, such as a system crash or
|
2006-12-01 04:29:15 +01:00
|
|
|
if the WAL archive has become inaccessible, then the recovery can simply
|
|
|
|
be restarted and it will restart almost from where it failed.
|
|
|
|
Recovery restart works much like checkpointing in normal operation:
|
|
|
|
the server periodically forces all its state to disk, and then updates
|
|
|
|
the <filename>pg_control</> file to indicate that the already-processed
|
|
|
|
WAL data need not be scanned again.
|
2006-09-15 23:55:07 +02:00
|
|
|
</para>
|
|
|
|
|
2004-08-04 01:42:59 +02:00
|
|
|
</sect2>
|
|
|
|
|
|
|
|
<sect2 id="backup-timelines">
|
|
|
|
<title>Timelines</title>
|
|
|
|
|
|
|
|
<indexterm zone="backup">
|
|
|
|
<primary>timelines</primary>
|
|
|
|
</indexterm>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The ability to restore the database to a previous point in time creates
|
|
|
|
some complexities that are akin to science-fiction stories about time
|
2010-02-03 18:25:06 +01:00
|
|
|
travel and parallel universes. For example, in the original history of the database,
|
|
|
|
suppose you dropped a critical table at 5:15PM on Tuesday evening, but
|
2007-12-17 05:30:05 +01:00
|
|
|
didn't realize your mistake until Wednesday noon.
|
2004-08-04 01:42:59 +02:00
|
|
|
Unfazed, you get out your backup, restore to the point-in-time 5:14PM
|
|
|
|
Tuesday evening, and are up and running. In <emphasis>this</> history of
|
2010-02-03 18:25:06 +01:00
|
|
|
the database universe, you never dropped the table. But suppose
|
|
|
|
you later realize this wasn't such a great idea, and would like
|
2007-12-17 05:30:05 +01:00
|
|
|
to return to sometime Wednesday morning in the original history.
|
|
|
|
You won't be able
|
2004-08-04 01:42:59 +02:00
|
|
|
to if, while your database was up-and-running, it overwrote some of the
|
2010-02-03 18:25:06 +01:00
|
|
|
WAL segment files that led up to the time you now wish you
|
|
|
|
could get back to. Thus, to avoid this, you need to distinguish the series of
|
2004-08-04 01:42:59 +02:00
|
|
|
WAL records generated after you've done a point-in-time recovery from
|
|
|
|
those that were generated in the original database history.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
To deal with this problem, <productname>PostgreSQL</> has a notion
|
|
|
|
of <firstterm>timelines</>. Whenever an archive recovery completes,
|
2007-09-29 03:36:10 +02:00
|
|
|
a new timeline is created to identify the series of WAL records
|
|
|
|
generated after that recovery. The timeline
|
2010-02-03 18:25:06 +01:00
|
|
|
ID number is part of WAL segment file names so a new timeline does
|
2004-08-04 01:42:59 +02:00
|
|
|
not overwrite the WAL data generated by previous timelines. It is
|
|
|
|
in fact possible to archive many different timelines. While that might
|
|
|
|
seem like a useless feature, it's often a lifesaver. Consider the
|
|
|
|
situation where you aren't quite sure what point-in-time to recover to,
|
|
|
|
and so have to do several point-in-time recoveries by trial and error
|
|
|
|
until you find the best place to branch off from the old history. Without
|
|
|
|
timelines this process would soon generate an unmanageable mess. With
|
|
|
|
timelines, you can recover to <emphasis>any</> prior state, including
|
2010-02-03 18:25:06 +01:00
|
|
|
states in timeline branches that you abandoned earlier.
|
2004-08-04 01:42:59 +02:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2010-02-03 18:25:06 +01:00
|
|
|
Every time a new timeline is created, <productname>PostgreSQL</> creates
|
2004-08-04 01:42:59 +02:00
|
|
|
a <quote>timeline history</> file that shows which timeline it branched
|
|
|
|
off from and when. These history files are necessary to allow the system
|
|
|
|
to pick the right WAL segment files when recovering from an archive that
|
|
|
|
contains multiple timelines. Therefore, they are archived into the WAL
|
|
|
|
archive area just like WAL segment files. The history files are just
|
|
|
|
small text files, so it's cheap and appropriate to keep them around
|
|
|
|
indefinitely (unlike the segment files which are large). You can, if
|
2010-02-03 18:25:06 +01:00
|
|
|
you like, add comments to a history file to record your own notes about
|
|
|
|
how and why this particular timeline was created. Such comments will be
|
2004-08-04 01:42:59 +02:00
|
|
|
especially valuable when you have a thicket of different timelines as
|
|
|
|
a result of experimentation.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
The default behavior of recovery is to recover along the same timeline
|
2010-02-03 18:25:06 +01:00
|
|
|
that was current when the base backup was taken. If you wish to recover
|
2004-08-04 01:42:59 +02:00
|
|
|
into some child timeline (that is, you want to return to some state that
|
|
|
|
was itself generated after a recovery attempt), you need to specify the
|
2004-12-28 20:08:58 +01:00
|
|
|
target timeline ID in <filename>recovery.conf</>. You cannot recover into
|
2004-08-04 01:42:59 +02:00
|
|
|
timelines that branched off earlier than the base backup.
|
|
|
|
</para>
|
|
|
|
</sect2>
|
2004-08-04 19:37:09 +02:00
|
|
|
|
2007-11-28 23:35:54 +01:00
|
|
|
<sect2 id="backup-tips">
|
|
|
|
<title>Tips and Examples</title>
|
|
|
|
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
Some tips for configuring continuous archiving are given here.
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<sect3 id="backup-standalone">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title>Standalone Hot Backups</title>
|
2007-11-28 23:35:54 +01:00
|
|
|
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
It is possible to use <productname>PostgreSQL</>'s backup facilities to
|
|
|
|
produce standalone hot backups. These are backups that cannot be used
|
|
|
|
for point-in-time recovery, yet are typically much faster to backup and
|
|
|
|
restore than <application>pg_dump</> dumps. (They are also much larger
|
|
|
|
than <application>pg_dump</> dumps, so in some cases the speed advantage
|
2010-02-03 18:25:06 +01:00
|
|
|
might be negated.)
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
2012-06-17 15:18:02 +02:00
|
|
|
As with base backups, the easiest way to produce a standalone
|
|
|
|
hot backup is to use the <xref linkend="app-pgbasebackup">
|
|
|
|
tool. If you include the <literal>-X</> parameter when calling
|
|
|
|
it, all the transaction log required to use the backup will be
|
|
|
|
included in the backup automatically, and no special action is
|
|
|
|
required to restore the backup.
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
If more flexibility in copying the backup files is needed, a lower
|
|
|
|
level process can be used for standalone hot backups as well.
|
|
|
|
To prepare for low level standalone hot backups, set <varname>wal_level</> to
|
2016-03-01 02:01:54 +01:00
|
|
|
<literal>replica</> or higher, <varname>archive_mode</> to
|
2007-12-17 05:30:05 +01:00
|
|
|
<literal>on</>, and set up an <varname>archive_command</> that performs
|
2010-02-03 18:25:06 +01:00
|
|
|
archiving only when a <emphasis>switch file</> exists. For example:
|
2007-11-28 23:35:54 +01:00
|
|
|
<programlisting>
|
2011-06-20 22:27:32 +02:00
|
|
|
archive_command = 'test ! -f /var/lib/pgsql/backup_in_progress || (test ! -f /var/lib/pgsql/archive/%f && cp %p /var/lib/pgsql/archive/%f)'
|
2007-11-28 23:35:54 +01:00
|
|
|
</programlisting>
|
2007-12-17 05:30:05 +01:00
|
|
|
This command will perform archiving when
|
|
|
|
<filename>/var/lib/pgsql/backup_in_progress</> exists, and otherwise
|
|
|
|
silently return zero exit status (allowing <productname>PostgreSQL</>
|
|
|
|
to recycle the unwanted WAL file).
|
|
|
|
</para>
|
|
|
|
|
|
|
|
<para>
|
|
|
|
With this preparation, a backup can be taken using a script like the
|
|
|
|
following:
|
2007-11-28 23:35:54 +01:00
|
|
|
<programlisting>
|
|
|
|
touch /var/lib/pgsql/backup_in_progress
|
|
|
|
psql -c "select pg_start_backup('hot_backup');"
|
2007-12-17 05:30:05 +01:00
|
|
|
tar -cf /var/lib/pgsql/backup.tar /var/lib/pgsql/data/
|
2007-11-28 23:35:54 +01:00
|
|
|
psql -c "select pg_stop_backup();"
|
|
|
|
rm /var/lib/pgsql/backup_in_progress
|
2007-12-17 05:30:05 +01:00
|
|
|
tar -rf /var/lib/pgsql/backup.tar /var/lib/pgsql/archive/
|
2007-11-28 23:35:54 +01:00
|
|
|
</programlisting>
|
|
|
|
The switch file <filename>/var/lib/pgsql/backup_in_progress</> is
|
2007-12-17 05:30:05 +01:00
|
|
|
created first, enabling archiving of completed WAL files to occur.
|
2007-11-28 23:35:54 +01:00
|
|
|
After the backup the switch file is removed. Archived WAL files are
|
|
|
|
then added to the backup so that both base backup and all required
|
|
|
|
WAL files are part of the same <application>tar</> file.
|
2008-04-05 03:34:06 +02:00
|
|
|
Please remember to add error handling to your backup scripts.
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
2009-01-13 01:54:11 +01:00
|
|
|
|
2012-06-17 15:18:02 +02:00
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="compressed-archive-logs">
|
|
|
|
<title>Compressed Archive Logs</title>
|
|
|
|
|
2012-04-09 20:50:48 +02:00
|
|
|
<para>
|
|
|
|
If archive storage size is a concern, you can use
|
|
|
|
<application>gzip</application> to compress the archive files:
|
|
|
|
<programlisting>
|
|
|
|
archive_command = 'gzip < %p > /var/lib/pgsql/archive/%f'
|
|
|
|
</programlisting>
|
|
|
|
You will then need to use <application>gunzip</> during recovery:
|
|
|
|
<programlisting>
|
|
|
|
restore_command = 'gunzip < /mnt/server/archivedir/%f > %p'
|
|
|
|
</programlisting>
|
|
|
|
</para>
|
2007-11-28 23:35:54 +01:00
|
|
|
</sect3>
|
|
|
|
|
|
|
|
<sect3 id="backup-scripts">
|
2011-01-29 19:00:18 +01:00
|
|
|
<title><varname>archive_command</varname> Scripts</title>
|
2007-11-28 23:35:54 +01:00
|
|
|
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
Many people choose to use scripts to define their
|
2007-11-28 23:35:54 +01:00
|
|
|
<varname>archive_command</varname>, so that their
|
2007-12-17 05:30:05 +01:00
|
|
|
<filename>postgresql.conf</> entry looks very simple:
|
2007-11-28 23:35:54 +01:00
|
|
|
<programlisting>
|
2011-11-04 21:01:35 +01:00
|
|
|
archive_command = 'local_backup_script.sh "%p" "%f"'
|
2007-11-28 23:35:54 +01:00
|
|
|
</programlisting>
|
2007-12-17 05:30:05 +01:00
|
|
|
Using a separate script file is advisable any time you want to use
|
|
|
|
more than a single command in the archiving process.
|
2007-11-28 23:35:54 +01:00
|
|
|
This allows all complexity to be managed within the script, which
|
|
|
|
can be written in a popular scripting language such as
|
2007-12-17 05:30:05 +01:00
|
|
|
<application>bash</> or <application>perl</>.
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
2007-12-17 05:30:05 +01:00
|
|
|
|
2007-11-28 23:35:54 +01:00
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
Examples of requirements that might be solved within a script include:
|
2007-11-28 23:35:54 +01:00
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
Copying data to secure off-site data storage
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
Batching WAL files so that they are transferred every three hours,
|
|
|
|
rather than one at a time
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
|
|
|
Interfacing with other backup and recovery software
|
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2007-12-17 05:30:05 +01:00
|
|
|
Interfacing with monitoring software to report errors
|
2007-11-28 23:35:54 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
2012-03-05 20:08:52 +01:00
|
|
|
|
|
|
|
<tip>
|
|
|
|
<para>
|
|
|
|
When using an <varname>archive_command</varname> script, it's desirable
|
|
|
|
to enable <xref linkend="guc-logging-collector">.
|
|
|
|
Any messages written to <systemitem>stderr</> from the script will then
|
|
|
|
appear in the database server log, allowing complex configurations to
|
|
|
|
be diagnosed easily if they fail.
|
|
|
|
</para>
|
|
|
|
</tip>
|
2007-11-28 23:35:54 +01:00
|
|
|
</sect3>
|
|
|
|
</sect2>
|
|
|
|
|
2006-03-03 23:02:08 +01:00
|
|
|
<sect2 id="continuous-archiving-caveats">
|
2004-08-04 19:37:09 +02:00
|
|
|
<title>Caveats</title>
|
|
|
|
|
|
|
|
<para>
|
2006-03-03 23:02:08 +01:00
|
|
|
At this writing, there are several limitations of the continuous archiving
|
2004-08-29 23:08:48 +02:00
|
|
|
technique. These will probably be fixed in future releases:
|
2004-08-04 19:37:09 +02:00
|
|
|
|
|
|
|
<itemizedlist>
|
|
|
|
<listitem>
|
|
|
|
<para>
|
2006-10-12 21:38:08 +02:00
|
|
|
Operations on hash indexes are not presently WAL-logged, so
|
Allow read only connections during recovery, known as Hot Standby.
Enabled by recovery_connections = on (default) and forcing archive recovery using a recovery.conf. Recovery processing now emulates the original transactions as they are replayed, providing full locking and MVCC behaviour for read only queries. Recovery must enter consistent state before connections are allowed, so there is a delay, typically short, before connections succeed. Replay of recovering transactions can conflict and in some cases deadlock with queries during recovery; these result in query cancellation after max_standby_delay seconds have expired. Infrastructure changes have minor effects on normal running, though introduce four new types of WAL record.
New test mode "make standbycheck" allows regression tests of static command behaviour on a standby server while in recovery. Typical and extreme dynamic behaviours have been checked via code inspection and manual testing. Few port specific behaviours have been utilised, though primary testing has been on Linux only so far.
This commit is the basic patch. Additional changes will follow in this release to enhance some aspects of behaviour, notably improved handling of conflicts, deadlock detection and query cancellation. Changes to VACUUM FULL are also required.
Simon Riggs, with significant and lengthy review by Heikki Linnakangas, including streamlined redesign of snapshot creation and two-phase commit.
Important contributions from Florian Pflug, Mark Kirkwood, Merlin Moncure, Greg Stark, Gianni Ciolli, Gabriele Bartolini, Hannu Krosing, Robert Haas, Tatsuo Ishii, Hiroyuki Yamada plus support and feedback from many other community members.
2009-12-19 02:32:45 +01:00
|
|
|
replay will not update these indexes. This will mean that any new inserts
|
2009-12-19 18:49:50 +01:00
|
|
|
will be ignored by the index, updated rows will apparently disappear and
|
|
|
|
deleted rows will still retain pointers. In other words, if you modify a
|
|
|
|
table with a hash index on it then you will get incorrect query results
|
|
|
|
on a standby server. When recovery completes it is recommended that you
|
2010-04-03 09:23:02 +02:00
|
|
|
manually <xref linkend="sql-reindex">
|
2006-10-12 21:38:08 +02:00
|
|
|
each such index after completing a recovery operation.
|
2004-08-04 19:37:09 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
2005-03-23 20:38:53 +01:00
|
|
|
|
2005-06-26 00:47:29 +02:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-04-03 09:23:02 +02:00
|
|
|
If a <xref linkend="sql-createdatabase">
|
2006-10-12 21:38:08 +02:00
|
|
|
command is executed while a base backup is being taken, and then
|
|
|
|
the template database that the <command>CREATE DATABASE</> copied
|
|
|
|
is modified while the base backup is still in progress, it is
|
|
|
|
possible that recovery will cause those modifications to be
|
|
|
|
propagated into the created database as well. This is of course
|
|
|
|
undesirable. To avoid this risk, it is best not to modify any
|
|
|
|
template databases while taking a base backup.
|
2005-06-26 00:47:29 +02:00
|
|
|
</para>
|
|
|
|
</listitem>
|
|
|
|
|
2005-03-23 20:38:53 +01:00
|
|
|
<listitem>
|
|
|
|
<para>
|
2010-04-03 09:23:02 +02:00
|
|
|
<xref linkend="sql-createtablespace">
|
2006-10-12 21:38:08 +02:00
|
|
|
commands are WAL-logged with the literal absolute path, and will
|
|
|
|
therefore be replayed as tablespace creations with the same
|
|
|
|
absolute path. This might be undesirable if the log is being
|
|
|
|
replayed on a different machine. It can be dangerous even if the
|
|
|
|
log is being replayed on the same machine, but into a new data
|
|
|
|
directory: the replay will still overwrite the contents of the
|
|
|
|
original tablespace. To avoid potential gotchas of this sort,
|
|
|
|
the best practice is to take a new base backup after creating or
|
|
|
|
dropping tablespaces.
|
2005-03-23 20:38:53 +01:00
|
|
|
</para>
|
|
|
|
</listitem>
|
2004-08-04 19:37:09 +02:00
|
|
|
</itemizedlist>
|
|
|
|
</para>
|
2004-08-08 06:34:43 +02:00
|
|
|
|
|
|
|
<para>
|
2005-10-13 19:32:42 +02:00
|
|
|
It should also be noted that the default <acronym>WAL</acronym>
|
2005-10-22 23:56:07 +02:00
|
|
|
format is fairly bulky since it includes many disk page snapshots.
|
2006-10-12 21:38:08 +02:00
|
|
|
These page snapshots are designed to support crash recovery, since
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
we might need to fix partially-written disk pages. Depending on
|
|
|
|
your system hardware and software, the risk of partial writes might
|
2006-10-12 21:38:08 +02:00
|
|
|
be small enough to ignore, in which case you can significantly
|
|
|
|
reduce the total volume of archived logs by turning off page
|
|
|
|
snapshots using the <xref linkend="guc-full-page-writes">
|
|
|
|
parameter. (Read the notes and warnings in <xref linkend="wal">
|
|
|
|
before you do so.) Turning off page snapshots does not prevent
|
|
|
|
use of the logs for PITR operations. An area for future
|
|
|
|
development is to compress archived WAL data by removing
|
|
|
|
unnecessary page copies even when <varname>full_page_writes</> is
|
Update documentation on may/can/might:
Standard English uses "may", "can", and "might" in different ways:
may - permission, "You may borrow my rake."
can - ability, "I can lift that log."
might - possibility, "It might rain today."
Unfortunately, in conversational English, their use is often mixed, as
in, "You may use this variable to do X", when in fact, "can" is a better
choice. Similarly, "It may crash" is better stated, "It might crash".
Also update two error messages mentioned in the documenation to match.
2007-01-31 21:56:20 +01:00
|
|
|
on. In the meantime, administrators might wish to reduce the number
|
2006-10-12 21:38:08 +02:00
|
|
|
of page snapshots included in WAL by increasing the checkpoint
|
|
|
|
interval parameters as much as feasible.
|
2004-08-08 06:34:43 +02:00
|
|
|
</para>
|
2004-08-04 19:37:09 +02:00
|
|
|
</sect2>
|
2000-06-30 18:14:21 +02:00
|
|
|
</sect1>
|
|
|
|
|
|
|
|
</chapter>
|