Skip to content

Commit 71e2767

Browse files
authored
PG-2246 - pgBackRest chapter updates (#931)
This PR merges some updates for pgBackRest: - Minor introduction update - Add explanations for the configuration file parameters in a Where description, including default port mentions and explanations for each setting - Update pg-host SRV_NAME to proper host name based on NODE number in the stanza
1 parent b153118 commit 71e2767

1 file changed

Lines changed: 46 additions & 37 deletions

File tree

docs/solutions/pgbackrest.md

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
11
# pgBackRest setup
22

3-
[pgBackRest :octicons-link-external-16:](https://pgbackrest.org/) is a backup tool used to perform PostgreSQL database backup, archiving, restoration, and point-in-time recovery.
3+
[pgBackRest :octicons-link-external-16:](https://pgbackrest.org/) is a tool used to perform PostgreSQL database backups, archiving, restoration, and point-in-time recovery.
44

5-
In our solution we deploy a [pgBackRest server on a dedicated host :octicons-link-external-16:](https://pgbackrest.org/user-guide-rhel.html#repo-host) and also deploy pgBackRest on the PostgreSQL servers. Them we configure PostgreSQL servers to use it for backups and archiving.
6-
7-
You also need a backup storage to store the backups. It can either be a remote storage such as AWS S3, S3-compatible storages or Azure blob storage, or a filesystem-based one.
5+
In this solution, a [pgBackRest server on a dedicated host :octicons-link-external-16:](https://pgbackrest.org/user-guide-rhel.html#repo-host) is deployed. pgBackRest is also installed and configured on the PostgreSQL servers to perform backups and manage WAL archiving.
86

97
## Preparation
108

119
Make sure to complete the [initial setup](ha-init-setup.md) steps.
1210

1311
## Install pgBackRest
1412

15-
Install pgBackRest on the following nodes: `node1`, `node2`, `node3`, `backup`
13+
Install pgBackRest on all nodes: `node1`, `node2`, `node3`, and `backup`.
1614

1715
=== ":material-debian: On Debian/Ubuntu"
1816

@@ -42,7 +40,7 @@ Do the following steps on the `backup` node.
4240
export CA_PATH="/etc/ssl/certs/pg_ha"
4341
```
4442

45-
2. Create the `pgBackRest` repository, *if necessary*
43+
2. Create the `pgBackRest` repository, *if necessary*.
4644

4745
A repository is where `pgBackRest` stores backups. In this example, the backups will be saved to `/var/lib/pgbackrest`.
4846

@@ -118,8 +116,8 @@ Do the following steps on the `backup` node.
118116
pg1-port=5432
119117
pg1-path=/var/lib/postgresql/{{pgversion}}/main
120118
pg1-host-type=tls
121-
pg1-host-cert-file=${CA_PATH}/${SRV_NAME}.crt
122-
pg1-host-key-file=${CA_PATH}/${SRV_NAME}.key
119+
pg1-host-cert-file=${CA_PATH}/${NODE1_NAME}.crt
120+
pg1-host-key-file=${CA_PATH}/${NODE1_NAME}.key
123121
pg1-host-ca-file=${CA_PATH}/ca.crt
124122
pg1-socket-path=/var/run/postgresql
125123
@@ -128,8 +126,8 @@ Do the following steps on the `backup` node.
128126
pg2-port=5432
129127
pg2-path=/var/lib/postgresql/{{pgversion}}/main
130128
pg2-host-type=tls
131-
pg2-host-cert-file=${CA_PATH}/${SRV_NAME}.crt
132-
pg2-host-key-file=${CA_PATH}/${SRV_NAME}.key
129+
pg2-host-cert-file=${CA_PATH}/${NODE2_NAME}.crt
130+
pg2-host-key-file=${CA_PATH}/${NODE2_NAME}.key
133131
pg2-host-ca-file=${CA_PATH}/ca.crt
134132
pg2-socket-path=/var/run/postgresql
135133
@@ -138,8 +136,8 @@ Do the following steps on the `backup` node.
138136
pg3-port=5432
139137
pg3-path=/var/lib/postgresql/{{pgversion}}/main
140138
pg3-host-type=tls
141-
pg3-host-cert-file=${CA_PATH}/${SRV_NAME}.crt
142-
pg3-host-key-file=${CA_PATH}/${SRV_NAME}.key
139+
pg3-host-cert-file=${CA_PATH}/${NODE3_NAME}.crt
140+
pg3-host-key-file=${CA_PATH}/${NODE3_NAME}.key
143141
pg3-host-ca-file=${CA_PATH}/ca.crt
144142
pg3-socket-path=/var/run/postgresql
145143
@@ -200,8 +198,8 @@ Do the following steps on the `backup` node.
200198
pg1-port=5432
201199
pg1-path=/var/lib/postgresql/{{pgversion}}/main
202200
pg1-host-type=tls
203-
pg1-host-cert-file=${CA_PATH}/${SRV_NAME}.crt
204-
pg1-host-key-file=${CA_PATH}/${SRV_NAME}.key
201+
pg1-host-cert-file=${CA_PATH}/${NODE1_NAME}.crt
202+
pg1-host-key-file=${CA_PATH}/${NODE1_NAME}.key
205203
pg1-host-ca-file=${CA_PATH}/ca.crt
206204
pg1-socket-path=/var/run/postgresql
207205
@@ -210,8 +208,8 @@ Do the following steps on the `backup` node.
210208
pg2-port=5432
211209
pg2-path=/var/lib/postgresql/{{pgversion}}/main
212210
pg2-host-type=tls
213-
pg2-host-cert-file=${CA_PATH}/${SRV_NAME}.crt
214-
pg2-host-key-file=${CA_PATH}/${SRV_NAME}.key
211+
pg2-host-cert-file=${CA_PATH}/${NODE2_NAME}.crt
212+
pg2-host-key-file=${CA_PATH}/${NODE2_NAME}.key
215213
pg2-host-ca-file=${CA_PATH}/ca.crt
216214
pg2-socket-path=/var/run/postgresql
217215
@@ -220,20 +218,32 @@ Do the following steps on the `backup` node.
220218
pg3-port=5432
221219
pg3-path=/var/lib/postgresql/{{pgversion}}/main
222220
pg3-host-type=tls
223-
pg3-host-cert-file=${CA_PATH}/${SRV_NAME}.crt
224-
pg3-host-key-file=${CA_PATH}/${SRV_NAME}.key
221+
pg3-host-cert-file=${CA_PATH}/${NODE3_NAME}.crt
222+
pg3-host-key-file=${CA_PATH}/${NODE3_NAME}.key
225223
pg3-host-ca-file=${CA_PATH}/ca.crt
226224
pg3-socket-path=/var/run/postgresql
227225
228226
" | sudo tee /etc/pgbackrest.conf
229227
```
230228
231-
*NOTE*: The option `backup-standby=y` above indicates the backups should be taken from a standby server. If you are operating with a primary only, or if your secondaries are not configured with `pgBackRest`, set this option to `n`.
229+
Where:
230+
231+
* `pgX-host` specifies the hostname of the PostgreSQL node
232+
* `pgX-host-port` specifies the port used by the pgBackRest server daemon on that node. The default pgBackRest server port is `8432`
233+
* `pgX-port` specifies the PostgreSQL server port (default `5432`)
234+
* `pgX-path` specifies the PostgreSQL data directory on the node
235+
236+
The `pgX` prefix is repeated for each PostgreSQL node in the cluster (for example `pg1`, `pg2`, `pg3`).
237+
238+
The numbering (`pg1`, `pg2`, `pg3`) represents individual PostgreSQL nodes defined in the cluster stanza.
239+
240+
!!! note
241+
The option `backup-standby=y` above indicates the backups should be taken from a standby server. If you are operating with a primary only, or if your secondaries are not configured with `pgBackRest`, set this option to `n`.
232242
233243
### Create the certificate files
234244
235245
Run the following commands as a root user or with `sudo` privileges
236-
246+
237247
1. Create the folder to store the certificates:
238248
239249
```{.bash data-prompt="$"}
@@ -252,21 +262,21 @@ Run the following commands as a root user or with `sudo` privileges
252262
$ sudo openssl req -new -x509 -days 365 -nodes -out ${CA_PATH}/ca.crt -keyout ${CA_PATH}/ca.key -subj "/CN=root-ca"
253263
```
254264
255-
3. Create the certificate and keys for the backup server
265+
4. Create the certificate and keys for the backup server
256266
257267
```{.bash data-prompt="$"}
258268
$ sudo openssl req -new -nodes -out ${CA_PATH}/${SRV_NAME}.csr -keyout ${CA_PATH}/${SRV_NAME}.key -subj "/CN=${SRV_NAME}"
259269
```
260270
261-
4. Create the certificates and keys for each PostgreSQL node
271+
5. Create the certificates and keys for each PostgreSQL node
262272
263273
```{.bash data-prompt="$"}
264274
$ sudo openssl req -new -nodes -out ${CA_PATH}/${NODE1_NAME}.csr -keyout ${CA_PATH}/${NODE1_NAME}.key -subj "/CN=${NODE1_NAME}"
265275
$ sudo openssl req -new -nodes -out ${CA_PATH}/${NODE2_NAME}.csr -keyout ${CA_PATH}/${NODE2_NAME}.key -subj "/CN=${NODE2_NAME}"
266276
$ sudo openssl req -new -nodes -out ${CA_PATH}/${NODE3_NAME}.csr -keyout ${CA_PATH}/${NODE3_NAME}.key -subj "/CN=${NODE3_NAME}"
267277
```
268278
269-
4. Sign all certificates with the `root-ca` key
279+
6. Sign all certificates with the `root-ca` key
270280
271281
```{.bash data-prompt="$"}
272282
$ sudo openssl x509 -req -in ${CA_PATH}/${SRV_NAME}.csr -days 365 -CA ${CA_PATH}/ca.crt -CAkey ${CA_PATH}/ca.key -CAcreateserial -out ${CA_PATH}/${SRV_NAME}.crt
@@ -275,13 +285,13 @@ Run the following commands as a root user or with `sudo` privileges
275285
$ sudo openssl x509 -req -in ${CA_PATH}/${NODE3_NAME}.csr -days 365 -CA ${CA_PATH}/ca.crt -CAkey ${CA_PATH}/ca.key -CAcreateserial -out ${CA_PATH}/${NODE3_NAME}.crt
276286
```
277287
278-
5. Remove temporary files, set ownership of the remaining files to the `postgres` user, and restrict their access:
288+
7. Remove temporary files, set ownership of the remaining files to the `postgres` user, and restrict their access:
279289
280290
```{.bash data-prompt="$"}
281291
$ sudo rm -f ${CA_PATH}/*.csr
282292
$ sudo chown postgres:postgres -R ${CA_PATH}
283293
$ sudo chmod 0600 ${CA_PATH}/*
284-
```
294+
```
285295
286296
### Create the `pgbackrest` daemon service
287297
@@ -305,15 +315,15 @@ Run the following commands as a root user or with `sudo` privileges
305315
[Install]
306316
WantedBy=multi-user.target
307317
```
308-
309-
2. Make `systemd` aware of the new service:
318+
319+
2. Make `systemd` aware of the new service:
310320
311321
```{.bash data-prompt="$"}
312322
$ sudo systemctl daemon-reload
313323
```
314324
315325
3. Enable `pgBackRest`:
316-
326+
317327
```{.bash data-prompt="$"}
318328
$ sudo systemctl enable --now pgbackrest.service
319329
```
@@ -335,15 +345,15 @@ Run the following commands on `node1`, `node2`, and `node3`.
335345
```{.bash data-prompt="$"}
336346
$ sudo yum install percona-pgbackrest
337347
```
338-
348+
339349
2. Export environment variables to simplify the config file creation:
340350
341351
```{.bash data-prompt="$"}
342352
$ export NODE_NAME=`hostname -f`
343353
$ export SRV_NAME="backup"
344354
$ export CA_PATH="/etc/ssl/certs/pg_ha"
345355
```
346-
356+
347357
3. Create the certificates folder:
348358
349359
```{.bash data-prompt="$"}
@@ -357,7 +367,7 @@ Run the following commands on `node1`, `node2`, and `node3`.
357367
$ sudo chown postgres:postgres -R ${CA_PATH}
358368
$ sudo chmod 0600 ${CA_PATH}/*
359369
```
360-
370+
361371
5. Make a copy of the configuration file. The path to it can be either `/etc/pgbackrest/pgbackrest.conf` or `/etc/pgbackrest.conf`:
362372
363373
```{.bash data-prompt="$"}
@@ -395,7 +405,6 @@ Run the following commands on `node1`, `node2`, and `node3`.
395405
" | sudo tee /etc/pgbackrest.conf
396406
```
397407
398-
399408
=== ":material-redhat: On RHEL/derivatives"
400409
401410
```ini title="pgbackrest.conf"
@@ -446,7 +455,7 @@ Run the following commands on `node1`, `node2`, and `node3`.
446455
WantedBy=multi-user.target
447456
```
448457
449-
8. Reload the `systemd`, the start the service
458+
8. Reload `systemd` and start the service:
450459
451460
```{.bash data-prompt="$"}
452461
$ sudo systemctl daemon-reload
@@ -468,7 +477,7 @@ Run the following commands on `node1`, `node2`, and `node3`.
468477
tcp 0 0 0.0.0.0:8432 0.0.0.0:* LISTEN 40224/pgbackrest
469478
```
470479
471-
9. If you are using Patroni, change its configuration to use `pgBackRest` for archiving and restoring WAL files. Run this command only on one node, for example, on `node1`:
480+
9. If you are using Patroni, change its configuration to use `pgBackRest` for archiving and restoring WAL files. Run this command only on one node, for example, on `node1`:
472481
473482
```{.bash data-prompt="$"}
474483
$ patronictl -c /etc/patroni/patroni.yml edit-config
@@ -504,7 +513,6 @@ Run the following commands on `node1`, `node2`, and `node3`.
504513
ttl: 30
505514
```
506515
507-
508516
11. Reload the changed configurations. Provide the cluster name or the node name for the following command. In our example we use the `cluster_1` cluster name:
509517
510518
```{.bash data-prompt="$"}
@@ -513,7 +521,8 @@ Run the following commands on `node1`, `node2`, and `node3`.
513521
514522
It may take a while to reload the new configuration.
515523
516-
*NOTE*: When configuring a PostgreSQL server that is not managed by Patroni to archive/restore WALs from the `pgBackRest` server, edit the server's main configuration file directly and adjust the `archive_command` and `restore_command` variables as shown above.
524+
!!! note
525+
When configuring a PostgreSQL server that is not managed by Patroni to archive/restore WALs from the `pgBackRest` server, edit the server's main configuration file directly and adjust the `archive_command` and `restore_command` variables as shown above.
517526

518527
## Create backups
519528

@@ -532,7 +541,7 @@ Run the following commands on the **backup server**:
532541
```
533542

534543
3. Check backup info
535-
544+
536545
```{.bash data-prompt="$"}
537546
$ sudo -iu postgres pgbackrest --stanza=cluster_1 info
538547
```

0 commit comments

Comments
 (0)