8000 Implement configurations for WAL-G by dragarcia · Pull Request #145 · supabase/postgres · GitHub
[go: up one dir, main page]

Skip to content
8000

Implement configurations for WAL-G #145

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Apr 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
2f2862e
chore: create wal-g Linux user
dragarcia Feb 17, 2022
a9497e0
Revert WAL-G backport
dragarcia Feb 17, 2022
116cb57
chore: give read access to PG data directory to groups
dragarcia Feb 17, 2022
c37826b
update configs in postgresql.conf
dragarcia Feb 17, 2022
249c69f
WAL-G specific scripts to be triggered via the admin API
dragarcia Feb 17, 2022
b8ced22
Create config directory for wal-g
dragarcia Feb 17, 2022
4073448
chore: add logging for wal-g processes
dragarcia Feb 23, 2022
aaf657a
ensure bindir when installing pgsodium works for EC2 builds
dragarcia Mar 23, 2022
0b7eb6d
triggere dockerbuild
dragarcia Mar 23, 2022
a7db18e
Run backup in background
dragarcia Mar 23, 2022
a1f6444
Inject WALG_SENTINEL_USER_DATA during backups
dragarcia Mar 23, 2022
e2de144
Add scripts for enabling and disabling walg
dragarcia Mar 23, 2022
0d284a5
removed unused scripts
dragarcia Mar 23, 2022
759ff9d
Merge branch 'develop' of github.com:supabase/postgres into chore/wal…
dragarcia Mar 23, 2022
c6394aa
bump versioning
dragarcia Mar 23, 2022
a8b7e24
cleanup
dragarcia Mar 23, 2022
8a76794
Move logrotate to its own dir
dragarcia Mar 23, 2022
2858abc
cleanup: loop through creation of log files
dragarcia Mar 23, 2022
e6b80eb
set up logrotate for all WAL-G log files
dragarcia Mar 23, 2022
9a0becb
Ensure backup is pointing to the correct log file
dragarcia Mar 23, 2022
ef4bec3
Merge branch 'fix/dockerbuild_bin_dir' of github.com:supabase/postgre…
dragarcia Mar 23, 2022
d12cea6
create postgres linux group
dragarcia Mar 24, 2022
cb94a41
ensure that it's config.json, not .env
dragarcia Mar 24, 2022
a3608fe
allow admin API to edit /etc/wal-g/config.json
dragarcia Mar 24, 2022
aa3e4d4
Merge branch 'develop' of github.com:supabase/postgres into chore/wal…
dragarcia Mar 28, 2022
8e8fa4c
add whitespace
dragarcia Mar 28, 2022
cc1d0e8
script to complete wal-g restoration
dragarcia Mar 28, 2022
2aa9ddd
Merge branch 'develop' of github.com:supabase/postgres into chore/wal…
dragarcia Mar 29, 2022
16e2f4a
update adminapi
dragarcia Mar 29, 2022
460ca54
remove bindir in setup-system
dragarcia Mar 29, 2022
55d4263
create wal-g group and move up setting up of wal-g
dragarcia Mar 29, 2022
efc0149
create wal-g log files towards the end
dragarcia Mar 30, 2022
35d7980
Ensure /var/lib/postgresql/data is owned by postgres:postgres
dragarcia Mar 30, 2022
8f57dc0
fix transfer of file found in subdirectories
dragarcia Mar 30, 2022
8c2184e
set primary groups for postgres and wal-g linux users
dragarcia Mar 30, 2022
e7d1c87
Merge branch 'develop' of github.com:supabase/postgres into chore/wal…
dragarcia Mar 30, 2022
7ace90c
newline
dragarcia Mar 31, 2022
96c7b34
modify wal-g related parameters in separate file
dragarcia Mar 31, 2022
992dcb3
bump to 14.1.0.21
dragarcia Apr 4, 2022
a7a9679
more newlines
dragarcia Apr 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ansible/files/admin_api_scripts/commence_walg_backup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#! /usr/bin/env bash

set -euo pipefail

WALG_SENTINEL_USER_DATA="{ \"backup_id\": $1, \"project_id\": $2 }" nohup wal-g backup-push /var/lib/postgresql/data --config /etc/wal-g/config.json --verify >> /var/log/wal-g/backup-push.log 2>&1 &
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just use systemd-cat? Do we expect this to be extremely verbose?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can get quite verbose depending on the size of the database. For example, this is a full backup done for a ~ 15 GB database.

