Skip to content

Commit

Permalink
pgbackrest: stanza-create
Browse files Browse the repository at this point in the history
issue: #143

also added an example of pgbackrest_conf parameters for configuring local backup.
  • Loading branch information
vitabaks committed Mar 10, 2023
1 parent 89deb7a commit 392dc34
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 7 deletions.
3 changes: 3 additions & 0 deletions deploy_pgcluster.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,9 @@

- role: patroni

- role: pgbackrest/stanza-create
when: pgbackrest_install|bool

- role: vip-manager
when: not with_haproxy_load_balancing|bool and
(cluster_vip is defined and cluster_vip | length > 0)
Expand Down
58 changes: 58 additions & 0 deletions roles/pgbackrest/stanza-create/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---

- name: Get repo1-path value
set_fact:
repo1_path: "{{ pgbackrest_conf['global'] | selectattr('option', 'equalto', 'repo1-path') | map(attribute='value') | list | first }}"
tags: pgbackrest, pgbackrest_stanza_create

# Create a stanza locally (if "pgbackrest_repo_host" is not set)
- block:
- name: "Make sure the {{ repo1_path }} directory exists"
file:
path: "{{ repo1_path }}"
state: directory
owner: postgres
group: postgres
mode: 0755
when: repo1_path | length > 0

- name: Create stanza "{{ pgbackrest_stanza }}"
become: true
become_user: postgres
command: "pgbackrest --stanza={{ pgbackrest_stanza }} --no-online stanza-create"
register: stanza_create_result
changed_when:
- stanza_create_result.rc == 0
- stanza_create_result.stdout is not search("already exists")
when:
- pgbackrest_repo_host | length < 1
- "'postgres_cluster' in group_names"
tags: pgbackrest, pgbackrest_stanza_create

# Create a stanza on the dedicated repository host
- block:
- name: "Make sure the {{ repo1_path }} directory exists"
file:
path: "{{ repo1_path }}"
state: directory
owner: "{{ pgbackrest_repo_user }}"
group: "{{ pgbackrest_repo_user }}"
mode: 0755
when: repo1_path | length > 0

- name: Create stanza "{{ pgbackrest_stanza }}"
become: true
become_user: "{{ pgbackrest_repo_user }}"
delegate_to: "{{ groups['pgbackret'][0] }}"
run_once: true
command: "pgbackrest --stanza={{ pgbackrest_stanza }} stanza-create"
register: stanza_create_result
changed_when:
- stanza_create_result.rc == 0
- stanza_create_result.stdout is not search("already exists")
when:
- pgbackrest_repo_host | length > 0
- "'pgbackrest' in group_names"
tags: pgbackrest, pgbackrest_stanza_create

...
2 changes: 1 addition & 1 deletion tags.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@
- - pgbackrest_install
- - pgbackrest_conf
- - pgbackrest_ssh_keys
- - pgbackrest_cron
- - pgbackrest_stanza_create
- pg_probackup
- - pg_probackup_repo
- - pg_probackup_install
Expand Down
20 changes: 14 additions & 6 deletions vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -405,24 +405,32 @@ wal_g_patroni_cluster_bootstrap_command: "wal-g backup-fetch {{ postgresql_data_
# pgBackRest
pgbackrest_install: false # or 'true'
pgbackrest_install_from_pgdg_repo: true # or 'false'
pgbackrest_stanza: "stanza_name" # specify your --stanza
pgbackrest_stanza: "{{ patroni_cluster_name }}" # specify your --stanza
pgbackrest_repo_type: "posix" # or "s3", "gcs", "azure"
pgbackrest_repo_host: "10.128.64.50" # dedicated repository host (if repo_type: "posix")
pgbackrest_repo_user: "postgres" # if "repo_host" is set
pgbackrest_repo_host: "" # dedicated repository host
pgbackrest_repo_user: "" # if "repo_host" is set
pgbackrest_conf_file: "/etc/pgbackrest/pgbackrest.conf"
# see more options https://pgbackrest.org/configuration.html
pgbackrest_conf:
global: # [global] section
- { option: "log-level-file", value: "detail" }
- { option: "log-path", value: "/var/log/pgbackrest" }
# - { option: "repo1-host", value: "{{ pgbackrest_repo_host }}" }
# - { option: "repo1-host-user", value: "{{ pgbackrest_repo_user }}" }
- { option: "repo1-type", value: "{{ pgbackrest_repo_type |lower }}" }
- { option: "repo1-host", value: "{{ pgbackrest_repo_host }}" }
- { option: "repo1-host-user", value: "{{ pgbackrest_repo_user }}" }
- { option: "repo1-path", value: "/var/lib/pgbackrest" }
- { option: "repo1-retention-full", value: "4" }
- { option: "start-fast", value: "y" }
- { option: "delta", value: "y" }
- { option: "stop-auto", value: "y" }
- { option: "resume", value: "n" }
- { option: "link-all", value: "y" }
# - { option: "", value: "" }
stanza: # [stanza_name] section
- { option: "pg1-path", value: "{{ postgresql_data_dir }}" }
- { option: "process-max", value: "2" }
- { option: "recovery-option", value: "recovery_target_action=promote" }
- { option: "log-level-console", value: "info" }
- { option: "process-max", value: "4" }
# - { option: "", value: "" }
pgbackrest_patroni_cluster_restore_command:
'/usr/bin/pgbackrest --stanza={{ pgbackrest_stanza }} --delta restore' # restore from latest backup
Expand Down

1 comment on commit 392dc34

@chlordk
Copy link
Contributor

Choose a reason for hiding this comment

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

Wow! That was a lot of code. I thought it was a small task.

Thanks!

Please sign in to comment.