app-pgbasebackup.md 21.5 KB
Newer Older
K
KyleZhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
## pg\_basebackup

pg\_basebackup — take a base backup of a PostgreSQL cluster

## Synopsis

`pg_basebackup` [*`option`*...]

## Description

pg\_basebackup is used to take a base backup of a running PostgreSQL database cluster. The backup is taken without affecting other clients of the database, and can be used both for point-in-time recovery (see [Section 26.3](continuous-archiving.html)) and as the starting point for a log-shipping or streaming-replication standby server (see [Section 27.2](warm-standby.html)).

pg\_basebackup makes an exact copy of the database cluster's files, while making sure the server is put into and out of backup mode automatically. Backups are always taken of the entire database cluster; it is not possible to back up individual databases or database objects. For selective backups, another tool such as [pg\_dump](app-pgdump.html) must be used.

 The backup is made over a regular PostgreSQL connection that uses the replication protocol. The connection must be made with a user ID that has `REPLICATION` permissions (see [Section 22.2](role-attributes.html)) or is a superuser, and [`pg_hba.conf`](auth-pg-hba-conf.html) must permit the replication connection. The server must also be configured with [max\_wal\_senders](runtime-config-replication.html#GUC-MAX-WAL-SENDERS) set high enough to provide at least one walsender for the backup plus one for WAL streaming (if used).

 There can be multiple `pg_basebackup`s running at the same time, but it is usually better from a performance point of view to take only one backup, and copy the result.

pg\_basebackup can make a base backup from not only a primary server but also a standby. To take a backup from a standby, set up the standby so that it can accept replication connections (that is, set `max_wal_senders` and [hot\_standby](runtime-config-replication.html#GUC-HOT-STANDBY), and configure its `pg_hba.conf` appropriately). You will also need to enable [full\_page\_writes](runtime-config-wal.html#GUC-FULL-PAGE-WRITES) on the primary.

 Note that there are some limitations in taking a backup from a standby:

* The backup history file is not created in the database cluster backed up.

* pg\_basebackup cannot force the standby to switch to a new WAL file at the end of backup. When you are using `-X none`, if write activity on the primary is low, pg\_basebackup may need to wait a long time for the last WAL file required for the backup to be switched and archived. In this case, it may be useful to run `pg_switch_wal` on the primary in order to trigger an immediate WAL file switch.

* If the standby is promoted to be primary during backup, the backup fails.

* All WAL records required for the backup must contain sufficient full-page writes, which requires you to enable `full_page_writes` on the primary and not to use a tool like pg\_compresslog as `archive_command` to remove full-page writes from WAL files.

 Whenever pg\_basebackup is taking a base backup, the server's `pg_stat_progress_basebackup` view will report the progress of the backup. See [Section 28.4.5](progress-reporting.html#BASEBACKUP-PROGRESS-REPORTING) for details.

## Options

 The following command-line options control the location and format of the output:

`-D *`directory`*`  
`--pgdata=*`directory`*`

 Sets the target directory to write the output to. pg\_basebackup will create this directory (and any missing parent directories) if it does not exist. If it already exists, it must be empty.

 When the backup is in tar format, the target directory may be specified as `-` (dash), causing the tar file to be written to `stdout`.

 This option is required.

`-F *`format`*`  
`--format=*`format`*`

 Selects the format for the output. *`format`* can be one of the following:

`p`  
`plain`

 Write the output as plain files, with the same layout as the source server's data directory and tablespaces. When the cluster has no additional tablespaces, the whole database will be placed in the target directory. If the cluster contains additional tablespaces, the main data directory will be placed in the target directory, but all other tablespaces will be placed in the same absolute path as they have on the source server. (See `--tablespace-mapping` to change that.)

 This is the default format.

`t`  
`tar`

 Write the output as tar files in the target directory. The main data directory's contents will be written to a file named `base.tar`, and each other tablespace will be written to a separate tar file named after that tablespace's OID.

 If the target directory is specified as `-` (dash), the tar contents will be written to standard output, suitable for piping to (for example) gzip. This is only allowed if the cluster has no additional tablespaces and WAL streaming is not used.

`-R`  
`--write-recovery-conf`

 Creates a [`standby.signal`](warm-standby.html#FILE-STANDBY-SIGNAL) []() file and appends connection settings to the `postgresql.auto.conf` file in the target directory (or within the base archive file when using tar format). This eases setting up a standby server using the results of the backup.

 The `postgresql.auto.conf` file will record the connection settings and, if specified, the replication slot that pg\_basebackup is using, so that streaming replication will use the same settings later on.

`-T *`olddir`*=*`newdir`*`  
`--tablespace-mapping=*`olddir`*=*`newdir`*`

 Relocates the tablespace in directory *`olddir`* to *`newdir`* during the backup. To be effective, *`olddir`* must exactly match the path specification of the tablespace as it is defined on the source server. (But it is not an error if there is no tablespace in *`olddir`* on the source server.) Meanwhile *`newdir`* is a directory in the receiving host's filesystem. As with the main target directory, *`newdir`* need not exist already, but if it does exist it must be empty. Both *`olddir`* and *`newdir`* must be absolute paths. If either path needs to contain an equal sign (`=`), precede that with a backslash. This option can be specified multiple times for multiple tablespaces.

 If a tablespace is relocated in this way, the symbolic links inside the main data directory are updated to point to the new location. So the new data directory is ready to be used for a new server instance with all tablespaces in the updated locations.

 Currently, this option only works with plain output format; it is ignored if tar format is selected.

`--waldir=*`waldir`*`

 Sets the directory to write WAL (write-ahead log) files to. By default WAL files will be placed in the `pg_wal` subdirectory of the target directory, but this option can be used to place them elsewhere. *`waldir`* must be an absolute path. As with the main target directory, *`waldir`* need not exist already, but if it does exist it must be empty. This option can only be specified when the backup is in plain format.

`-X *`method`*`  
`--wal-method=*`method`*`

 Includes the required WAL (write-ahead log) files in the backup. This will include all write-ahead logs generated during the backup. Unless the method `none` is specified, it is possible to start a postmaster in the target directory without the need to consult the log archive, thus making the output a completely standalone backup.

 The following *`method`*s for collecting the write-ahead logs are supported:

`n`  
`none`

 Don't include write-ahead logs in the backup.

`f`  
`fetch`

 The write-ahead log files are collected at the end of the backup. Therefore, it is necessary for the source server's [wal\_keep\_size](runtime-config-replication.html#GUC-WAL-KEEP-SIZE) parameter to be set high enough that the required log data is not removed before the end of the backup. If the required log data has been recycled before it's time to transfer it, the backup will fail and be unusable.

 When tar format is used, the write-ahead log files will be included in the `base.tar` file.

`s`  
`stream`

 Stream write-ahead log data while the backup is being taken. This method will open a second connection to the server and start streaming the write-ahead log in parallel while running the backup. Therefore, it will require two replication connections not just one. As long as the client can keep up with the write-ahead log data, using this method requires no extra write-ahead logs to be saved on the source server.

 When tar format is used, the write-ahead log files will be written to a separate file named `pg_wal.tar` (if the server is a version earlier than 10, the file will be named `pg_xlog.tar`).

 This value is the default.

`-z`  
`--gzip`

 Enables gzip compression of tar file output, with the default compression level. Compression is only available when using the tar format, and the suffix `.gz` will automatically be added to all tar filenames.

`-Z *`level`*`  
`--compress=*`level`*`

 Enables gzip compression of tar file output, and specifies the compression level (0 through 9, 0 being no compression and 9 being best compression). Compression is only available when using the tar format, and the suffix `.gz` will automatically be added to all tar filenames.

 The following command-line options control the generation of the backup and the invocation of the program:

`-c *`fast|spread`*`  
`--checkpoint=*`fast|spread`*`

 Sets checkpoint mode to fast (immediate) or spread (the default) (see [Section 26.3.3](continuous-archiving.html#BACKUP-LOWLEVEL-BASE-BACKUP)).

`-C`  
`--create-slot`

 Specifies that the replication slot named by the `--slot` option should be created before starting the backup. An error is raised if the slot already exists.

`-l *`label`*`  
`--label=*`label`*`

 Sets the label for the backup. If none is specified, a default value of “`pg_basebackup base backup`” will be used.

`-n`  
`--no-clean`

 By default, when `pg_basebackup` aborts with an error, it removes any directories it might have created before discovering that it cannot finish the job (for example, the target directory and write-ahead log directory). This option inhibits tidying-up and is thus useful for debugging.

 Note that tablespace directories are not cleaned up either way.

`-N`  
`--no-sync`

 By default, `pg_basebackup` will wait for all files to be written safely to disk. This option causes `pg_basebackup` to return without waiting, which is faster, but means that a subsequent operating system crash can leave the base backup corrupt. Generally, this option is useful for testing but should not be used when creating a production installation.

`-P`  
`--progress`

 Enables progress reporting. Turning this on will deliver an approximate progress report during the backup. Since the database may change during the backup, this is only an approximation and may not end at exactly `100%`. In particular, when WAL log is included in the backup, the total amount of data cannot be estimated in advance, and in this case the estimated target size will increase once it passes the total estimate without WAL.

`-r *`rate`*`  
`--max-rate=*`rate`*`

 Sets the maximum transfer rate at which data is collected from the source server. This can be useful to limit the impact of pg\_basebackup on the server. Values are in kilobytes per second. Use a suffix of `M` to indicate megabytes per second. A suffix of `k` is also accepted, and has no effect. Valid values are between 32 kilobytes per second and 1024 megabytes per second.

 This option always affects transfer of the data directory. Transfer of WAL files is only affected if the collection method is `fetch`.

`-S *`slotname`*`  
`--slot=*`slotname`*`

 This option can only be used together with `-X stream`. It causes WAL streaming to use the specified replication slot. If the base backup is intended to be used as a streaming-replication standby using a replication slot, the standby should then use the same replication slot name as [primary\_slot\_name](runtime-config-replication.html#GUC-PRIMARY-SLOT-NAME). This ensures that the primary server does not remove any necessary WAL data in the time between the end of the base backup and the start of streaming replication on the new standby.

 The specified replication slot has to exist unless the option `-C` is also used.

 If this option is not specified and the server supports temporary replication slots (version 10 and later), then a temporary replication slot is automatically used for WAL streaming.

`-v`  
`--verbose`

 Enables verbose mode. Will output some extra steps during startup and shutdown, as well as show the exact file name that is currently being processed if progress reporting is also enabled.

`--manifest-checksums=*`algorithm`*`

 Specifies the checksum algorithm that should be applied to each file included in the backup manifest. Currently, the available algorithms are `NONE`, `CRC32C`, `SHA224`, `SHA256`, `SHA384`, and `SHA512`. The default is `CRC32C`.

 If `NONE` is selected, the backup manifest will not contain any checksums. Otherwise, it will contain a checksum of each file in the backup using the specified algorithm. In addition, the manifest will always contain a `SHA256` checksum of its own contents. The `SHA` algorithms are significantly more CPU-intensive than `CRC32C`, so selecting one of them may increase the time required to complete the backup.

 Using a SHA hash function provides a cryptographically secure digest of each file for users who wish to verify that the backup has not been tampered with, while the CRC32C algorithm provides a checksum that is much faster to calculate; it is good at catching errors due to accidental changes but is not resistant to malicious modifications. Note that, to be useful against an adversary who has access to the backup, the backup manifest would need to be stored securely elsewhere or otherwise verified not to have been modified since the backup was taken.

[pg\_verifybackup](app-pgverifybackup.html) can be used to check the integrity of a backup against the backup manifest.

`--manifest-force-encode`

 Forces all filenames in the backup manifest to be hex-encoded. If this option is not specified, only non-UTF8 filenames are hex-encoded. This option is mostly intended to test that tools which read a backup manifest file properly handle this case.

`--no-estimate-size`

 Prevents the server from estimating the total amount of backup data that will be streamed, resulting in the `backup_total` column in the `pg_stat_progress_basebackup` view always being `NULL`.

 Without this option, the backup will start by enumerating the size of the entire database, and then go back and send the actual contents. This may make the backup take slightly longer, and in particular it will take longer before the first data is sent. This option is useful to avoid such estimation time if it's too long.

 This option is not allowed when using `--progress`.

`--no-manifest`

 Disables generation of a backup manifest. If this option is not specified, the server will generate and send a backup manifest which can be verified using [pg\_verifybackup](app-pgverifybackup.html). The manifest is a list of every file present in the backup with the exception of any WAL files that may be included. It also stores the size, last modification time, and an optional checksum for each file.

`--no-slot`

 Prevents the creation of a temporary replication slot for the backup.

 By default, if log streaming is selected but no slot name is given with the `-S` option, then a temporary replication slot is created (if supported by the source server).

 The main purpose of this option is to allow taking a base backup when the server has no free replication slots. Using a replication slot is almost always preferred, because it prevents needed WAL from being removed by the server during the backup.

`--no-verify-checksums`

 Disables verification of checksums, if they are enabled on the server the base backup is taken from.

 By default, checksums are verified and checksum failures will result in a non-zero exit status. However, the base backup will not be removed in such a case, as if the `--no-clean` option had been used. Checksum verification failures will also be reported in the [`pg_stat_database`](monitoring-stats.html#MONITORING-PG-STAT-DATABASE-VIEW) view.

 The following command-line options control the connection to the source server:

`-d *`connstr`*`  
`--dbname=*`connstr`*`

 Specifies parameters used to connect to the server, as a [connection string](libpq-connect.html#LIBPQ-CONNSTRING); these will override any conflicting command line options.

 The option is called `--dbname` for consistency with other client applications, but because pg\_basebackup doesn't connect to any particular database in the cluster, any database name in the connection string will be ignored.

`-h *`host`*`  
`--host=*`host`*`

 Specifies the host name of the machine on which the server is running. If the value begins with a slash, it is used as the directory for a Unix domain socket. The default is taken from the `PGHOST` environment variable, if set, else a Unix domain socket connection is attempted.

`-p *`port`*`  
`--port=*`port`*`

 Specifies the TCP port or local Unix domain socket file extension on which the server is listening for connections. Defaults to the `PGPORT` environment variable, if set, or a compiled-in default.

`-s *`interval`*`  
`--status-interval=*`interval`*`

 Specifies the number of seconds between status packets sent back to the source server. Smaller values allow more accurate monitoring of backup progress from the server. A value of zero disables periodic status updates completely, although an update will still be sent when requested by the server, to avoid timeout-based disconnects. The default value is 10 seconds.

`-U *`username`*`  
`--username=*`username`*`

 Specifies the user name to connect as.

`-w`  
`--no-password`

 Prevents issuing a password prompt. If the server requires password authentication and a password is not available by other means such as a `.pgpass` file, the connection attempt will fail. This option can be useful in batch jobs and scripts where no user is present to enter a password.

`-W`  
`--password`

 Forces pg\_basebackup to prompt for a password before connecting to the source server.

 This option is never essential, since pg\_basebackup will automatically prompt for a password if the server demands password authentication. However, pg\_basebackup will waste a connection attempt finding out that the server wants a password. In some cases it is worth typing `-W` to avoid the extra connection attempt.

 Other options are also available:

`-V`  
`--version`

 Prints the pg\_basebackup version and exits.

`-?`  
`--help`

 Shows help about pg\_basebackup command line arguments, and exits.

## Environment

 This utility, like most other PostgreSQL utilities, uses the environment variables supported by libpq (see [Section 34.15](libpq-envars.html)).

 The environment variable `PG_COLOR` specifies whether to use color in diagnostic messages. Possible values are `always`, `auto` and `never`.

## Notes

 At the beginning of the backup, a checkpoint needs to be performed on the source server. This can take some time (especially if the option `--checkpoint=fast` is not used), during which pg\_basebackup will appear to be idle.

 The backup will include all files in the data directory and tablespaces, including the configuration files and any additional files placed in the directory by third parties, except certain temporary files managed by PostgreSQL. But only regular files and directories are copied, except that symbolic links used for tablespaces are preserved. Symbolic links pointing to certain directories known to PostgreSQL are copied as empty directories. Other symbolic links and special device files are skipped. See [Section 53.4](protocol-replication.html) for the precise details.

 In plain format, tablespaces will be backed up to the same path they have on the source server, unless the option `--tablespace-mapping` is used. Without this option, running a plain format base backup on the same host as the server will not work if tablespaces are in use, because the backup would have to be written to the same directory locations as the original tablespaces.

 When tar format is used, it is the user's responsibility to unpack each tar file before starting a PostgreSQL server that uses the data. If there are additional tablespaces, the tar files for them need to be unpacked in the correct locations. In this case the symbolic links for those tablespaces will be created by the server according to the contents of the `tablespace_map` file that is included in the `base.tar` file.

pg\_basebackup works with servers of the same or an older major version, down to 9.1. However, WAL streaming mode (`-X stream`) only works with server version 9.3 and later, and tar format (`--format=tar`) only works with server version 9.5 and later.

pg\_basebackup will preserve group permissions for data files if group permissions are enabled on the source cluster.

## Examples

 To create a base backup of the server at `mydbserver` and store it in the local directory `/usr/local/pgsql/data`:

```
$ pg_basebackup -h mydbserver -D /usr/local/pgsql/data

```

 To create a backup of the local server with one compressed tar file for each tablespace, and store it in the directory `backup`, showing a progress report while running:

```
$ pg_basebackup -D backup -Ft -z -P

```

 To create a backup of a single-tablespace local database and compress this with bzip2:

```
$ pg_basebackup -D - -Ft -X fetch | bzip2 > backup.tar.bz2

```

 (This command will fail if there are multiple tablespaces in the database.)

 To create a backup of a local database where the tablespace in `/opt/ts` is relocated to `./backup/ts`:

```
$ pg_basebackup -D backup/data -T /opt/ts=$(pwd)/backup/ts

```

## See Also

[pg\_dump](app-pgdump.html), [Section 28.4.5](progress-reporting.html#BASEBACKUP-PROGRESS-REPORTING)