INFO: 2022/03/30 16:24:20.486917 Selecting the latest backup as the base for the current delta backup...
INFO: 2022/03/30 16:24:20.616928 Reached max delta steps. Doing full backup.
INFO: 2022/03/30 16:24:20.639508 Calling pg_start_backup()
INFO: 2022/03/30 16:24:20.704709 Starting a new tar bundle
INFO: 2022/03/30 16:24:20.704745 Walking ...
INFO: 2022/03/30 16:24:20.704972 Starting part 1 ...
INFO: 2022/03/30 16:24:39.139675 Finished writing part 1.
INFO: 2022/03/30 16:24:39.139706 Starting part 2 ...
INFO: 2022/03/30 16:24:56.362484 Finished writing part 2.
INFO: 2022/03/30 16:24:56.362518 Starting part 3 ...
INFO: 2022/03/30 16:25:12.891977 Finished writing part 3.
INFO: 2022/03/30 16:25:12.892011 Starting part 4 ...
INFO: 2022/03/30 16:25:29.653868 Finished writing part 4.
INFO: 2022/03/30 16:25:29.653915 Starting part 5 ...
INFO: 2022/03/30 16:25:45.906419 Finished writing part 5.
INFO: 2022/03/30 16:25:45.906558 Starting part 6 ...
INFO: 2022/03/30 16:26:10.879299 Finished writing part 6.
INFO: 2022/03/30 16:26:10.879339 Starting part 7 ...
INFO: 2022/03/30 16:26:27.899468 Finished writing part 7.
INFO: 2022/03/30 16:26:27.899497 Starting part 8 ...
INFO: 2022/03/30 16:26:44.314572 Finished writing part 8.
INFO: 2022/03/30 16:26:44.314606 Starting part 9 ...
INFO: 2022/03/30 16:27:00.893701 Finished writing part 9.
INFO: 2022/03/30 16:27:00.893731 Starting part 10 ...
INFO: 2022/03/30 16:27:17.346363 Finished writing part 10.
INFO: 2022/03/30 16:27:17.346395 Starting part 11 ...
INFO: 2022/03/30 16:27:33.721219 Finished writing part 11.
INFO: 2022/03/30 16:27:33.721251 Starting part 12 ...
INFO: 2022/03/30 16:27:50.275346 Finished writing part 12.
INFO: 2022/03/30 16:27:50.275377 Starting part 13 ...
INFO: 2022/03/30 16:28:06.648361 Finished writing part 13.
INFO: 2022/03/30 16:28:06.648392 Starting part 14 ...
INFO: 2022/03/30 16:28:17.527654 Finished writing part 14.
INFO: 2022/03/30 16:28:17.527697 Starting part 15 ...
INFO: 2022/03/30 16:28:28.318634 Finished writing part 15.
INFO: 2022/03/30 16:28:28.318662 Starting part 16 ...
INFO: 2022/03/30 16:28:35.075051 Packing ...
INFO: 2022/03/30 16:28:35.076358 Finished writing part 16.
INFO: 2022/03/30 16:28:35.400641 Starting part 17 ...
INFO: 2022/03/30 16:28:35.400675 /global/pg_control
INFO: 2022/03/30 16:28:35.402910 Finished writing part 17.
INFO: 2022/03/30 16:28:35.403969 Calling pg_stop_backup()
INFO: 2022/03/30 16:28:36.446093 Starting part 18 ...
INFO: 2022/03/30 16:28:36.453716 backup_label
INFO: 2022/03/30 16:28:36.453859 tablespace_map
INFO: 2022/03/30 16:28:36.453963 Finished writing part 18.
INFO: 2022/03/30 16:28:36.580489 Wrote backup with name base_0000000100000007000000D4

Also, would want to split the logs for the different wal-g sub-commands used:

  • backup-push
  • backup-fetch
  • wal-push
  • wal-fetch


echo "WAL-G backup job commenced"
26 changes: 26 additions & 0 deletions ansible/files/admin_api_scripts/complete_walg_restore.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#! /usr/bin/env bash

set -euo pipefail

# disable 169.254.169.254 for postgres
sed -i "/#\\sdon't\\sdelete\\sthe\\s'COMMIT'/ i -I OUTPUT 1 --proto tcp --destination 169.254.169.254 --match owner --uid-owner postgres --jump REJECT\\n" /etc/ufw/before.rules
ufw reload

# move config file to its final location and change its ownership
mv /etc/postgresql/wal-g-config.json /etc/wal-g/config.json
chown wal-g:wal-g /etc/wal-g/config.json


# disable recovery commands in the event of a restart
sed -i "s/.*restore_command/#restore_command/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*recovery_target_time/#recovery_target_time/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*recovery_target_action/#recovery_target_action/" /etc/postgresql-custom/wal-g.conf

# enable archive_command
sed -i "s/.*archive_mode/archive_mode/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*archive_command/archive_command/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*archive_timeout/archive_timeout/" /etc/postgresql-custom/wal-g.conf

systemctl restart postgresql

echo "Cleanup post WAL-G restoration complete"
11 changes: 11 additions & 0 deletions ansible/files/admin_api_scripts/disable_walg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /usr/bin/env bash

set -euo pipefail

