first commit

This commit is contained in:
ace 2023-02-08 02:18:19 +03:00
commit 922582e57e
No known key found for this signature in database
GPG Key ID: E805F7888C3ED7C0
22 changed files with 397 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
.galaxy_install_info

1
README.md Normal file
View File

@ -0,0 +1 @@
Setup WAL-G for RHEL8

28
defaults/main.yaml Normal file
View File

@ -0,0 +1,28 @@
wal_g_version: "2.0.1-3.el{{ ansible_distribution_major_version }}"
wal_g_pg: no
wal_g_pg_home_dir: "/var/lib/pgsql"
wal_g_pg_binary_name: "wal-g-pg"
wal_g_pg_config_owner: "postgres"
wal_g_pg_config_group: "postgres"
wal_g_pg_backup_owner: "postgres"
wal_g_pg_backup_group: "postgres"
wal_g_pg_package_name_regex: 'postgresql.*server'
wal_g_postgresql_major_version: "14"
wal_g_backup_schedule: ""
wal_g_backup_retention_schedule: ""
wal_g_backup_retention_number: 7
wal_g_backup_permanent_schedule: ""
wal_g_default_config: # more options see https://github.com/wal-g/wal-g#configuration
# AWS_ACCESS_KEY_ID: "secret-key"
# AWS_SECRET_ACCESS_KEY: "access-key"
# AWS_ENDPOINT: "https://s3.example.com"
# WALG_S3_PREFIX: "s3://bucket"
# AWS_S3_FORCE_PATH_STYLE: "true"
WALG_COMPRESSION_METHOD: "brotli"
PGDATA: "/var/lib/pgsql/{{ wal_g_postgresql_major_version }}/data"
PGHOST: "/var/run/postgresql"
# WALG_LIBSODIUM_KEY: "d82560ddab2b096c56ea8c6573dfdf29de8cd6f2bb5e672d1d3b5a89749b8cbd"
WALG_LIBSODIUM_KEY_TRANSFORM: "hex"

50
handlers/main.yaml Normal file
View File

@ -0,0 +1,50 @@
- name: Daemon reload backup service
systemd:
name: backup.service
enabled: no
masked : no
daemon_reload: yes
when: backup_service.changed
- name: Restart backup timer
systemd:
name: backup.timer
enabled: yes
masked : no
state: started
daemon_reload: yes
when: backup_timer.changed
- name: Daemon reload backup retention service
systemd:
name: backup-retention.service
enabled: no
masked : no
daemon_reload: yes
when: backup_retention_service.changed
- name: Restart backup retention timer
systemd:
name: backup-retention.timer
enabled: yes
masked : no
state: started
daemon_reload: yes
when: backup_retention_timer.changed
- name: Daemon reload backup permanent service
systemd:
name: backup-permanent.service
enabled: no
masked : no
daemon_reload: yes
when: backup_permanent_service.changed
- name: Restart backup permanent timer
systemd:
name: backup-permanent.timer
enabled: yes
masked : no
state: started
daemon_reload: yes
when: backup_permanent_timer.changed

29
tasks/backup_job.yaml Normal file
View File

@ -0,0 +1,29 @@
- name: Install curl and jq
dnf:
name:
- jq
- curl
state: present
- name: Template backup script
template:
src: backup.sh.j2
dest: "{{ wal_g_pg_home_dir }}/backup.sh"
mode: 0755
owner: "{{ wal_g_pg_backup_owner }}"
group: "{{ wal_g_pg_backup_group }}"
- name: Template systemd backup service
template:
src: backup.service.j2
dest: /etc/systemd/system/backup.service
notify: Daemon reload backup service
register: backup_service
- name: Reload systemd backup service
systemd:
name: backup.service
enabled: no
masked: no
daemon_reload: yes
when: not backup_service.changed

View File

@ -0,0 +1,15 @@
- name: Template systemd backup timer
template:
src: backup.timer.j2
dest: /etc/systemd/system/backup.timer
notify: Restart backup timer
register: backup_timer
- name: Enable and start systemd backup timer
systemd:
name: backup.timer
enabled: yes
masked: no
state: started
daemon_reload: yes
when: not backup_timer.changed

View File

