Nella prima parte di questo blog, Jonathan ha spiegato come funziona il comando barman-wal-archive. Ora, supponendo che tu abbia seguito queste istruzioni, hai un'istanza PostgreSQL correttamente configurata attiva e funzionante. In questa seconda parte, ti mostrerò come funziona il barman-cloud-backup il comando funziona.
Come puoi intuire dal nome del comando stesso, il barman-cloud-backup Il comando consente di eseguire backup direttamente da un server PostgreSQL e utilizzare come destinazione un object store compatibile S3 nel Cloud.
example@sqldat.com:~ $ barman-cloud-backup --help
usage: barman-cloud-backup [-V] [--help] [-v | -q] [-P PROFILE] [-z | -j]
[-e {AES256,aws:kms}] [-t] [-h HOST] [-p PORT]
[-U USER] [--immediate-checkpoint] [-J JOBS]
[-S MAX_ARCHIVE_SIZE] [--endpoint-url ENDPOINT_URL]
destination_url server_name
This script can be used to perform a backup of a local PostgreSQL instance and
ship the resulting tarball(s) to the Cloud. Currently only AWS S3 is supported.
positional arguments:
destination_url URL of the cloud destination, such as a bucket in AWS
S3. For example: `s3://bucket/path/to/folder`.
server_name the name of the server as configured in Barman.
optional arguments:
-V, --version show program's version number and exit
--help show this help message and exit
-v, --verbose increase output verbosity (e.g., -vv is more than -v)
-q, --quiet decrease output verbosity (e.g., -qq is less than -q)
-P PROFILE, --profile PROFILE
profile name (e.g. INI section in AWS credentials
file)
-z, --gzip gzip-compress the WAL while uploading to the cloud
-j, --bzip2 bzip2-compress the WAL while uploading to the cloud
-e {AES256,aws:kms}, --encryption {AES256,aws:kms}
Enable server-side encryption for the transfer.
Allowed values: 'AES256'|'aws:kms'.
-t, --test Test cloud connectivity and exit
-h HOST, --host HOST host or Unix socket for PostgreSQL connection
(default: libpq settings)
-p PORT, --port PORT port for PostgreSQL connection (default: libpq
settings)
-U USER, --user USER user name for PostgreSQL connection (default: libpq
settings)
--immediate-checkpoint
forces the initial checkpoint to be done as quickly as
possible
-J JOBS, --jobs JOBS number of subprocesses to upload data to S3 (default:
2)
-S MAX_ARCHIVE_SIZE, --max-archive-size MAX_ARCHIVE_SIZE
maximum size of an archive when uploading to S3
(default: 100GB)
--endpoint-url ENDPOINT_URL
Override default S3 endpoint URL with the given one
Ora che abbiamo un'idea più chiara del comando e delle sue opzioni, siamo pronti per eseguire il nostro primo backup su cloud:
example@sqldat.com:~ $ barman-cloud-backup -P barman-cloud \ -e AES256 -j --immediate-checkpoint -J 4 \ s3://barman-s3-test/ pg12
Una volta che il backup è stato completato con successo, il base la directory contenente il backup sarà nel tuo bucket S3. Controlliamolo, costruendo il percorso di destinazione con il nome del server e la base directory:
example@sqldat.com:~ $ aws s3 --profile barman-cloud ls s3://barman-s3-test/pg12/base/
PRE 20200713T120856/
In alternativa, puoi utilizzare barman-cloud-backup-list , ma in questo articolo vorrei concentrarmi sui meccanismi che stanno dietro.
Il 20200711T092548 directory contiene tutti i file relativi al backup che abbiamo appena eseguito. Diamo un'occhiata al suo contenuto:
example@sqldat.com:~ $ aws s3 --profile barman-cloud ls s3://barman-s3-test/pg12/base/20200713T120856/ 2020-07-13 12:09:08 1138 backup.info 2020-07-13 12:09:07 9263096 data.tar.bz2
Come possiamo vedere, esiste un file compresso e crittografato contenente il nostro backup (data.tar.bz2 ) e un file chiamato backup.info contenente le informazioni relative al backup. Possiamo ripristinare il backup copiando e decomprimendo il data.tar.bz2 file sul nostro server locale come mostrato di seguito:
example@sqldat.com:~ $ aws s3 --profile barman-cloud cp s3://barman-s3-test/pg12/base/20200713T120856/data.tar.bz2 restore-dir download: s3://barman-s3-test/pg12/base/20200713T120856/data.tar.bz2 to restore-dir/data.tar.bz2 example@sqldat.com:~ $ cd restore-dir example@sqldat.com:~/restore-dir $ tar xjvf data.tar.bz2 example@sqldat.com:~/restore-dir $ ls PG_VERSION conf.d pg_commit_ts pg_ident.conf pg_notify pg_snapshots pg_subtrans pg_wal postgresql.conf backup_label data.tar.bz2 pg_dynshmem pg_logical pg_replslot pg_stat pg_tblspc pg_xact base global pg_hba.conf pg_multixact pg_serial pg_stat_tmp pg_twophase postgresql.auto.conf
Grande! Come possiamo vedere, tutti i file nel DATADIR dell'istanza PostgreSQL di cui abbiamo eseguito il backup, inclusi i file di configurazione, sono elencati correttamente qui.
Conclusioni
Con il barman-cloud-backup command Barman introduce un'importante funzionalità che consente di eseguire e spedire direttamente backup di base da un server PostgreSQL locale a servizi di cloud object storage compatibili con AWS S3 in pochi semplici passaggi. Supporta crittografia, caricamento parallelo, compressione e consente di risparmiare spazio su disco sul server locale e trasferire in sicurezza i backup nel cloud.
Con il rilascio di Barman 2.11 , successo qualche giorno fa, sono state introdotte importanti novità, tra cui il barman-cloud-wal-restore e il barman-cloud-restore comandi, che consentono di recuperare i file di backup e WAL da un object store come AWS S3. A tal proposito, pubblicheremo a breve un nuovo articolo sul blog che spiegherà come preparare il ripristino di un'istanza PostgreSQL utilizzando questi comandi. Resta sintonizzato.