]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/pg_basebackup.sgml
Add --xlogdir option to pg_basebackup, for specifying the pg_xlog directory.
[postgresql] / doc / src / sgml / ref / pg_basebackup.sgml
1 <!--
2 doc/src/sgml/ref/pg_basebackup.sgml
3 PostgreSQL documentation
4 -->
5
6 <refentry id="app-pgbasebackup">
7  <refmeta>
8   <refentrytitle>pg_basebackup</refentrytitle>
9   <manvolnum>1</manvolnum>
10   <refmiscinfo>Application</refmiscinfo>
11  </refmeta>
12
13  <refnamediv>
14   <refname>pg_basebackup</refname>
15   <refpurpose>take a base backup of a <productname>PostgreSQL</productname> cluster</refpurpose>
16  </refnamediv>
17
18  <indexterm zone="app-pgbasebackup">
19   <primary>pg_basebackup</primary>
20  </indexterm>
21
22  <refsynopsisdiv>
23   <cmdsynopsis>
24    <command>pg_basebackup</command>
25    <arg rep="repeat"><replaceable>option</></arg>
26   </cmdsynopsis>
27  </refsynopsisdiv>
28
29  <refsect1>
30   <title>
31    Description
32   </title>
33   <para>
34    <application>pg_basebackup</application> is used to take base backups of
35    a running <productname>PostgreSQL</productname> database cluster. These
36    are taken without affecting other clients to the database, and can be used
37    both for point-in-time recovery (see <xref linkend="continuous-archiving">)
38    and as the starting point for a log shipping or streaming replication standby
39    servers (see <xref linkend="warm-standby">).
40   </para>
41
42   <para>
43    <application>pg_basebackup</application> makes a binary copy of the database
44    cluster files, while making sure the system is automatically put in and
45    out of backup mode automatically. Backups are always taken of the entire
46    database cluster, it is not possible to back up individual databases or
47    database objects. For individual database backups, a tool such as
48    <xref linkend="APP-PGDUMP"> must be used.
49   </para>
50
51   <para>
52    The backup is made over a regular <productname>PostgreSQL</productname>
53    connection, and uses the replication protocol. The connection must be made
54    with a superuser or a user having <literal>REPLICATION</literal>
55    permissions (see <xref linkend="role-attributes">),
56    and <filename>pg_hba.conf</filename> must explicitly permit the replication
57    connection. The server must also be configured
58    with <xref linkend="guc-max-wal-senders"> set high enough to leave at least
59    one session available for the backup.
60   </para>
61
62   <para>
63    There can be multiple <command>pg_basebackup</command>s running at the same time, but it is
64    better from a performance point of view to take only one backup, and copy
65    the result.
66   </para>
67
68   <para>
69    <application>pg_basebackup</application> can make a base backup from
70    not only the master but also the standby. To take a backup from the standby,
71    set up the standby so that it can accept replication connections (that is, set
72    <varname>max_wal_senders</> and <xref linkend="guc-hot-standby">,
73    and configure <link linkend="auth-pg-hba-conf">host-based authentication</link>).
74    You will also need to enable <xref linkend="guc-full-page-writes"> on the master.
75   </para>
76
77   <para>
78    Note that there are some limitations in an online backup from the standby:
79
80    <itemizedlist>
81     <listitem>
82      <para>
83       The backup history file is not created in the database cluster backed up.
84      </para>
85     </listitem>
86     <listitem>
87      <para>
88       There is no guarantee that all WAL files required for the backup are archived
89       at the end of backup. If you are planning to use the backup for an archive
90       recovery and want to ensure that all required files are available at that moment,
91       you need to include them into the backup by using <literal>-x</> option.
92      </para>
93     </listitem>
94     <listitem>
95      <para>
96       If the standby is promoted to the master during online backup, the backup fails.
97      </para>
98     </listitem>
99     <listitem>
100      <para>
101       All WAL records required for the backup must contain sufficient full-page writes,
102       which requires you to enable <varname>full_page_writes</> on the master and
103       not to use a tool like <application>pg_compresslog</> as
104       <varname>archive_command</> to remove full-page writes from WAL files.
105      </para>
106     </listitem>
107    </itemizedlist>
108   </para>
109  </refsect1>
110
111  <refsect1>
112   <title>Options</title>
113
114    <para>
115     The following command-line options control the location and format of the
116     output.
117
118     <variablelist>
119      <varlistentry>
120       <term><option>-D <replaceable class="parameter">directory</replaceable></option></term>
121       <term><option>--pgdata=<replaceable class="parameter">directory</replaceable></option></term>
122       <listitem>
123        <para>
124         Directory to write the output to.
125         <application>pg_basebackup</application> will create the directory and
126         any parent directories if necessary.  The directory may already exist,
127         but it is an error if the directory already exists and is not empty.
128        </para>
129        <para>
130         When the backup is in tar mode, and the directory is specified as
131         <literal>-</literal> (dash), the tar file will be written to
132         <literal>stdout</literal>.
133        </para>
134        <para>
135         This option is required.
136        </para>
137       </listitem>
138      </varlistentry>
139
140      <varlistentry>
141       <term><option>-F <replaceable class="parameter">format</replaceable></option></term>
142       <term><option>--format=<replaceable class="parameter">format</replaceable></option></term>
143       <listitem>
144        <para>
145         Selects the format for the output. <replaceable>format</replaceable>
146         can be one of the following:
147
148         <variablelist>
149          <varlistentry>
150           <term><literal>p</literal></term>
151           <term><literal>plain</literal></term>
152           <listitem>
153            <para>
154             Write the output as plain files, with the same layout as the
155             current data directory and tablespaces. When the cluster has
156             no additional tablespaces, the whole database will be placed in
157             the target directory. If the cluster contains additional
158             tablespaces, the main data directory will be placed in the
159             target directory, but all other tablespaces will be placed
160             in the same absolute path as they have on the server.
161            </para>
162            <para>
163             This is the default format.
164            </para>
165           </listitem>
166          </varlistentry>
167
168          <varlistentry>
169           <term><literal>t</literal></term>
170           <term><literal>tar</literal></term>
171           <listitem>
172            <para>
173             Write the output as tar files in the target directory. The main
174             data directory will be written to a file named
175             <filename>base.tar</filename>, and all other tablespaces will
176             be named after the tablespace OID.
177             </para>
178            <para>
179             If the value <literal>-</literal> (dash) is specified as
180             target directory, the tar contents will be written to
181             standard output, suitable for piping to for example
182             <productname>gzip</productname>. This is only possible if
183             the cluster has no additional tablespaces.
184            </para>
185            </listitem>
186          </varlistentry>
187         </variablelist></para>
188       </listitem>
189      </varlistentry>
190
191      <varlistentry>
192       <term><option>-R</option></term>
193       <term><option>--write-recovery-conf</option></term>
194       <listitem>
195
196        <para>
197         Write a minimal <filename>recovery.conf</filename> in the output directory (or into
198         the base archive file when using tar format) to ease setting
199         up a standby server.
200        </para>
201
202       </listitem>
203      </varlistentry>
204
205      <varlistentry>
206       <term><option>--xlogdir=<replaceable class="parameter">xlogdir</replaceable></option></term>
207       <listitem>
208        <para>
209         Specifies the location for the transaction log directory. 
210         <replaceable>xlogdir</replaceable> must be an absolute path.
211         The transaction log directory can only be specified when
212         the backup is in plain mode.
213        </para>
214       </listitem>
215      </varlistentry>
216
217      <varlistentry>
218       <term><option>-x</option></term>
219       <term><option>--xlog</option></term>
220       <listitem>
221        <para>
222         Using this option is equivalent of using <literal>-X</literal> with
223         method <literal>fetch</literal>.
224        </para>
225       </listitem>
226      </varlistentry>
227
228      <varlistentry>
229       <term><option>-X <replaceable class="parameter">method</replaceable></option></term>
230       <term><option>--xlog-method=<replaceable class="parameter">method</replaceable></option></term>
231       <listitem>
232        <para>
233         Includes the required transaction log files (WAL files) in the
234         backup. This will include all transaction logs generated during
235         the backup. If this option is specified, it is possible to start
236         a postmaster directly in the extracted directory without the need
237         to consult the log archive, thus making this a completely standalone
238         backup.
239        </para>
240        <para>
241         The following methods for collecting the transaction logs are
242         supported:
243
244         <variablelist>
245          <varlistentry>
246           <term><literal>f</literal></term>
247           <term><literal>fetch</literal></term>
248           <listitem>
249            <para>
250             The transaction log files are collected at the end of the backup.
251             Therefore, it is necessary for the
252             <xref linkend="guc-wal-keep-segments"> parameter to be set high
253              enough that the log is not removed before the end of the backup.
254              If the log has been rotated when it's time to transfer it, the
255              backup will fail and be unusable.
256            </para>
257           </listitem>
258          </varlistentry>
259
260          <varlistentry>
261           <term><literal>s</literal></term>
262           <term><literal>stream</literal></term>
263           <listitem>
264            <para>
265             Stream the transaction log while the backup is created. This will
266             open a second connection to the server and start streaming the
267             transaction log in parallel while running the backup. Therefore,
268             it will use up two slots configured by the
269             <xref linkend="guc-max-wal-senders"> parameter. As long as the
270              client can keep up with transaction log received, using this mode
271              requires no extra transaction logs to be saved on the master.
272            </para>
273           </listitem>
274          </varlistentry>
275         </variablelist>
276        </para>
277       </listitem>
278      </varlistentry>
279
280      <varlistentry>
281       <term><option>-z</option></term>
282       <term><option>--gzip</option></term>
283       <listitem>
284        <para>
285         Enables gzip compression of tar file output, with the default
286         compression level. Compression is only available when using
287         the tar format.
288        </para>
289       </listitem>
290      </varlistentry>
291
292      <varlistentry>
293       <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
294       <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
295       <listitem>
296        <para>
297         Enables gzip compression of tar file output, and specifies the
298         compression level (1 through 9, 9 being best
299         compression). Compression is only available when using the tar
300         format.
301        </para>
302       </listitem>
303      </varlistentry>
304     </variablelist>
305    </para>
306    <para>
307     The following command-line options control the generation of the
308     backup and the running of the program.
309
310     <variablelist>
311      <varlistentry>
312       <term><option>-c <replaceable class="parameter">fast|spread</replaceable></option></term>
313       <term><option>--checkpoint=<replaceable class="parameter">fast|spread</replaceable></option></term>
314       <listitem>
315        <para>
316         Sets checkpoint mode to fast or spread (default).
317        </para>
318       </listitem>
319      </varlistentry>
320
321      <varlistentry>
322       <term><option>-l <replaceable class="parameter">label</replaceable></option></term>
323       <term><option>--label=<replaceable class="parameter">label</replaceable></option></term>
324       <listitem>
325        <para>
326         Sets the label for the backup. If none is specified, a default value of
327         <quote><literal>pg_basebackup base backup</literal></quote> will be used.
328        </para>
329       </listitem>
330      </varlistentry>
331
332      <varlistentry>
333       <term><option>-P</option></term>
334       <term><option>--progress</option></term>
335       <listitem>
336        <para>
337         Enables progress reporting. Turning this on will deliver an approximate
338         progress report during the backup. Since the database may change during
339         the backup, this is only an approximation and may not end at exactly
340         <literal>100%</literal>. In particular, when WAL log is included in the
341         backup, the total amount of data cannot be estimated in advance, and
342         in this case the estimated target size will increase once it passes the
343         total estimate without WAL.
344        </para>
345        <para>
346         When this is enabled, the backup will start by enumerating the size of
347         the entire database, and then go back and send the actual contents.
348         This may make the backup take slightly longer, and in particular it
349         will take longer before the first data is sent.
350        </para>
351       </listitem>
352      </varlistentry>
353
354      <varlistentry>
355       <term><option>-v</option></term>
356       <term><option>--verbose</option></term>
357       <listitem>
358        <para>
359         Enables verbose mode. Will output some extra steps during startup and
360         shutdown, as well as show the exact file name that is currently being
361         processed if progress reporting is also enabled.
362        </para>
363       </listitem>
364      </varlistentry>
365
366     </variablelist>
367    </para>
368
369    <para>
370     The following command-line options control the database connection parameters.
371
372     <variablelist>
373      <varlistentry>
374       <term><option>-d <replaceable class="parameter">connstr</replaceable></option></term>
375       <term><option>--dbname=<replaceable class="parameter">connstr</replaceable></option></term>
376       <listitem>
377        <para>
378         Specifies parameters used to connect to the server, as a connection
379         string. See <xref linkend="libpq-connstring"> for more information.
380        </para>
381        <para>
382         The option is called <literal>--dbname</> for consistency with other
383         client applications, but because <application>pg_basebackup</application>
384         doesn't connect to any particular database in the cluster, database
385         name in the connection string will be ignored.
386        </para>
387       </listitem>
388      </varlistentry>
389
390      <varlistentry>
391       <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
392       <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
393       <listitem>
394        <para>
395         Specifies the host name of the machine on which the server is
396         running.  If the value begins with a slash, it is used as the
397         directory for the Unix domain socket. The default is taken
398         from the <envar>PGHOST</envar> environment variable, if set,
399         else a Unix domain socket connection is attempted.
400        </para>
401       </listitem>
402      </varlistentry>
403
404      <varlistentry>
405       <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
406       <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
407       <listitem>
408        <para>
409         Specifies the TCP port or local Unix domain socket file
410         extension on which the server is listening for connections.
411         Defaults to the <envar>PGPORT</envar> environment variable, if
412         set, or a compiled-in default.
413        </para>
414       </listitem>
415      </varlistentry>
416
417      <varlistentry>
418       <term><option>-s <replaceable class="parameter">interval</replaceable></option></term>
419       <term><option>--status-interval=<replaceable class="parameter">interval</replaceable></option></term>
420       <listitem>
421        <para>
422         Specifies the number of seconds between status packets sent back to the
423         server. This allows for easier monitoring of the progress from server.
424         A value of zero disables the periodic status updates completely,
425         although an update will still be sent when requested by the server, to
426         avoid timeout disconnect. The default value is 10 seconds.
427        </para>
428       </listitem>
429      </varlistentry>
430
431      <varlistentry>
432       <term><option>-U <replaceable>username</replaceable></option></term>
433       <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
434       <listitem>
435        <para>
436         User name to connect as.
437        </para>
438       </listitem>
439      </varlistentry>
440
441      <varlistentry>
442       <term><option>-w</></term>
443       <term><option>--no-password</></term>
444       <listitem>
445        <para>
446         Never issue a password prompt.  If the server requires
447         password authentication and a password is not available by
448         other means such as a <filename>.pgpass</filename> file, the
449         connection attempt will fail.  This option can be useful in
450         batch jobs and scripts where no user is present to enter a
451         password.
452        </para>
453       </listitem>
454      </varlistentry>
455
456      <varlistentry>
457       <term><option>-W</option></term>
458       <term><option>--password</option></term>
459       <listitem>
460        <para>
461         Force <application>pg_basebackup</application> to prompt for a
462         password before connecting to a database.
463        </para>
464
465        <para>
466         This option is never essential, since
467         <application>pg_basebackup</application> will automatically prompt
468         for a password if the server demands password authentication.
469         However, <application>pg_basebackup</application> will waste a
470         connection attempt finding out that the server wants a password.
471         In some cases it is worth typing <option>-W</> to avoid the extra
472         connection attempt.
473        </para>
474       </listitem>
475      </varlistentry>
476     </variablelist>
477    </para>
478
479    <para>
480     Other options are also available:
481
482     <variablelist>
483      <varlistentry>
484        <term><option>-V</></term>
485        <term><option>--version</></term>
486        <listitem>
487        <para>
488        Print the <application>pg_basebackup</application> version and exit.
489        </para>
490        </listitem>
491      </varlistentry>
492
493      <varlistentry>
494        <term><option>-?</></term>
495        <term><option>--help</></term>
496        <listitem>
497        <para>
498        Show help about <application>pg_basebackup</application> command line
499        arguments, and exit.
500        </para>
501        </listitem>
502      </varlistentry>
503
504     </variablelist>
505    </para>
506
507  </refsect1>
508
509  <refsect1>
510   <title>Environment</title>
511
512   <para>
513    This utility, like most other <productname>PostgreSQL</> utilities,
514    uses the environment variables supported by <application>libpq</>
515    (see <xref linkend="libpq-envars">).
516   </para>
517
518  </refsect1>
519
520  <refsect1>
521   <title>Notes</title>
522
523   <para>
524    The backup will include all files in the data directory and tablespaces,
525    including the configuration files and any additional files placed in the
526    directory by third parties. Only regular files and directories are allowed
527    in the data directory, no symbolic links or special device files.
528   </para>
529
530   <para>
531    The way <productname>PostgreSQL</productname> manages tablespaces, the path
532    for all additional tablespaces must be identical whenever a backup is
533    restored. The main data directory, however, is relocatable to any location.
534   </para>
535
536   <para>
537    <application>pg_basebackup</application> works with servers of the same
538    or an older major version, down to 9.1. However, WAL streaming mode (-X
539    stream) only works with server version 9.3.
540   </para>
541  </refsect1>
542
543  <refsect1>
544   <title>Examples</title>
545
546   <para>
547    To create a base backup of the server at <literal>mydbserver</literal>
548    and store it in the local directory
549    <filename>/usr/local/pgsql/data</filename>:
550 <screen>
551 <prompt>$</prompt> <userinput>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</userinput>
552 </screen>
553   </para>
554
555   <para>
556    To create a backup of the local server with one compressed
557    tar file for each tablespace, and store it in the directory
558    <filename>backup</filename>, showing a progress report while running:
559 <screen>
560 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft -z -P</userinput>
561 </screen>
562   </para>
563
564   <para>
565    To create a backup of a single-tablespace local database and compress
566    this with <productname>bzip2</productname>:
567 <screen>
568 <prompt>$</prompt> <userinput>pg_basebackup -D - -Ft | bzip2 &gt; backup.tar.bz2</userinput>
569 </screen>
570    (This command will fail if there are multiple tablespaces in the
571    database.)
572   </para>
573  </refsect1>
574
575  <refsect1>
576   <title>See Also</title>
577
578   <simplelist type="inline">
579    <member><xref linkend="APP-PGDUMP"></member>
580   </simplelist>
581  </refsect1>
582
583 </refentry>