@ -0,0 +1,29 @@
- name: Install curl and jq
dnf:
name:
- jq
- curl
state: present
- name: Template backup permanent script
template:
src: backup-permanent.sh.j2
dest: "{{ wal_g_pg_home_dir }}/backup-permanent.sh"
mode: 0755
owner: "{{ wal_g_pg_backup_owner }}"
group: "{{ wal_g_pg_backup_group }}"
- name: Template systemd backup permanent service
template:
src: backup-permanent.service.j2
dest: /etc/systemd/system/backup-permanent.service
notify: Daemon reload backup permanent service
register: backup_permanent_service
- name: Reload systemd backup permanent service
systemd:
name: backup-permanent.service
enabled: no
masked: no
daemon_reload: yes
when: not backup_permanent_service.changed

View File

@ -0,0 +1,15 @@
- name: Template systemd backup permanent timer
template:
src: backup-permanent.timer.j2
dest: /etc/systemd/system/backup-permanent.timer
notify: Restart backup permanent timer
register: backup_permanent_timer
- name: Enable and start systemd backup permanent timer
systemd:
name: backup-permanent.timer
enabled: yes
masked: no
state: started
daemon_reload: yes
when: not backup_permanent_timer.changed

View File

@ -0,0 +1,30 @@
- name: Install curl and jq
dnf:
name:
- jq
- curl
state: present
- name: Template backup retention script
template:
src: backup-retention.sh.j2
dest: "{{ wal_g_pg_home_dir }}/backup-retention.sh"
mode: 0755
owner: "{{ wal_g_pg_backup_owner }}"
group: "{{ wal_g_pg_backup_group }}"
- name: Template systemd backup retention service
template:
src: backup-retention.service.j2
dest: /etc/systemd/system/backup-retention.service
notify: Daemon reload backup retention service
register: backup_retention_service
- name: Reload systemd backup retention service
systemd:
name: backup-retention.service
enabled: no
masked: no
daemon_reload: yes
when: not backup_retention_service.changed

View File

@ -0,0 +1,15 @@
- name: Template systemd backup retention timer
template:
src: backup-retention.timer.j2
dest: /etc/systemd/system/backup-retention.timer
notify: Restart backup retention timer
register: backup_retention_timer
- name: Enable and start systemd backup retention timer
systemd:
name: backup-retention.timer
enabled: yes
masked: no
state: started
daemon_reload: yes
when: not backup_retention_timer.changed

40
tasks/main.yml Normal file
View File

@ -0,0 +1,40 @@
---
- name: "Include install WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: pg.yaml
when: wal_g_pg
- name: "Include backup job with WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: backup_job.yaml
when:
- wal_g_pg
- name: "Include backup job timer with WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: backup_job_timer.yaml
when:
- wal_g_pg
- wal_g_backup_schedule is defined
- wal_g_backup_schedule | length > 0
- name: "Include backup retention job with WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: backup_retention_job.yaml
when:
- wal_g_pg
- name: "Include backup retention job timer with WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: backup_retention_job_timer.yaml
when:
- wal_g_pg
- wal_g_backup_retention_schedule is defined
- wal_g_backup_retention_schedule | length > 0
- name: "Include permanent backup job with WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: backup_permanent_job.yaml
when:
- wal_g_pg
- name: "Include permanent backup job timer with WAL-G {{ wal_g_version }} for PostgreSQL"
include_tasks: backup_permanent_job_timer.yaml
when:
- wal_g_pg
- wal_g_backup_permanent_schedule is defined
- wal_g_backup_permanent_schedule | length > 0

39
tasks/pg.yaml Normal file
View File

@ -0,0 +1,39 @@
---
- name: "Install EPEL"
dnf:
name: "epel-release"
state: present
- name: "Install WAL-G for PostgreSQL"
dnf:
name: "wal-g{{ ('-' ~ wal_g_version) | default('') }}"
state: installed
update_cache: yes
allow_downgrade: yes
tags: wal-g, wal_g, wal_g_install
- name: "Gather packages"
package_facts:
manager: auto
- name: "Set fact about PostgreSQL package"
set_fact:
wal_g_postgresql_package: "{{ ansible_facts.packages | list | select('match', wal_g_pg_package_name_regex) | first }}"
- name: "Get PostgreSQL major version"
set_fact:
wal_g_postgresql_major_version: "{{ ansible_facts.packages[wal_g_postgresql_package][0]['version'] | split('.') | first }}"
- name: Merge user options for WAL-G part of config
set_fact:
wal_g_combined_config: "{{ wal_g_default_config | combine(wal_g_config|default({}), recursive=true) }}"
- name: "Generate conf file walg.json"
copy:
content: "{{ wal_g_combined_config | to_nice_json }}"
dest: "{{ wal_g_pg_home_dir }}/.walg.json"
owner: "{{ wal_g_pg_config_owner }}"
group: "{{ wal_g_pg_config_group }}"
mode: 0644
tags: wal-g, wal_g, wal_g_conf

