Backup and Recovery: Single-Server Streaming - Recovery
Suggest editsIn the previous step, we created a full backup of the example database. But it's not a real backup until we've successfully restored it - so let's end with that.
This demo is interactive
You can follow along right in your browser. When you click "Start Now," Katacoda will load a Docker Compose
environment with two container images representing a PostgreSQL 13 server with
the Pagila database loaded (named pg
)
and a backup server for Barman (named backup
).
Once you see a barman@backup
prompt, you can follow the steps below.
Something terrible has happened to our example database! This should list all the tables:
psql -h pg -d pagila -U barman -c '\dt'
Did not find any relations.
All the data is gone!
Ok, I just didn't populate the data for this step, in order to demonstrate a total loss. Fortunately, we still have a backup!
barman list-backup pg
Let's instruct Barman to ssh into the database server and restore the backup.
Connect to pg and shut down the database cluster:
ssh postgres@pg /usr/lib/postgresql/13/bin/pg_ctl \ --pgdata=/var/lib/postgresql/data stop
Outputwaiting for server to shut down.... done server stopped
Back up the corrupt data directory, just in case something goes wrong. Then delete the corrupt data.
ssh postgres@pg "cp -a /var/lib/postgresql/data \ /var/lib/postgresql/old_data \ && rm -rf /var/lib/postgresql/data/*"
Use Barman's recover command to connect to pg and restore the latest backup
barman recover --remote-ssh-command 'ssh postgres@pg' \ pg latest \ /var/lib/postgresql/data
OutputStarting remote restore for server pg using backup 20210330T040549 Destination directory: /var/lib/postgresql/data Remote command: ssh postgres@pg Using safe horizon time for smart rsync copy: 2021-03-30 04:05:58+00:00 Copying the base backup. Copying required WAL segments. Generating archive status files Identify dangerous settings in destination directory. Recovery completed (start time: 2021-03-31 02:29:23.555186, elapsed time: 1 second) Your PostgreSQL server has been successfully prepared for recovery!
Barman handles connecting to the destination server, but we do have to provide a valid path on that server. In this example, the PostgreSQL cluster lives in /var/lib/postgresql/data and we're asking Barman to overwrite it with the backup.
Restart the server:
ssh postgres@pg "/usr/lib/postgresql/13/bin/pg_ctl \ --pgdata=/var/lib/postgresql/data \ -l /var/log/postgresql/pg.log \ start \ ; tail /var/log/postgresql/pg.log"
Outputwaiting for server to start.... done server started 2021-03-31 02:29:12.761 UTC [2201] LOG: database system is shut down 2021-03-31 02:30:13.156 UTC [2515] LOG: starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit 2021-03-31 02:30:13.157 UTC [2515] LOG: listening on IPv4 address "0.0.0.0", port 5432 2021-03-31 02:30:13.157 UTC [2515] LOG: listening on IPv6 address "::", port 5432 2021-03-31 02:30:13.160 UTC [2515] LOG: listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432" 2021-03-31 02:30:13.164 UTC [2516] LOG: database system was interrupted; last known up at 2021-03-30 04:05:58 UTC 2021-03-31 02:30:13.337 UTC [2516] LOG: redo starts at 0/4000028 2021-03-31 02:30:13.337 UTC [2516] LOG: consistent recovery state reached at 0/4000138 2021-03-31 02:30:13.337 UTC [2516] LOG: redo done at 0/4000138 2021-03-31 02:30:13.363 UTC [2515] LOG: database system is ready to accept connections
Now we should be able to reconnect to the database: