Présentation du système de sauvegarde PostgreSQL wal-g

WAL-G est un outil simple et efficace pour sauvegarder PostgreSQL dans le cloud. Par ses fonctionnalités de base, il est l'héritier de l'outil WAL-E populaire , mais réécrit dans Go. Mais il y a une nouvelle fonctionnalité importante dans WAL-G - les copies delta. Les copies delta WAL-G stockent les pages de fichiers qui ont changé par rapport à une version de sauvegarde précédente. WAL-G implémente plusieurs technologies pour paralléliser les sauvegardes. WAL-G est beaucoup plus rapide que WAL-E.


Les détails de wal-g peuvent être trouvés dans l'article: Sauvegarde de l'overclocking. Conférence Yandex


Le protocole de stockage S3 est devenu populaire pour le stockage de données. L'un des avantages de S3 est la possibilité d'accéder via l'API, qui permet une interaction flexible avec le référentiel, y compris un accès public en lecture, tandis que les informations sont mises à jour dans le référentiel uniquement par des personnes autorisées.


Il existe plusieurs implémentations ouvertes et privées de stockages fonctionnant sous le protocole S3. Aujourd'hui, nous allons examiner une solution populaire pour l'organisation de petits stockages - Minio.


Pour tester wal-g, un serveur PostgreSQL convient et Minio est utilisé en remplacement de S3.


Serveur Minio


Installation de Minio


yum -y install yum-plugin-copr
yum copr enable -y lkiesow/minio
yum install -y minio mc

Modifiez AccessKey et SecretKey dans /etc/minio/minio.conf


vi /etc/minio/minio.conf

Si vous n'utilisez pas nginx avant Minio, vous devez changer


--address 127.0.0.1:9000

--address 0.0.0.0:9000

Générez et ajoutez à MINIO_ACCESS_KEY et MINIO_SECRET_KEY dans /etc/minio/minio.conf


# Custom username or access key of minimum 3 characters in length.
#MINIO_ACCESS_KEY=

# Custom password or secret key of minimum 8 characters in length.
#MINIO_SECRET_KEY=

Lancer Minio


systemctl start minio

web- Minio http://ip---minio:9000 (, pg-backups).



WAL-G rpm ( ). Github, Fedora COPR.


RPM-based .


wal-g rpm , /etc/wal-g.d/server-s3.conf.


backup-fetch.sh
backup-list.sh
backup-push.sh
wal-fetch.sh
wal-g-run.sh
wal-push.sh

wal-g.


yum -y install yum-plugin-copr
yum copr enable -y antonpatsev/wal-g
yum install -y wal-g

wal-g.


wal-g --version
wal-g version v0.2.14

/etc/wal-g.d/server-s3.conf .


, , , PGDATA


#!/bin/bash

export PG_VER="9.6"

export WALE_S3_PREFIX="s3://pg-backups" # ,     S3
export AWS_ACCESS_KEY_ID="xxxx" # AccessKey  /etc/minio/minio.conf 
export AWS_ENDPOINT="http://ip---minio:9000"
export AWS_S3_FORCE_PATH_STYLE="true"
export AWS_SECRET_ACCESS_KEY="yyyy" # SecretKey  /etc/minio/minio.conf

export PGDATA=/var/lib/pgsql/$PG_VER/data/
export PGHOST=/var/run/postgresql/.s.PGSQL.5432 #     PostgreSQL

export WALG_UPLOAD_CONCURRENCY=2 # -    
export WALG_DOWNLOAD_CONCURRENCY=2 # -   
export WALG_UPLOAD_DISK_CONCURRENCY=2 # -     
export WALG_DELTA_MAX_STEPS=7
export WALG_COMPRESSION_METHOD=brotli #    .

WAL-G WALG_DELTA_MAX_STEPS — , base- -, -. , . , , .


.


yum install -y https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm
yum install -y postgresql96 postgresql96-server mc

.


/usr/pgsql-9.6/bin/postgresql96-setup initdb
Initializing database ... OK

1 , wal_level archive PostgreSQL 10 , replica PostgreSQL 10 .


wal_level = archive

WAL 60 PostgreSQL. archive_timeout.


archive_mode = on
archive_command = '/usr/local/bin/wal-push.sh %p'
archive_timeout = 60 #  60     archive_command.

PostgreSQL


systemctl start postgresql-9.6

PostgreSQL : (postgresql-Wed.log ).


