pg_basebackup(物理备份)

pg_basebackup(物理备份)

常用参数速查

参数 简写 说明 示例
–pgdata=DIRECTORY -D 备份目标目录(必需) -D /backup/base
–format=p|t -F 输出格式:plain/tar
–wal-method=METHOD -X WAL 处理方式 -Xs流复制
–checkpoint=fast|spread 检查点模式
–progress -P 显示进度 -P
–verbose -v 详细输出 -v
–jobs=NUM -j 并行压缩/传输 -j 4
–compress=METHOD -z 压缩方法 -z 9
–host=HOSTNAME -h 主机地址 -h 192.168.1.1
–port=PORT -p 端口 -p 5432
–username=NAME -U 用户名 -U rep
–no-password -w 不提示密码 -w
–password -W 强制提示密码 -W
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
pg_basebackup takes a base backup of a running PostgreSQL server.

Usage:

pg_basebackup [OPTION]...

Options controlling the output:

-D, --pgdata=DIRECTORY receive base backup into directory

-F, --format=p|t output format (plain (default), tar)

-r, --max-rate=RATE maximum transfer rate to transfer data directory

(in kB/s, or use suffix "k" or "M")

-R, --write-recovery-conf

write configuration for replication

-t, --target=TARGET[:DETAIL]

backup target (if other than client)

-T, --tablespace-mapping=OLDDIR=NEWDIR

relocate tablespace in OLDDIR to NEWDIR

--waldir=WALDIR location for the write-ahead log directory

-X, --wal-method=none|fetch|stream

include required WAL files with specified method

-z, --gzip compress tar output

-Z, --compress=[{client|server}-]METHOD[:DETAIL]

compress on client or server as specified

-Z, --compress=none do not compress tar output

General options:

-c, --checkpoint=fast|spread

set fast or spread checkpointing

-C, --create-slot create replication slot

-l, --label=LABEL set backup label

-n, --no-clean do not clean up after errors

-N, --no-sync do not wait for changes to be written safely to disk

-P, --progress show progress information

-S, --slot=SLOTNAME replication slot to use

-v, --verbose output verbose messages

-V, --version output version information, then exit

--manifest-checksums=SHA{224,256,384,512}|CRC32C|NONE

use algorithm for manifest checksums

--manifest-force-encode

hex encode all file names in manifest

--no-estimate-size do not estimate backup size in server side

--no-manifest suppress generation of backup manifest

--no-slot prevent creation of temporary replication slot

--no-verify-checksums

do not verify checksums

-?, --help show this help, then exit

Connection options:

-d, --dbname=CONNSTR connection string

-h, --host=HOSTNAME database server host or socket directory

-p, --port=PORT database server port number

-s, --status-interval=INTERVAL

time between status packets sent to server (in seconds)

-U, --username=NAME connect as specified database user

-w, --no-password never prompt for password

-W, --password force password prompt (should happen automatically)

Report bugs to <pgsql-bugs@lists.postgresql.org>.

PostgreSQL home page: <https://www.postgresql.org/>

示例

常用备份语法:

1
pg_basebackup -D /opt/pg_basebackup/$(date +"%Y%m%d_%H%M%S") -Fp -Xs -P -v -U rep -h 192.168.0.3

1
pg_basebackup -D /opt/pg_basebackup/$(date +"%Y%m%d_%H%M%S") -Ft -Xs -P -v -U rep -h 192.168.0.3

backup_manifest

pg_dump

常用参数速查

参数 说明 示例
-d, –dbname=DBNAME 指定数据库 -d mydb
-U, –username=NAME 用户名 -U postgres
-h, –host=HOSTNAME 主机地址 -h 127.0.0.1
-p, –port=PORT 端口 -p 5432
-f, –file=FILENAME 输出文件(替代 >) -f backup.sql
-v, –verbose 详细输出 -v
-W, –password 强制提示密码 -W
-w, –no-password 从不提示密码 -w
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
pg_dump dumps a database as a text file or to other formats.

Usage:

pg_dump [OPTION]... [DBNAME]

General options:

