commit 922582e57eadbb9b2d43c414add42d4b7a6f2bfb Author: ace Date: Wed Feb 8 02:18:19 2023 +0300 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ba6d390 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.galaxy_install_info diff --git a/README.md b/README.md new file mode 100644 index 0000000..f180772 --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Setup WAL-G for RHEL8 diff --git a/defaults/main.yaml b/defaults/main.yaml new file mode 100644 index 0000000..6a85a52 --- /dev/null +++ b/defaults/main.yaml @@ -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" diff --git a/handlers/main.yaml b/handlers/main.yaml new file mode 100644 index 0000000..2ddb054 --- /dev/null +++ b/handlers/main.yaml @@ -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 diff --git a/tasks/backup_job.yaml b/tasks/backup_job.yaml new file mode 100644 index 0000000..955be31 --- /dev/null +++ b/tasks/backup_job.yaml @@ -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 diff --git a/tasks/backup_job_timer.yaml b/tasks/backup_job_timer.yaml new file mode 100644 index 0000000..5ace49d --- /dev/null +++ b/tasks/backup_job_timer.yaml @@ -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 diff --git a/tasks/backup_permanent_job.yaml b/tasks/backup_permanent_job.yaml new file mode 100644 index 0000000..90e44d5 --- /dev/null +++ b/tasks/backup_permanent_job.yaml @@ -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 diff --git a/tasks/backup_permanent_job_timer.yaml b/tasks/backup_permanent_job_timer.yaml new file mode 100644 index 0000000..e5efdaf --- /dev/null +++ b/tasks/backup_permanent_job_timer.yaml @@ -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 diff --git a/tasks/backup_retention_job.yaml b/tasks/backup_retention_job.yaml new file mode 100644 index 0000000..361150a --- /dev/null +++ b/tasks/backup_retention_job.yaml @@ -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 + diff --git a/tasks/backup_retention_job_timer.yaml b/tasks/backup_retention_job_timer.yaml new file mode 100644 index 0000000..3eb655b --- /dev/null +++ b/tasks/backup_retention_job_timer.yaml @@ -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 diff --git a/tasks/main.yml b/tasks/main.yml new file mode 100644 index 0000000..fd7aa0e --- /dev/null +++ b/tasks/main.yml @@ -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 diff --git a/tasks/pg.yaml b/tasks/pg.yaml new file mode 100644 index 0000000..5c5e1c5 --- /dev/null +++ b/tasks/pg.yaml @@ -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 + diff --git a/templates/backup-permanent.service.j2 b/templates/backup-permanent.service.j2 new file mode 100644 index 0000000..f1ac6c9 --- /dev/null +++ b/templates/backup-permanent.service.j2 @@ -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 diff --git a/templates/backup-permanent.sh.j2 b/templates/backup-permanent.sh.j2 new file mode 100644 index 0000000..4f33e32 --- /dev/null +++ b/templates/backup-permanent.sh.j2 @@ -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>&- diff --git a/templates/backup-permanent.timer.j2 b/templates/backup-permanent.timer.j2 new file mode 100644 index 0000000..5ac4502 --- /dev/null +++ b/templates/backup-permanent.timer.j2 @@ -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 diff --git a/templates/backup-retention.service.j2 b/templates/backup-retention.service.j2 new file mode 100644 index 0000000..ce09bdd --- /dev/null +++ b/templates/backup-retention.service.j2 @@ -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 diff --git a/templates/backup-retention.sh.j2 b/templates/backup-retention.sh.j2 new file mode 100644 index 0000000..b29879c --- /dev/null +++ b/templates/backup-retention.sh.j2 @@ -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>&- diff --git a/templates/backup-retention.timer.j2 b/templates/backup-retention.timer.j2 new file mode 100644 index 0000000..fcf5014 --- /dev/null +++ b/templates/backup-retention.timer.j2 @@ -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 diff --git a/templates/backup.service.j2 b/templates/backup.service.j2 new file mode 100644 index 0000000..f184d65 --- /dev/null +++ b/templates/backup.service.j2 @@ -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 diff --git a/templates/backup.sh.j2 b/templates/backup.sh.j2 new file mode 100644 index 0000000..953ba4c --- /dev/null +++ b/templates/backup.sh.j2 @@ -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>&- diff --git a/templates/backup.timer.j2 b/templates/backup.timer.j2 new file mode 100644 index 0000000..6da0972 --- /dev/null +++ b/templates/backup.timer.j2 @@ -0,0 +1,9 @@ +[Unit] +Description=Run PostgreSQL backup + +[Timer] +Unit=backup.service +OnCalendar={{ wal_g_backup_schedule }} + +[Install] +WantedBy=timers.target diff --git a/templates/walg.json.j2 b/templates/walg.json.j2 new file mode 100644 index 0000000..53e9a39 --- /dev/null +++ b/templates/walg.json.j2 @@ -0,0 +1,7 @@ +{ +{% for wal_g in wal_g_json %} + "{{ wal_g.option }}": "{{ wal_g.value }}"{% if not loop.last %}, +{% endif %} +{% endfor %} + +}