View File

@ -0,0 +1,11 @@
[Unit]
Description=Permanent backup PostgreSQL
[Service]
Type=oneshot
ExecStart=/bin/bash /var/lib/pgsql/backup-permanent.sh
User={{ wal_g_pg_backup_owner }}
Group={{ wal_g_pg_backup_group }}
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,12 @@
exec 200>/tmp/backup.lock
flock -x 200
role=$(curl -k -s http://127.0.0.1:8008 || curl -k -s https://127.0.0.1:8008 | jq '.role')
if [ "${role}" == "\"master\"" ] ; then
{{ wal_g_pg_binary_name }} backup-push {{ wal_g_pg_home_dir }}/{{ wal_g_postgresql_major_version }}/data --full --permanent 2>&1 | tee -a /var/log/postgresql/backup-permanent.log
else
echo ""
fi
exec 200>&-

View File

@ -0,0 +1,9 @@
[Unit]
Description=Run PostgreSQL backup permanent
[Timer]
Unit=backup-permanent.service
OnCalendar={{ wal_g_backup_permanent_schedule }}
[Install]
WantedBy=timers.target

View File

@ -0,0 +1,12 @@
[Unit]
Description=Backups retention for PostgreSQL
Wants=backup-retention.timer
[Service]
Type=oneshot
ExecStart=/bin/bash /var/lib/pgsql/backup-retention.sh
User={{ wal_g_pg_backup_owner }}
Group={{ wal_g_pg_backup_group }}
[Install]
WantedBy=multi-user.target

View File

@ -0,0 +1,12 @@
exec 200>/tmp/backup.lock
flock -x 200
role=$(curl -k -s http://127.0.0.1:8008 || curl -k -s https://127.0.0.1:8008 | jq '.role')
if [ "${role}" == "\"master\"" ] ; then
{{ wal_g_pg_binary_name }} retain {{ wal_g_backup_retention_number }} --confirm --use-sentinel-time 2>&1 | tee -a /var/log/postgresql/backup-retention.log
else
echo ""
fi
exec 200>&-

View File

@ -0,0 +1,9 @@
[Unit]
Description=Run PostgreSQL backup retention
[Timer]
Unit=backup-retention.service
OnCalendar={{ wal_g_backup_retention_schedule }}
[Install]
WantedBy=timers.target

View File

@ -0,0 +1,12 @@
[Unit]
Description=Backup PostgreSQL
Wants=backup.timer
[Service]
Type=oneshot
ExecStart=/bin/bash /var/lib/pgsql/backup.sh
User={{ wal_g_pg_backup_owner }}
Group={{ wal_g_pg_backup_group }}
[Install]
WantedBy=multi-user.target

12
templates/backup.sh.j2 Normal file
View File

@ -0,0 +1,12 @@
exec 200>/tmp/backup.lock
flock -x 200
role=$(curl -k -s http://127.0.0.1:8008 || curl -k -s https://127.0.0.1:8008 | jq '.role')
if [ "${role}" == "\"master\"" ] ; then
{{ wal_g_pg_binary_name }} backup-push {{ wal_g_pg_home_dir }}/{{ wal_g_postgresql_major_version }}/data --full 2>&1 | tee -a /var/log/postgresql/backup.log
else
echo ""
fi
exec 200>&-

View File

@ -0,0 +1,9 @@
[Unit]
Description=Run PostgreSQL backup
[Timer]
Unit=backup.service
OnCalendar={{ wal_g_backup_schedule }}
[Install]
WantedBy=timers.target

7
templates/walg.json.j2 Normal file
View File

@ -0,0 +1,7 @@
{
{% for wal_g in wal_g_json %}
"{{ wal_g.option }}": "{{ wal_g.value }}"{% if not loop.last %},
{% endif %}
{% endfor %}
}