gooderp18绿色标准版
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

406 line
33KB

  1. <?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>pg_basebackup</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets V1.79.1" /><link rel="prev" href="app-ecpg.html" title="ecpg" /><link rel="next" href="pgbench.html" title="pgbench" /></head><body><div xmlns="http://www.w3.org/TR/xhtml1/transitional" class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">pg_basebackup</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="app-ecpg.html" title="ecpg">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="reference-client.html" title="PostgreSQL Client Applications">Up</a></td><th width="60%" align="center">PostgreSQL Client Applications</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 12.4 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="pgbench.html" title="pgbench">Next</a></td></tr></table><hr></hr></div><div class="refentry" id="APP-PGBASEBACKUP"><div class="titlepage"></div><a id="id-1.9.4.9.1" class="indexterm"></a><div class="refnamediv"><h2><span class="refentrytitle">pg_basebackup</span></h2><p>pg_basebackup — take a base backup of a <span class="productname">PostgreSQL</span> cluster</p></div><div class="refsynopsisdiv"><h2>Synopsis</h2><div class="cmdsynopsis"><p id="id-1.9.4.9.4.1"><code class="command">pg_basebackup</code> [<em class="replaceable"><code>option</code></em>...]</p></div></div><div class="refsect1" id="id-1.9.4.9.5"><h2>
  3. Description
  4. </h2><p>
  5. <span class="application">pg_basebackup</span> is used to take base backups of
  6. a running <span class="productname">PostgreSQL</span> database cluster. These
  7. are taken without affecting other clients to the database, and can be used
  8. both for point-in-time recovery (see <a class="xref" href="continuous-archiving.html" title="25.3. Continuous Archiving and Point-in-Time Recovery (PITR)">Section 25.3</a>)
  9. and as the starting point for a log shipping or streaming replication standby
  10. servers (see <a class="xref" href="warm-standby.html" title="26.2. Log-Shipping Standby Servers">Section 26.2</a>).
  11. </p><p>
  12. <span class="application">pg_basebackup</span> makes a binary copy of the database
  13. cluster files, while making sure the system is put in and
  14. out of backup mode automatically. Backups are always taken of the entire
  15. database cluster; it is not possible to back up individual databases or
  16. database objects. For individual database backups, a tool such as
  17. <a class="xref" href="app-pgdump.html" title="pg_dump"><span class="refentrytitle">pg_dump</span></a> must be used.
  18. </p><p>
  19. The backup is made over a regular <span class="productname">PostgreSQL</span>
  20. connection, and uses the replication protocol. The connection must be made
  21. with a superuser or a user having <code class="literal">REPLICATION</code>
  22. permissions (see <a class="xref" href="role-attributes.html" title="21.2. Role Attributes">Section 21.2</a>),
  23. and <code class="filename">pg_hba.conf</code> must explicitly permit the replication
  24. connection. The server must also be configured
  25. with <a class="xref" href="runtime-config-replication.html#GUC-MAX-WAL-SENDERS">max_wal_senders</a> set high enough to leave at least
  26. one session available for the backup and one for WAL streaming (if used).
  27. </p><p>
  28. There can be multiple <code class="command">pg_basebackup</code>s running at the same time, but it is
  29. better from a performance point of view to take only one backup, and copy
  30. the result.
  31. </p><p>
  32. <span class="application">pg_basebackup</span> can make a base backup from
  33. not only the master but also the standby. To take a backup from the standby,
  34. set up the standby so that it can accept replication connections (that is, set
  35. <code class="varname">max_wal_senders</code> and <a class="xref" href="runtime-config-replication.html#GUC-HOT-STANDBY">hot_standby</a>,
  36. and configure <a class="link" href="auth-pg-hba-conf.html" title="20.1. The pg_hba.conf File">host-based authentication</a>).
  37. You will also need to enable <a class="xref" href="runtime-config-wal.html#GUC-FULL-PAGE-WRITES">full_page_writes</a> on the master.
  38. </p><p>
  39. Note that there are some limitations in an online backup from the standby:
  40. </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
  41. The backup history file is not created in the database cluster backed up.
  42. </p></li><li class="listitem"><p>
  43. If you are using <code class="literal">-X none</code>, there is no guarantee that all
  44. WAL files required for the backup are archived at the end of backup.
  45. </p></li><li class="listitem"><p>
  46. If the standby is promoted to the master during online backup, the backup fails.
  47. </p></li><li class="listitem"><p>
  48. All WAL records required for the backup must contain sufficient full-page writes,
  49. which requires you to enable <code class="varname">full_page_writes</code> on the master and
  50. not to use a tool like <span class="application">pg_compresslog</span> as
  51. <code class="varname">archive_command</code> to remove full-page writes from WAL files.
  52. </p></li></ul></div><p>
  53. </p></div><div class="refsect1" id="id-1.9.4.9.6"><h2>Options</h2><p>
  54. The following command-line options control the location and format of the
  55. output.
  56. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-D <em class="replaceable"><code>directory</code></em></code><br /></span><span class="term"><code class="option">--pgdata=<em class="replaceable"><code>directory</code></em></code></span></dt><dd><p>
  57. Directory to write the output to.
  58. <span class="application">pg_basebackup</span> will create the directory and
  59. any parent directories if necessary. The directory may already exist,
  60. but it is an error if the directory already exists and is not empty.
  61. </p><p>
  62. When the backup is in tar mode, and the directory is specified as
  63. <code class="literal">-</code> (dash), the tar file will be written to
  64. <code class="literal">stdout</code>.
  65. </p><p>
  66. This option is required.
  67. </p></dd><dt><span class="term"><code class="option">-F <em class="replaceable"><code>format</code></em></code><br /></span><span class="term"><code class="option">--format=<em class="replaceable"><code>format</code></em></code></span></dt><dd><p>
  68. Selects the format for the output. <em class="replaceable"><code>format</code></em>
  69. can be one of the following:
  70. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">p</code><br /></span><span class="term"><code class="literal">plain</code></span></dt><dd><p>
  71. Write the output as plain files, with the same layout as the
  72. current data directory and tablespaces. When the cluster has
  73. no additional tablespaces, the whole database will be placed in
  74. the target directory. If the cluster contains additional
  75. tablespaces, the main data directory will be placed in the
  76. target directory, but all other tablespaces will be placed
  77. in the same absolute path as they have on the server.
  78. </p><p>
  79. This is the default format.
  80. </p></dd><dt><span class="term"><code class="literal">t</code><br /></span><span class="term"><code class="literal">tar</code></span></dt><dd><p>
  81. Write the output as tar files in the target directory. The main
  82. data directory will be written to a file named
  83. <code class="filename">base.tar</code>, and all other tablespaces will
  84. be named after the tablespace OID.
  85. </p><p>
  86. If the value <code class="literal">-</code> (dash) is specified as
  87. target directory, the tar contents will be written to
  88. standard output, suitable for piping to for example
  89. <span class="productname">gzip</span>. This is only possible if
  90. the cluster has no additional tablespaces and WAL
  91. streaming is not used.
  92. </p></dd></dl></div></dd><dt><span class="term"><code class="option">-r <em class="replaceable"><code>rate</code></em></code><br /></span><span class="term"><code class="option">--max-rate=<em class="replaceable"><code>rate</code></em></code></span></dt><dd><p>
  93. The maximum transfer rate of data transferred from the server. Values are
  94. in kilobytes per second. Use a suffix of <code class="literal">M</code> to indicate megabytes
  95. per second. A suffix of <code class="literal">k</code> is also accepted, and has no effect.
  96. Valid values are between 32 kilobytes per second and 1024 megabytes per second.
  97. </p><p>
  98. The purpose is to limit the impact of <span class="application">pg_basebackup</span>
  99. on the running server.
  100. </p><p>
  101. This option always affects transfer of the data directory. Transfer of
  102. WAL files is only affected if the collection method is <code class="literal">fetch</code>.
  103. </p></dd><dt><span class="term"><code class="option">-R</code><br /></span><span class="term"><code class="option">--write-recovery-conf</code></span></dt><dd><p>
  104. Create <code class="filename">standby.signal</code> and append connection settings
  105. to <code class="filename">postgresql.auto.conf</code> in the output
  106. directory (or into the base archive file when using tar format) to
  107. ease setting up a standby server.
  108. The <code class="filename">postgresql.auto.conf</code> file will record the connection
  109. settings and, if specified, the replication slot
  110. that <span class="application">pg_basebackup</span> is using, so that the
  111. streaming replication will use the same settings later on.
  112. </p></dd><dt><span class="term"><code class="option">-T <em class="replaceable"><code>olddir</code></em>=<em class="replaceable"><code>newdir</code></em></code><br /></span><span class="term"><code class="option">--tablespace-mapping=<em class="replaceable"><code>olddir</code></em>=<em class="replaceable"><code>newdir</code></em></code></span></dt><dd><p>
  113. Relocate the tablespace in directory <em class="replaceable"><code>olddir</code></em>
  114. to <em class="replaceable"><code>newdir</code></em> during the backup. To be
  115. effective, <em class="replaceable"><code>olddir</code></em> must exactly match the
  116. path specification of the tablespace as it is currently defined. (But
  117. it is not an error if there is no tablespace
  118. in <em class="replaceable"><code>olddir</code></em> contained in the backup.)
  119. Both <em class="replaceable"><code>olddir</code></em>
  120. and <em class="replaceable"><code>newdir</code></em> must be absolute paths. If a
  121. path happens to contain a <code class="literal">=</code> sign, escape it with a
  122. backslash. This option can be specified multiple times for multiple
  123. tablespaces. See examples below.
  124. </p><p>
  125. If a tablespace is relocated in this way, the symbolic links inside
  126. the main data directory are updated to point to the new location. So
  127. the new data directory is ready to be used for a new server instance
  128. with all tablespaces in the updated locations.
  129. </p></dd><dt><span class="term"><code class="option">--waldir=<em class="replaceable"><code>waldir</code></em></code></span></dt><dd><p>
  130. Specifies the location for the write-ahead log directory.
  131. <em class="replaceable"><code>waldir</code></em> must be an absolute path.
  132. The write-ahead log directory can only be specified when
  133. the backup is in plain mode.
  134. </p></dd><dt><span class="term"><code class="option">-X <em class="replaceable"><code>method</code></em></code><br /></span><span class="term"><code class="option">--wal-method=<em class="replaceable"><code>method</code></em></code></span></dt><dd><p>
  135. Includes the required write-ahead log files (WAL files) in the
  136. backup. This will include all write-ahead logs generated during
  137. the backup. Unless the method <code class="literal">none</code> is specified,
  138. it is possible to start a postmaster directly in the extracted
  139. directory without the need to consult the log archive, thus
  140. making this a completely standalone backup.
  141. </p><p>
  142. The following methods for collecting the write-ahead logs are
  143. supported:
  144. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="literal">n</code><br /></span><span class="term"><code class="literal">none</code></span></dt><dd><p>
  145. Don't include write-ahead log in the backup.
  146. </p></dd><dt><span class="term"><code class="literal">f</code><br /></span><span class="term"><code class="literal">fetch</code></span></dt><dd><p>
  147. The write-ahead log files are collected at the end of the backup.
  148. Therefore, it is necessary for the
  149. <a class="xref" href="runtime-config-replication.html#GUC-WAL-KEEP-SEGMENTS">wal_keep_segments</a> parameter to be set high
  150. enough that the log is not removed before the end of the backup.
  151. If the log has been rotated when it's time to transfer it, the
  152. backup will fail and be unusable.
  153. </p><p>
  154. When tar format mode is used, the write-ahead log files will be
  155. written to the <code class="filename">base.tar</code> file.
  156. </p></dd><dt><span class="term"><code class="literal">s</code><br /></span><span class="term"><code class="literal">stream</code></span></dt><dd><p>
  157. Stream the write-ahead log while the backup is created. This will
  158. open a second connection to the server and start streaming the
  159. write-ahead log in parallel while running the backup. Therefore,
  160. it will use up two connections configured by the
  161. <a class="xref" href="runtime-config-replication.html#GUC-MAX-WAL-SENDERS">max_wal_senders</a> parameter. As long as the
  162. client can keep up with write-ahead log received, using this mode
  163. requires no extra write-ahead logs to be saved on the master.
  164. </p><p>
  165. When tar format mode is used, the write-ahead log files will be
  166. written to a separate file named <code class="filename">pg_wal.tar</code>
  167. (if the server is a version earlier than 10, the file will be named
  168. <code class="filename">pg_xlog.tar</code>).
  169. </p><p>
  170. This value is the default.
  171. </p></dd></dl></div><p>
  172. </p></dd><dt><span class="term"><code class="option">-z</code><br /></span><span class="term"><code class="option">--gzip</code></span></dt><dd><p>
  173. Enables gzip compression of tar file output, with the default
  174. compression level. Compression is only available when using
  175. the tar format, and the suffix <code class="filename">.gz</code> will
  176. automatically be added to all tar filenames.
  177. </p></dd><dt><span class="term"><code class="option">-Z <em class="replaceable"><code>level</code></em></code><br /></span><span class="term"><code class="option">--compress=<em class="replaceable"><code>level</code></em></code></span></dt><dd><p>
  178. Enables gzip compression of tar file output, and specifies the
  179. compression level (0 through 9, 0 being no compression and 9 being best
  180. compression). Compression is only available when using the tar
  181. format, and the suffix <code class="filename">.gz</code> will
  182. automatically be added to all tar filenames.
  183. </p></dd></dl></div><p>
  184. </p><p>
  185. The following command-line options control the generation of the
  186. backup and the running of the program.
  187. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-c <em class="replaceable"><code>fast|spread</code></em></code><br /></span><span class="term"><code class="option">--checkpoint=<em class="replaceable"><code>fast|spread</code></em></code></span></dt><dd><p>
  188. Sets checkpoint mode to fast (immediate) or spread (default) (see <a class="xref" href="continuous-archiving.html#BACKUP-LOWLEVEL-BASE-BACKUP" title="25.3.3. Making a Base Backup Using the Low Level API">Section 25.3.3</a>).
  189. </p></dd><dt><span class="term"><code class="option">-C</code><br /></span><span class="term"><code class="option">--create-slot</code></span></dt><dd><p>
  190. This option causes creation of a replication slot named by the
  191. <code class="literal">--slot</code> option before starting the backup.
  192. An error is raised if the slot already exists.
  193. </p></dd><dt><span class="term"><code class="option">-l <em class="replaceable"><code>label</code></em></code><br /></span><span class="term"><code class="option">--label=<em class="replaceable"><code>label</code></em></code></span></dt><dd><p>
  194. Sets the label for the backup. If none is specified, a default value of
  195. <span class="quote">“<span class="quote"><code class="literal">pg_basebackup base backup</code></span>”</span> will be used.
  196. </p></dd><dt><span class="term"><code class="option">-n</code><br /></span><span class="term"><code class="option">--no-clean</code></span></dt><dd><p>
  197. By default, when <code class="command">pg_basebackup</code> aborts with an
  198. error, it removes any directories it might have created before
  199. discovering that it cannot finish the job (for example, data directory
  200. and write-ahead log directory). This option inhibits tidying-up and is
  201. thus useful for debugging.
  202. </p><p>
  203. Note that tablespace directories are not cleaned up either way.
  204. </p></dd><dt><span class="term"><code class="option">-N</code><br /></span><span class="term"><code class="option">--no-sync</code></span></dt><dd><p>
  205. By default, <code class="command">pg_basebackup</code> will wait for all files
  206. to be written safely to disk. This option causes
  207. <code class="command">pg_basebackup</code> to return without waiting, which is
  208. faster, but means that a subsequent operating system crash can leave
  209. the base backup corrupt. Generally, this option is useful for testing
  210. but should not be used when creating a production installation.
  211. </p></dd><dt><span class="term"><code class="option">-P</code><br /></span><span class="term"><code class="option">--progress</code></span></dt><dd><p>
  212. Enables progress reporting. Turning this on will deliver an approximate
  213. progress report during the backup. Since the database may change during
  214. the backup, this is only an approximation and may not end at exactly
  215. <code class="literal">100%</code>. In particular, when WAL log is included in the
  216. backup, the total amount of data cannot be estimated in advance, and
  217. in this case the estimated target size will increase once it passes the
  218. total estimate without WAL.
  219. </p><p>
  220. When this is enabled, the backup will start by enumerating the size of
  221. the entire database, and then go back and send the actual contents.
  222. This may make the backup take slightly longer, and in particular it
  223. will take longer before the first data is sent.
  224. </p></dd><dt><span class="term"><code class="option">-S <em class="replaceable"><code>slotname</code></em></code><br /></span><span class="term"><code class="option">--slot=<em class="replaceable"><code>slotname</code></em></code></span></dt><dd><p>
  225. This option can only be used together with <code class="literal">-X
  226. stream</code>. It causes the WAL streaming to use the specified
  227. replication slot. If the base backup is intended to be used as a
  228. streaming replication standby using replication slots, it should then
  229. use the same replication slot name
  230. in <a class="xref" href="runtime-config-replication.html#GUC-PRIMARY-SLOT-NAME">primary_slot_name</a>. That way, it is ensured that
  231. the server does not remove any necessary WAL data in the time between
  232. the end of the base backup and the start of streaming replication.
  233. </p><p>
  234. The specified replication slot has to exist unless the
  235. option <code class="option">-C</code> is also used.
  236. </p><p>
  237. If this option is not specified and the server supports temporary
  238. replication slots (version 10 and later), then a temporary replication
  239. slot is automatically used for WAL streaming.
  240. </p></dd><dt><span class="term"><code class="option">-v</code><br /></span><span class="term"><code class="option">--verbose</code></span></dt><dd><p>
  241. Enables verbose mode. Will output some extra steps during startup and
  242. shutdown, as well as show the exact file name that is currently being
  243. processed if progress reporting is also enabled.
  244. </p></dd><dt><span class="term"><code class="option">--no-slot</code></span></dt><dd><p>
  245. This option prevents the creation of a temporary replication slot
  246. during the backup even if it's supported by the server.
  247. </p><p>
  248. Temporary replication slots are created by default if no slot name
  249. is given with the option <code class="option">-S</code> when using log streaming.
  250. </p><p>
  251. The main purpose of this option is to allow taking a base backup when
  252. the server is out of free replication slots. Using replication slots
  253. is almost always preferred, because it prevents needed WAL from being
  254. removed by the server during the backup.
  255. </p></dd><dt><span class="term"><code class="option">--no-verify-checksums</code></span></dt><dd><p>
  256. Disables verification of checksums, if they are enabled on the server
  257. the base backup is taken from.
  258. </p><p>
  259. By default, checksums are verified and checksum failures will result
  260. in a non-zero exit status. However, the base backup will not be
  261. removed in such a case, as if the <code class="option">--no-clean</code> option
  262. had been used. Checksum verifications failures will also be reported
  263. in the <a class="xref" href="monitoring-stats.html#PG-STAT-DATABASE-VIEW" title="Table 27.12. pg_stat_database View">pg_stat_database</a> view.
  264. </p></dd></dl></div><p>
  265. </p><p>
  266. The following command-line options control the database connection parameters.
  267. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-d <em class="replaceable"><code>connstr</code></em></code><br /></span><span class="term"><code class="option">--dbname=<em class="replaceable"><code>connstr</code></em></code></span></dt><dd><p>
  268. Specifies parameters used to connect to the server, as a connection
  269. string. See <a class="xref" href="libpq-connect.html#LIBPQ-CONNSTRING" title="33.1.1. Connection Strings">Section 33.1.1</a> for more information.
  270. </p><p>
  271. The option is called <code class="literal">--dbname</code> for consistency with other
  272. client applications, but because <span class="application">pg_basebackup</span>
  273. doesn't connect to any particular database in the cluster, database
  274. name in the connection string will be ignored.
  275. </p></dd><dt><span class="term"><code class="option">-h <em class="replaceable"><code>host</code></em></code><br /></span><span class="term"><code class="option">--host=<em class="replaceable"><code>host</code></em></code></span></dt><dd><p>
  276. Specifies the host name of the machine on which the server is
  277. running. If the value begins with a slash, it is used as the
  278. directory for the Unix domain socket. The default is taken
  279. from the <code class="envar">PGHOST</code> environment variable, if set,
  280. else a Unix domain socket connection is attempted.
  281. </p></dd><dt><span class="term"><code class="option">-p <em class="replaceable"><code>port</code></em></code><br /></span><span class="term"><code class="option">--port=<em class="replaceable"><code>port</code></em></code></span></dt><dd><p>
  282. Specifies the TCP port or local Unix domain socket file
  283. extension on which the server is listening for connections.
  284. Defaults to the <code class="envar">PGPORT</code> environment variable, if
  285. set, or a compiled-in default.
  286. </p></dd><dt><span class="term"><code class="option">-s <em class="replaceable"><code>interval</code></em></code><br /></span><span class="term"><code class="option">--status-interval=<em class="replaceable"><code>interval</code></em></code></span></dt><dd><p>
  287. Specifies the number of seconds between status packets sent back to the
  288. server. This allows for easier monitoring of the progress from server.
  289. A value of zero disables the periodic status updates completely,
  290. although an update will still be sent when requested by the server, to
  291. avoid timeout disconnect. The default value is 10 seconds.
  292. </p></dd><dt><span class="term"><code class="option">-U <em class="replaceable"><code>username</code></em></code><br /></span><span class="term"><code class="option">--username=<em class="replaceable"><code>username</code></em></code></span></dt><dd><p>
  293. User name to connect as.
  294. </p></dd><dt><span class="term"><code class="option">-w</code><br /></span><span class="term"><code class="option">--no-password</code></span></dt><dd><p>
  295. Never issue a password prompt. If the server requires
  296. password authentication and a password is not available by
  297. other means such as a <code class="filename">.pgpass</code> file, the
  298. connection attempt will fail. This option can be useful in
  299. batch jobs and scripts where no user is present to enter a
  300. password.
  301. </p></dd><dt><span class="term"><code class="option">-W</code><br /></span><span class="term"><code class="option">--password</code></span></dt><dd><p>
  302. Force <span class="application">pg_basebackup</span> to prompt for a
  303. password before connecting to a database.
  304. </p><p>
  305. This option is never essential, since
  306. <span class="application">pg_basebackup</span> will automatically prompt
  307. for a password if the server demands password authentication.
  308. However, <span class="application">pg_basebackup</span> will waste a
  309. connection attempt finding out that the server wants a password.
  310. In some cases it is worth typing <code class="option">-W</code> to avoid the extra
  311. connection attempt.
  312. </p></dd></dl></div><p>
  313. </p><p>
  314. Other options are also available:
  315. </p><div class="variablelist"><dl class="variablelist"><dt><span class="term"><code class="option">-V</code><br /></span><span class="term"><code class="option">--version</code></span></dt><dd><p>
  316. Print the <span class="application">pg_basebackup</span> version and exit.
  317. </p></dd><dt><span class="term"><code class="option">-?</code><br /></span><span class="term"><code class="option">--help</code></span></dt><dd><p>
  318. Show help about <span class="application">pg_basebackup</span> command line
  319. arguments, and exit.
  320. </p></dd></dl></div><p>
  321. </p></div><div class="refsect1" id="id-1.9.4.9.7"><h2>Environment</h2><p>
  322. This utility, like most other <span class="productname">PostgreSQL</span> utilities,
  323. uses the environment variables supported by <span class="application">libpq</span>
  324. (see <a class="xref" href="libpq-envars.html" title="33.14. Environment Variables">Section 33.14</a>).
  325. </p><p>
  326. The environment variable <code class="envar">PG_COLOR</code> specifies whether to use
  327. color in diagnostic messages. Possible values are
  328. <code class="literal">always</code>, <code class="literal">auto</code> and
  329. <code class="literal">never</code>.
  330. </p></div><div class="refsect1" id="id-1.9.4.9.8"><h2>Notes</h2><p>
  331. At the beginning of the backup, a checkpoint needs to be written on the
  332. server the backup is taken from. Especially if the option
  333. <code class="literal">--checkpoint=fast</code> is not used, this can take some time
  334. during which <span class="application">pg_basebackup</span> will be appear
  335. to be idle.
  336. </p><p>
  337. The backup will include all files in the data directory and tablespaces,
  338. including the configuration files and any additional files placed in the
  339. directory by third parties, except certain temporary files managed by
  340. PostgreSQL. But only regular files and directories are copied, except that
  341. symbolic links used for tablespaces are preserved. Symbolic links pointing
  342. to certain directories known to PostgreSQL are copied as empty directories.
  343. Other symbolic links and special device files are skipped.
  344. See <a class="xref" href="protocol-replication.html" title="52.4. Streaming Replication Protocol">Section 52.4</a> for the precise details.
  345. </p><p>
  346. Tablespaces will in plain format by default be backed up to the same path
  347. they have on the server, unless the
  348. option <code class="literal">--tablespace-mapping</code> is used. Without
  349. this option, running a plain format base backup on the same host as the
  350. server will not work if tablespaces are in use, because the backup would
  351. have to be written to the same directory locations as the original
  352. tablespaces.
  353. </p><p>
  354. When tar format mode is used, it is the user's responsibility to unpack each
  355. tar file before starting the PostgreSQL server. If there are additional tablespaces, the
  356. tar files for them need to be unpacked in the correct locations. In this
  357. case the symbolic links for those tablespaces will be created by the server
  358. according to the contents of the <code class="filename">tablespace_map</code> file that is
  359. included in the <code class="filename">base.tar</code> file.
  360. </p><p>
  361. <span class="application">pg_basebackup</span> works with servers of the same
  362. or an older major version, down to 9.1. However, WAL streaming mode (<code class="literal">-X
  363. stream</code>) only works with server version 9.3 and later, and tar format mode
  364. (<code class="literal">--format=tar</code>) of the current version only works with server version 9.5
  365. or later.
  366. </p><p>
  367. <span class="application">pg_basebackup</span> will preserve group permissions in
  368. both the <code class="literal">plain</code> and <code class="literal">tar</code> formats if group
  369. permissions are enabled on the source cluster.
  370. </p></div><div class="refsect1" id="id-1.9.4.9.9"><h2>Examples</h2><p>
  371. To create a base backup of the server at <code class="literal">mydbserver</code>
  372. and store it in the local directory
  373. <code class="filename">/usr/local/pgsql/data</code>:
  374. </p><pre class="screen">
  375. <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -h mydbserver -D /usr/local/pgsql/data</code></strong>
  376. </pre><p>
  377. </p><p>
  378. To create a backup of the local server with one compressed
  379. tar file for each tablespace, and store it in the directory
  380. <code class="filename">backup</code>, showing a progress report while running:
  381. </p><pre class="screen">
  382. <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D backup -Ft -z -P</code></strong>
  383. </pre><p>
  384. </p><p>
  385. To create a backup of a single-tablespace local database and compress
  386. this with <span class="productname">bzip2</span>:
  387. </p><pre class="screen">
  388. <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D - -Ft -X fetch | bzip2 &gt; backup.tar.bz2</code></strong>
  389. </pre><p>
  390. (This command will fail if there are multiple tablespaces in the
  391. database.)
  392. </p><p>
  393. To create a backup of a local database where the tablespace in
  394. <code class="filename">/opt/ts</code> is relocated
  395. to <code class="filename">./backup/ts</code>:
  396. </p><pre class="screen">
  397. <code class="prompt">$</code> <strong class="userinput"><code>pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts</code></strong>
  398. </pre><p>
  399. </p></div><div class="refsect1" id="id-1.9.4.9.10"><h2>See Also</h2><span class="simplelist"><a class="xref" href="app-pgdump.html" title="pg_dump"><span class="refentrytitle">pg_dump</span></a></span></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="app-ecpg.html">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="reference-client.html">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="pgbench.html">Next</a></td></tr><tr><td width="40%" align="left" valign="top"><span class="application">ecpg</span> </td><td width="20%" align="center"><a accesskey="h" href="index.html">Home</a></td><td width="40%" align="right" valign="top"> <span class="application">pgbench</span></td></tr></table></div></body></html>
上海开阖软件有限公司 沪ICP备12045867号-1