tail -fn100 /var/lib/pgsql/9.6/data/pg_log/postgresql-Wed.log

psql.


su - postgres
psql

psql .


test1.


create database test1;

test.


postgres=# \c test1;

indexing_table.


test1=# CREATE TABLE indexing_table(created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW());

.


. 10-20 .


#!/bin/bash
# postgres
while true; do
psql -U postgres -d test1 -c "INSERT INTO indexing_table(created_at) VALUES (CURRENT_TIMESTAMP);"
sleep 60;
done

test1


select * from indexing_table;
2020-01-29 09:41:25.226198+
2020-01-29 09:42:25.336989+
2020-01-29 09:43:25.356069+
2020-01-29 09:44:25.37381+
2020-01-29 09:45:25.392944+
2020-01-29 09:46:25.412327+
2020-01-29 09:47:25.432564+
2020-01-29 09:48:25.451985+
2020-01-29 09:49:25.472653+
2020-01-29 09:50:25.491974+
2020-01-29 09:51:25.510178+

.


WAL:


select pg_switch_xlog();
 PostgreSQL  10:
select pg_switch_wal();

.


su - postgres
/usr/local/bin/backup-push.sh


/usr/local/bin/backup-list.sh


WAL.


Postgresql.


/var/lib/pgsql/9.6/data.


/usr/local/bin/backup-fetch.sh postgres.


su - postgres
/usr/local/bin/backup-fetch.sh

Backup extraction complete.


recovery.conf /var/lib/pgsql/9.6/data .


restore_command = '/usr/local/bin/wal-fetch.sh "%f" "%p"'

PostgreSQL. PostgreSQL recovery WAL, .


systemctl start postgresql-9.6
tail -fn100 /var/lib/pgsql/9.6/data/pg_log/postgresql-Wed.log

.


, recovery.conf recovery_target_time — .


restore_command = '/usr/local/bin/wal-fetch.sh "%f" "%p"'
recovery_target_time = '2020-01-29 09:46:25'

indexing_table


 2020-01-29 09:41:25.226198+00
 2020-01-29 09:42:25.336989+00
 2020-01-29 09:43:25.356069+00
 2020-01-29 09:44:25.37381+00
 2020-01-29 09:45:25.392944+00

PostgreSQL. PostgreSQL recovery WAL, .


systemctl start postgresql-9.6
tail -fn100 /var/lib/pgsql/9.6/data/pg_log/postgresql-Wed.log

PostgreSQL 12 -:


  • restore_command recovery_target_time postgresql.conf
  • $PGDATA/recovery.signal


1GB https://gist.github.com/ololobus/5b25c432f208d7eb31051a5f238dffff


1GB .


postgres=# SELECT pg_size_pretty(pg_database_size('test1'));
pg_size_pretty
----------------
1003 MB

s4cmd est un outil en ligne de commande gratuit pour travailler avec des données situées dans le stockage Amazon S3. L'utilitaire est écrit dans le langage de programmation python et, pour cette raison, il peut être utilisé sur les systèmes d'exploitation Windows et Linux.


Installer s4cmd


pip install s4cmd

Lz4


s4cmd --endpoint-url=http://ip---minio:9000 --access-key=xxxx --secret-key=yyyy du -r s3://pg-backups
840540822       s3://pg-backups/wal_005/
840    lz4  WAL 

   lz4 - 1GB 
time backup_push.sh
real 0m18.582s

 S3    

581480085       s3://pg-backups/basebackups_005/
842374424   s3://pg-backups/wal_005
581    

LZMA


  1 
338413694       s3://pg-backups/wal_005/
338     lzma

   
time backup_push.sh
real    5m25.054s

   S3
270310495       s3://pg-backups/basebackups_005/
433485092   s3://pg-backups/wal_005/

270       lzma

Brotli


  1 
459229886       s3://pg-backups/wal_005/
459     brotli

   
real    0m23.408s

   S3
312960942       s3://pg-backups/basebackups_005/
459309262   s3://pg-backups/wal_005/

312       brotli

Comparaison des résultats sur le graphique.



Comme vous pouvez le voir, Brotli est de taille comparable à LZMA, mais la sauvegarde est effectuée pendant LZ4.


Chat de la communauté russophone PostgreSQL: https://t.me/pgsql


Veuillez mettre une étoile sur Github si vous utilisez wal-g

Source: https://habr.com/ru/post/undefined/


All Articles