-f, --file=FILENAME output file or directory name/输出文件名或目录名

-F, --format=c|d|t|p output file format (custom, directory, tar,

plain text (default))

-j, --jobs=NUM use this many parallel jobs to dump

-v, --verbose verbose mode

-V, --version output version information, then exit

-Z, --compress=METHOD[:DETAIL]

compress as specified

--lock-wait-timeout=TIMEOUT fail after waiting TIMEOUT for a table lock

--no-sync do not wait for changes to be written safely to disk

-?, --help show this help, then exit

Options controlling the output content:

-a, --data-only dump only the data, not the schema

-b, --large-objects include large objects in dump

--blobs (same as --large-objects, deprecated)

-B, --no-large-objects exclude large objects in dump

--no-blobs (same as --no-large-objects, deprecated)

-c, --clean clean (drop) database objects before recreating

-C, --create include commands to create database in dump

-e, --extension=PATTERN dump the specified extension(s) only

-E, --encoding=ENCODING dump the data in encoding ENCODING

-n, --schema=PATTERN dump the specified schema(s) only

-N, --exclude-schema=PATTERN do NOT dump the specified schema(s)

-O, --no-owner skip restoration of object ownership in

plain-text format

-s, --schema-only dump only the schema, no data

-S, --superuser=NAME superuser user name to use in plain-text format

-t, --table=PATTERN dump only the specified table(s)

-T, --exclude-table=PATTERN do NOT dump the specified table(s)

-x, --no-privileges do not dump privileges (grant/revoke)

--binary-upgrade for use by upgrade utilities only

--column-inserts dump data as INSERT commands with column names

--disable-dollar-quoting disable dollar quoting, use SQL standard quoting

--disable-triggers disable triggers during data-only restore

--enable-row-security enable row security (dump only content user has

access to)

--exclude-table-and-children=PATTERN

do NOT dump the specified table(s), including

child and partition tables

--exclude-table-data=PATTERN do NOT dump data for the specified table(s)

--exclude-table-data-and-children=PATTERN

do NOT dump data for the specified table(s),

including child and partition tables

--extra-float-digits=NUM override default setting for extra_float_digits

--if-exists use IF EXISTS when dropping objects

--include-foreign-data=PATTERN

include data of foreign tables on foreign

servers matching PATTERN

--inserts dump data as INSERT commands, rather than COPY

--load-via-partition-root load partitions via the root table

--no-comments do not dump comments

--no-publications do not dump publications

--no-security-labels do not dump security label assignments

--no-subscriptions do not dump subscriptions

--no-table-access-method do not dump table access methods

--no-tablespaces do not dump tablespace assignments

--no-toast-compression do not dump TOAST compression methods

--no-unlogged-table-data do not dump unlogged table data

--on-conflict-do-nothing add ON CONFLICT DO NOTHING to INSERT commands

--quote-all-identifiers quote all identifiers, even if not key words

--rows-per-insert=NROWS number of rows per INSERT; implies --inserts

--section=SECTION dump named section (pre-data, data, or post-data)

--serializable-deferrable wait until the dump can run without anomalies

--snapshot=SNAPSHOT use given snapshot for the dump

--strict-names require table and/or schema include patterns to

match at least one entity each

--table-and-children=PATTERN dump only the specified table(s), including

child and partition tables

--use-set-session-authorization

use SET SESSION AUTHORIZATION commands instead of

ALTER OWNER commands to set ownership

Connection options:

-d, --dbname=DBNAME database to dump

-h, --host=HOSTNAME database server host or socket directory

-p, --port=PORT database server port number

-U, --username=NAME connect as specified database user

-w, --no-password never prompt for password

-W, --password force password prompt (should happen automatically)

--role=ROLENAME do SET ROLE before dump

If no database name is supplied, then the PGDATABASE environment

variable value is used.

Report bugs to <pgsql-bugs@lists.postgresql.org>.

PostgreSQL home page: <https://www.postgresql.org/>

示例

1
pg_dump -U postgres -d demo1 > $(date +"%Y%m%d_%H%M%S").sql