]> granicus.if.org Git - postgresql/blob - doc/src/sgml/ref/pg_basebackup.sgml
eb0c1d6f36e50af3f0018c1ebcc1f7acded69bfa
[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>-x</option></term>
207       <term><option>--xlog</option></term>
208       <listitem>
209        <para>
210         Using this option is equivalent of using <literal>-X</literal> with
211         method <literal>fetch</literal>.
212        </para>
213       </listitem>
214      </varlistentry>
215
216      <varlistentry>
217       <term><option>-X <replaceable class="parameter">method</replaceable></option></term>
218       <term><option>--xlog-method=<replaceable class="parameter">method</replaceable></option></term>
219       <listitem>
220        <para>
221         Includes the required transaction log files (WAL files) in the
222         backup. This will include all transaction logs generated during
223         the backup. If this option is specified, it is possible to start
224         a postmaster directly in the extracted directory without the need
225         to consult the log archive, thus making this a completely standalone
226         backup.
227        </para>
228        <para>
229         The following methods for collecting the transaction logs are
230         supported:
231
232         <variablelist>
233          <varlistentry>
234           <term><literal>f</literal></term>
235           <term><literal>fetch</literal></term>
236           <listitem>
237            <para>
238             The transaction log files are collected at the end of the backup.
239             Therefore, it is necessary for the
240             <xref linkend="guc-wal-keep-segments"> parameter to be set high
241              enough that the log is not removed before the end of the backup.
242              If the log has been rotated when it's time to transfer it, the
243              backup will fail and be unusable.
244            </para>
245           </listitem>
246          </varlistentry>
247
248          <varlistentry>
249           <term><literal>s</literal></term>
250           <term><literal>stream</literal></term>
251           <listitem>
252            <para>
253             Stream the transaction log while the backup is created. This will
254             open a second connection to the server and start streaming the
255             transaction log in parallel while running the backup. Therefore,
256             it will use up two slots configured by the
257             <xref linkend="guc-max-wal-senders"> parameter. As long as the
258              client can keep up with transaction log received, using this mode
259              requires no extra transaction logs to be saved on the master.
260            </para>
261           </listitem>
262          </varlistentry>
263         </variablelist>
264        </para>
265       </listitem>
266      </varlistentry>
267
268      <varlistentry>
269       <term><option>-z</option></term>
270       <term><option>--gzip</option></term>
271       <listitem>
272        <para>
273         Enables gzip compression of tar file output, with the default
274         compression level. Compression is only available when using
275         the tar format.
276        </para>
277       </listitem>
278      </varlistentry>
279
280      <varlistentry>
281       <term><option>-Z <replaceable class="parameter">level</replaceable></option></term>
282       <term><option>--compress=<replaceable class="parameter">level</replaceable></option></term>
283       <listitem>
284        <para>
285         Enables gzip compression of tar file output, and specifies the
286         compression level (1 through 9, 9 being best
287         compression). Compression is only available when using the tar
288         format.
289        </para>
290       </listitem>
291      </varlistentry>
292     </variablelist>
293    </para>
294    <para>
295     The following command-line options control the generation of the
296     backup and the running of the program.
297
298     <variablelist>
299      <varlistentry>
300       <term><option>-c <replaceable class="parameter">fast|spread</replaceable></option></term>
301       <term><option>--checkpoint=<replaceable class="parameter">fast|spread</replaceable></option></term>
302       <listitem>
303        <para>
304         Sets checkpoint mode to fast or spread (default).
305        </para>
306       </listitem>
307      </varlistentry>
308
309      <varlistentry>
310       <term><option>-l <replaceable class="parameter">label</replaceable></option></term>
311       <term><option>--label=<replaceable class="parameter">label</replaceable></option></term>
312       <listitem>
313        <para>
314         Sets the label for the backup. If none is specified, a default value of
315         <quote><literal>pg_basebackup base backup</literal></quote> will be used.
316        </para>
317       </listitem>
318      </varlistentry>
319
320      <varlistentry>
321       <term><option>-P</option></term>
322       <term><option>--progress</option></term>
323       <listitem>
324        <para>
325         Enables progress reporting. Turning this on will deliver an approximate
326         progress report during the backup. Since the database may change during
327         the backup, this is only an approximation and may not end at exactly
328         <literal>100%</literal>. In particular, when WAL log is included in the
329         backup, the total amount of data cannot be estimated in advance, and
330         in this case the estimated target size will increase once it passes the
331         total estimate without WAL.
332        </para>
333        <para>
334         When this is enabled, the backup will start by enumerating the size of
335         the entire database, and then go back and send the actual contents.
336         This may make the backup take slightly longer, and in particular it
337         will take longer before the first data is sent.
338        </para>
339       </listitem>
340      </varlistentry>
341
342      <varlistentry>
343       <term><option>-v</option></term>
344       <term><option>--verbose</option></term>
345       <listitem>
346        <para>
347         Enables verbose mode. Will output some extra steps during startup and
348         shutdown, as well as show the exact file name that is currently being
349         processed if progress reporting is also enabled.
350        </para>
351       </listitem>
352      </varlistentry>
353
354     </variablelist>
355    </para>
356
357    <para>
358     The following command-line options control the database connection parameters.
359
360     <variablelist>
361      <varlistentry>
362       <term><option>-d <replaceable class="parameter">connstr</replaceable></option></term>
363       <term><option>--dbname=<replaceable class="parameter">connstr</replaceable></option></term>
364       <listitem>
365        <para>
366         Specifies parameters used to connect to the server, as a connection
367         string. See <xref linkend="libpq-connstring"> for more information.
368        </para>
369        <para>
370         The option is called <literal>--dbname</> for consistency with other
371         client applications, but because <application>pg_basebackup</application>
372         doesn't connect to any particular database in the cluster, database
373         name in the connection string will be ignored.
374        </para>
375       </listitem>
376      </varlistentry>
377
378      <varlistentry>
379       <term><option>-h <replaceable class="parameter">host</replaceable></option></term>
380       <term><option>--host=<replaceable class="parameter">host</replaceable></option></term>
381       <listitem>
382        <para>
383         Specifies the host name of the machine on which the server is
384         running.  If the value begins with a slash, it is used as the
385         directory for the Unix domain socket. The default is taken
386         from the <envar>PGHOST</envar> environment variable, if set,
387         else a Unix domain socket connection is attempted.
388        </para>
389       </listitem>
390      </varlistentry>
391
392      <varlistentry>
393       <term><option>-p <replaceable class="parameter">port</replaceable></option></term>
394       <term><option>--port=<replaceable class="parameter">port</replaceable></option></term>
395       <listitem>
396        <para>
397         Specifies the TCP port or local Unix domain socket file
398         extension on which the server is listening for connections.
399         Defaults to the <envar>PGPORT</envar> environment variable, if
400         set, or a compiled-in default.
401        </para>
402       </listitem>
403      </varlistentry>
404
405      <varlistentry>
406       <term><option>-s <replaceable class="parameter">interval</replaceable></option></term>
407       <term><option>--status-interval=<replaceable class="parameter">interval</replaceable></option></term>
408       <listitem>
409        <para>
410         Specifies the number of seconds between status packets sent back to the
411         server. This allows for easier monitoring of the progress from server.
412         A value of zero disables the periodic status updates completely,
413         although an update will still be sent when requested by the server, to
414         avoid timeout disconnect. The default value is 10 seconds.
415        </para>
416       </listitem>
417      </varlistentry>
418
419      <varlistentry>
420       <term><option>-U <replaceable>username</replaceable></option></term>
421       <term><option>--username=<replaceable class="parameter">username</replaceable></option></term>
422       <listitem>
423        <para>
424         User name to connect as.
425        </para>
426       </listitem>
427      </varlistentry>
428
429      <varlistentry>
430       <term><option>-w</></term>
431       <term><option>--no-password</></term>
432       <listitem>
433        <para>
434         Never issue a password prompt.  If the server requires
435         password authentication and a password is not available by
436         other means such as a <filename>.pgpass</filename> file, the
437         connection attempt will fail.  This option can be useful in
438         batch jobs and scripts where no user is present to enter a
439         password.
440        </para>
441       </listitem>
442      </varlistentry>
443
444      <varlistentry>
445       <term><option>-W</option></term>
446       <term><option>--password</option></term>
447       <listitem>
448        <para>
449         Force <application>pg_basebackup</application> to prompt for a
450         password before connecting to a database.
451        </para>
452
453        <para>
454         This option is never essential, since
455         <application>pg_basebackup</application> will automatically prompt
456         for a password if the server demands password authentication.
457         However, <application>pg_basebackup</application> will waste a
458         connection attempt finding out that the server wants a password.
459         In some cases it is worth typing <option>-W</> to avoid the extra
460         connection attempt.
461        </para>
462       </listitem>
463      </varlistentry>
464     </variablelist>
465    </para>
466
467    <para>
468     Other options are also available:
469
470     <variablelist>
471      <varlistentry>
472        <term><option>-V</></term>
473        <term><option>--version</></term>
474        <listitem>
475        <para>
476        Print the <application>pg_basebackup</application> version and exit.
477        </para>
478        </listitem>
479      </varlistentry>
480
481      <varlistentry>
482        <term><option>-?</></term>
483        <term><option>--help</></term>
484        <listitem>
485        <para>
486        Show help about <application>pg_basebackup</application> command line
487        arguments, and exit.
488        </para>
489        </listitem>
490      </varlistentry>
491
492     </variablelist>
493    </para>
494
495  </refsect1>
496
497  <refsect1>
498   <title>Environment</title>
499
500   <para>
501    This utility, like most other <productname>PostgreSQL</> utilities,
502    uses the environment variables supported by <application>libpq</>
503    (see <xref linkend="libpq-envars">).
504   </para>
505
506  </refsect1>
507
508  <refsect1>
509   <title>Notes</title>
510
511   <para>
512    The backup will include all files in the data directory and tablespaces,
513    including the configuration files and any additional files placed in the
514    directory by third parties. Only regular files and directories are allowed
515    in the data directory, no symbolic links or special device files.
516   </para>
517
518   <para>
519    The way <productname>PostgreSQL</productname> manages tablespaces, the path
520    for all additional tablespaces must be identical whenever a backup is
521    restored. The main data directory, however, is relocatable to any location.
522   </para>
523
524   <para>
525    <application>pg_basebackup</application> works with servers of the same
526    or an older major version, down to 9.1. However, WAL streaming mode (-X
527    stream) only works with server version 9.3.
528   </para>
529  </refsect1>
530
531  <refsect1>
532   <title>Examples</title>
533
534   <para>
535    To create a base backup of the server at <literal>mydbserver</literal>
536    and store it in the local directory
537    <filename>/usr/local/pgsql/data</filename>:
538 <screen>
539 <prompt>$</prompt> <userinput>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</userinput>
540 </screen>
541   </para>
542
543   <para>
544    To create a backup of the local server with one compressed
545    tar file for each tablespace, and store it in the directory
546    <filename>backup</filename>, showing a progress report while running:
547 <screen>
548 <prompt>$</prompt> <userinput>pg_basebackup -D backup -Ft -z -P</userinput>
549 </screen>
550   </para>
551
552   <para>
553    To create a backup of a single-tablespace local database and compress
554    this with <productname>bzip2</productname>:
555 <screen>
556 <prompt>$</prompt> <userinput>pg_basebackup -D - -Ft | bzip2 &gt; backup.tar.bz2</userinput>
557 </screen>
558    (This command will fail if there are multiple tablespaces in the
559    database.)
560   </para>
561  </refsect1>
562
563  <refsect1>
564   <title>See Also</title>
565
566   <simplelist type="inline">
567    <member><xref linkend="APP-PGDUMP"></member>
568   </simplelist>
569  </refsect1>
570
571 </refentry>