WAL-G is a simple and effective tool for backing up PostgreSQL to the cloud. By its basic functionality, it is the heir to the popular WAL-E tool , but rewritten in Go. But there is one important new feature in WAL-G - delta copies. WAL-G delta copies store file pages that have changed from a previous backup version. WAL-G implements quite a few technologies for parallelizing backups. WAL-G is much faster than WAL-E.
Details of wal-g can be found in the article: Overclocking backup. Yandex lecture
The S3 storage protocol has become popular for data storage. One of the advantages of S3 is the ability to access via the API, which allows for flexible interaction with the repository, including public read access, while information is updated in the repository only by authorized persons.
There are several both open and private implementations of storages operating under the S3 protocol. Today we are going to look at a popular solution for organizing small storage - Minio.
For testing wal-g, one PostgreSQL server is suitable, and Minio is used as a replacement for S3.
Minio Server
Minio Installation
yum -y install yum-plugin-copr
yum copr enable -y lkiesow/minio
yum install -y minio mc
Edit AccessKey and SecretKey in /etc/minio/minio.conf
vi /etc/minio/minio.conf
If you will not use nginx before Minio, then you need to change
--address 127.0.0.1:9000
--address 0.0.0.0:9000
Generate and add to MINIO_ACCESS_KEY and MINIO_SECRET_KEY in /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=
Launch 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 is a free command line tool for working with data located in Amazon S3 storage. The utility is written in the python programming language, and due to this it can be used on both Windows and Linux operating systems.
Install 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
Comparison of the results on the graph.

As you can see, Brotli is comparable in size to LZMA, but backup is performed during LZ4.
Chat of the Russian-speaking community PostgreSQL: https://t.me/pgsql
Please put a star on Github if you use wal-g