From 98b48a3566021105baf860d2168a153a505f204c Mon Sep 17 00:00:00 2001 From: ace Date: Fri, 17 Feb 2023 18:13:18 +0300 Subject: [PATCH] add restore from backup support --- tasks/RedHat/data_dir.yaml | 19 ++++++++ tasks/restore.yaml | 88 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) create mode 100644 tasks/RedHat/data_dir.yaml create mode 100644 tasks/restore.yaml diff --git a/tasks/RedHat/data_dir.yaml b/tasks/RedHat/data_dir.yaml new file mode 100644 index 0000000..b049e2e --- /dev/null +++ b/tasks/RedHat/data_dir.yaml @@ -0,0 +1,19 @@ +- name: Create data directory + file: + path: "{{ postgresql_data_dir }}" + owner: "postgres" + group: "postgres" + state: directory + +- name: Create override directory for systemd unit + file: + path: "{{ postgresql_system_override_dir }}" + state: directory + +- name: Create override for data directory + copy: + dest: "{{ postgresql_system_override_dir }}/override.conf" + content: | + [Service] + Environment=PGDATA={{ postgresql_data_dir }} + notify: Restart PostgreSQL diff --git a/tasks/restore.yaml b/tasks/restore.yaml new file mode 100644 index 0000000..545ce37 --- /dev/null +++ b/tasks/restore.yaml @@ -0,0 +1,88 @@ +- name: Restore database + when: postgresql_wal_g_force_delete_and_restore + block: + - name: Stop PostgreSQL + systemd: + daemon_reload: true + name: "{{ postgresql_unit_name }}" + state: stopped + masked: no + + - name: Force purge database directory + file: + path: "{{ postgresql_data_dir }}" + state: absent + + - name: Recreate database directory + file: + path: "{{ postgresql_data_dir }}" + state: directory + become_user: postgres + +- name: Check if d folder is empty before proceeding + find: + paths: "{{ postgresql_data_dir }}" + register: postgresql_files_found + +- name: Restore database + when: + - postgresql_files_found.matched == 0 + block: + - name: Restore database dir from backup + shell: "{{ postgresql_wal_g_binary_name ~ ' --config ' ~ postgresql_base_dir ~ '/' ~ postgresql_wal_g_restore_config_name ~ ' backup-fetch ' ~ postgresql_data_dir ~ ' ' ~ postgresql_wal_g_postgresql_pitr }}" + become_user: postgres + + - name: Include PostgreSQL configuration + vars: + postgresql_restore_command: "{{ postgresql_wal_g_binary_name ~ ' --config ' ~ postgresql_base_dir ~ '/' ~ postgresql_wal_g_restore_config_name ~ ' wal-fetch %f %p' }}" + include_tasks: config.yaml + + - name: Create recovery.signal file + file: + name: "{{ postgresql_data_dir }}/recovery.signal" + state: touch + become_user: postgres + + - name: Create standby.signal file + file: + name: "{{ postgresql_data_dir }}/standby.signal" + state: touch + become_user: postgres + + - name: Start PostgreSQL for recovery + systemd: + daemon_reload: true + name: "{{ postgresql_unit_name }}" + state: started + masked: no + register: postgresql_start_for_recovery + + - name: Ping PostgreSQL server + vars: + ansible_python_interpreter: '/usr/bin/env python3' + community.postgresql.postgresql_ping: + db: "{{ postgresql_superuser_db }}" + login_user: "{{ postgresql_superuser_username }}" + login_password: "{{ postgresql_superuser_password }}" + register: postgresql_ping + until: postgresql_ping.is_available + retries: 60 + delay: 20 + + - name: debug + debug: + msg: "{{ postgresql_ping }}" + + - name: Stop PostgreSQL + systemd: + daemon_reload: true + name: "{{ postgresql_unit_name }}" + state: stopped + masked: no + register: postgresql_stop + + - name: Create promote trigger file + file: + name: "{{ postgresql_promote_trigger_file }}" + state: touch + become_user: postgres