sed -i "s/.*archive_mode/#archive_mode/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*archive_command/#archive_command/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*archive_timeout/#archive_timeout/" /etc/postgresql-custom/wal-g.conf

systemctl restart postgresql

echo "WAL-G successfully disabled"
11 changes: 11 additions & 0 deletions ansible/files/admin_api_scripts/enable_walg.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#! /usr/bin/env bash

set -euo pipefail

sed -i "s/.*archive_mode/archive_mode/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*archive_command/archive_command/" /etc/postgresql-custom/wal-g.conf
sed -i "s/.*archive_timeout/archive_timeout/" /etc/postgresql-custom/wal-g.conf

systemctl restart postgresql

echo "WAL-G successfully enabled"
File renamed without changes.
9 changes: 9 additions & 0 deletions ansible/files/logrotate_config/logrotate-walg.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/var/log/wal-g/*.log {
size 50M
rotate 3
copytruncate
delaycompress
compress
notifempty
missingok
}
16 changes: 16 additions & 0 deletions ansible/files/postgresql_config/custom_walg.conf.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# - Archiving -

#archive_mode = on
#archive_command = 'sudo -u wal-g wal-g wal-push %p --config /etc/wal-g/config.json >> /var/log/wal-g/wal-push.log 2>&1'
#archive_timeout = 120


# - Archive Recovery -

#restore_command = 'sudo -u wal-g wal-g wal-fetch "%f" "%p" --config /etc/wal-g/config.json >> /var/log/wal-g/wal-fetch.log 2>&1'


# - Recovery Target -

#recovery_target_time = ''
#recovery_target_action = 'promote'
3 changes: 3 additions & 0 deletions ansible/files/postgresql_config/postgresql.conf.j2
Original file line number Diff line number Diff line change
Expand Up @@ -791,6 +791,9 @@ jit_provider = 'llvmjit' # JIT library to use
# User-supplied custom parameters, override any automatically generated ones
#include = '/etc/postgresql-custom/custom-overrides.conf'

# WAL-G specific configurations
#include = '/etc/postgresql-custom/wal-g.conf'

#------------------------------------------------------------------------------
# CUSTOMIZED OPTIONS
#------------------------------------------------------------------------------
Expand Down
23 changes: 12 additions & 11 deletions ansible/playbook.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@
tags:
- install-pgbouncer

- name: Install WAL-G
import_tasks: tasks/setup-wal-g.yml

- name: Install Supabase specific content
import_tasks: tasks/setup-supabase-internal.yml
tags:
Expand Down Expand Up @@ -64,9 +67,6 @@
state: absent
loop: "{{ sql_files }}"

- name: Install WAL-G
import_tasks: tasks/setup-wal-g.yml

- name: Install PostgREST
import_tasks: tasks/setup-postgrest.yml
tags:
Expand Down Expand Up @@ -103,15 +103,16 @@
policy: deny
direction: incoming

- name: Setup logrotate for postgres logs
copy:
src: files/logrotate-postgres
dest: /etc/logrotate.d/postgres

- name: Setup logrotate for postgres CSV logs
- name: Move logrotate files to /etc/logrotate.d/
copy:
src: files/logrotate-postgres-csv
dest: /etc/logrotate.d/postgres-csv
src: "files/logrotate_config/{{ item.file }}"
dest: "/etc/logrotate.d/{{ item.file }}"
mode: "0700"
owner: root
loop:
- { file: "logrotate-postgres-csv.conf" }
- { file: "logrotate-postgres.conf" }
- { file: "logrotate-walg.conf" }

- name: Disable cron access
copy:
Expand Down
20 changes: 15 additions & 5 deletions ansible/tasks/internal/admin-api.yml
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
- name: adminapi - system user
user:
name: adminapi
groups: root,admin,postgres,pgbouncer
groups: root,admin,postgres,pgbouncer,wal-g
append: yes

- name: disk script
- name: Move shell scripts to /root dir
copy:
src: files/grow_fs.sh
dest: /root/grow_fs.sh
mode: '0700'
src: "files/admin_api_scripts/{{ item.file }}"
dest: "/root/{{ item.file }}"
mode: "0700"
owner: root
loop:
- { file: "commence_walg_backup.sh" }
- { file: "complete_walg_restore.sh" }
- { file: "disable_walg.sh" }
- { file: "enable_walg.sh" }
- { file: "grow_fs.sh" }

- name: give adminapi user permissions
copy:
content: |
%adminapi ALL= NOPASSWD: /root/grow_fs.sh
%adminapi ALL= NOPASSWD: /root/commence_walg_backup.sh
%adminapi ALL= NOPASSWD: /root/complete_walg_restore.sh
%adminapi ALL= NOPASSWD: /root/disable_walg.sh
%adminapi ALL= NOPASSWD: /root/enable_walg.sh
%adminapi ALL= NOPASSWD: /usr/bin/systemctl daemon-reload
%adminapi ALL= NOPASSWD: /usr/bin/systemctl restart postgresql.service
%adminapi ALL= NOPASSWD: /usr/bin/systemctl restart adminapi.service
Expand Down
10 changes: 8 additions & 2 deletions ansible/tasks/setup-postgres.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,19 @@
target: install-world-bin
chdir: /tmp/postgresql-{{ postgresql_release }}

- name: Create postgres group
group:
name: postgres
state: present

# Create postgres user
- name: Create postgres user
user:
name: postgres
shell: /bin/false
comment: Postgres user
groups: ssl-cert
group: postgres
groups: postgres,ssl-cert

- name: Create relevant directories
file:
Expand Down Expand Up @@ -141,7 +147,7 @@
become: yes
become_user: postgres
shell:
cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb
cmd: /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data initdb -o "--allow-group-access"
vars:
ansible_command_timeout: 60
# Circumvents the following error:
Expand Down
52 changes: 51 additions & 1 deletion ansible/tasks/setup-wal-g.yml
1241
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
# Download WAL-G
- name: wal-g - download latest version
git:
repo: https://github.com/darora/wal-g.git
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😿

repo: https://github.com/wal-g/wal-g.git
dest: /tmp/wal-g
version: "{{ wal_g_release }}"
become: yes
Expand Down Expand Up @@ -61,6 +61,56 @@
USE_LIBSODIUM: true
become: yes

- name: Create wal-g group
group:
name: wal-g
state: present

- name: Create wal-g user
user:
name: wal-g
shell: /bin/false
comment: WAL-G user
group: wal-g
groups: wal-g, postgres

- name: Give postgres access to execute wal-g binary as wal-g user
copy:
content: |
postgres ALL=(wal-g) NOPASSWD: /usr/local/bin/wal-g
dest: /etc/sudoers.d/postgres

- name: Create a config directory owned by wal-g
file:
path: /etc/wal-g
state: directory
owner: wal-g
group: wal-g
mode: '0760'

- name: Create /etc/wal-g/config.json
file:
path: /etc/wal-g/config.json
state: touch
owner: wal-g
group: wal-g
mode: '0760'

- name: Move custom wal-g.conf file to /etc/postgresql-custom/wal-g.conf
template:
src: "files/postgresql_config/custom_walg.conf.j2"
dest: /etc/postgresql-custom/wal-g.conf
mode: 0664
owner: postgres
group: postgres

- name: Include /etc/postgresql-custom/wal-g.conf in postgresql.conf
become: yes
replace:
path: /etc/postgresql/postgresql.conf
regexp: "#include = '/etc/postgresql-custom/wal-g.conf'"
replace: "include = '/etc/postgresql-custom/wal-g.conf'"

# Clean up Go
- name: Uninstall Go
become: yes
Expand Down
2 changes: 1 addition & 1 deletion ansible/vars.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ golang_version_checksum:
arm64: sha256:06f505c8d27203f78706ad04e47050b49092f1b06dc9ac4fbee4f0e4d015c8d4
amd64: sha256:550f9845451c0c94be679faf116291e7807a8d78b43149f9506c1b15eb89008c

wal_g_release: "v1.1-backports"
wal_g_release: "v1.1"

sfcgal_release: "1.3.10"
sfcgal_release_checksum: sha1:f4add34a00afb0b5f594685fc646565a2bda259b
Expand Down
2 changes: 1 addition & 1 deletion common.vars.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"postgres-version": "14.1.0.20"
"postgres-version": "14.1.0.21"
}
2 changes: 1 addition & 1 deletion scripts/02-credentials_cleanup.sh
Origina E8DF l file line number Diff line number Diff line change
@@ -1 +1 @@
sudo rm /home/ubuntu/.ssh/authorized_keys
sudo rm /home/ubuntu/.ssh/authorized_keys
2 changes: 1 addition & 1 deletion scripts/90-cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ dd if=/dev/zero of=/zerofile &
sleep 5
done
sync; rm /zerofile; sync
cat /dev/null > /var/log/lastlog; cat /dev/null > /var/log/wtmp
cat /dev/null > /var/log/lastlog; cat /dev/null > /var/log/wtmp
8 changes: 7 additions & 1 deletion scripts/91-log_cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,10 @@ touch /var/log/pgbouncer.log
chown pgbouncer:postgres /var/log/pgbouncer.log

mkdir /var/log/postgresql
chown postgres:postgres /var/log/postgresql
chown postgres:postgres /var/log/postgresql

mkdir /var/log/wal-g
cd /var/log/wal-g
touch backup-push.log backup-fetch.log wal-push.log wal-fetch.log
chown -R wal-g:wal-g /var/log/wal-g
chmod -R 0700 /var/log/wal-g
0