1
0
Fork 0
mirror of https://github.com/roles-ansible/role-postgresql-backup.git synced 2024-08-16 10:19:49 +02:00

update linting and adding hostname to backup file

This commit is contained in:
L3D 2023-03-02 16:21:16 +01:00
parent a37febde0a
commit a3bcbd491c
Signed by: l3d
GPG key ID: CD08445BFF4313D1
5 changed files with 31 additions and 27 deletions

View file

@ -1,29 +1,32 @@
---
- name: combine postgresql_backup configuration
set_fact:
postgresql_backup: "{{ _postgresql_backup|combine(postgresql_backup, recursive=True) }}"
- name: Combine postgresql_backup configuration
ansible.builtin.set_fact:
postgresql_backup: "{{ _postgresql_backup | combine(postgresql_backup, recursive=true) }}"
- include_tasks: versioncheck.yml
- name: Run optional versionscheck
ansible.builtin.include_tasks: versioncheck.yml
when: submodules_versioncheck|bool
- include_tasks: packages.yml
- name: Install required Packages
ansible.builtin.include_tasks: packages.yml
- name: generate backup script
- name: Generate backup script
become: true
template:
ansible.builtin.template:
src: templates/postgresql-backup-script.sh.j2
dest: /opt/postgresql-backup-script.sh
owner: "{{ postgresql_backup.user }}"
backup: true
mode: 0755
- name: add backup script as cronjob
# TODO: Change to systemd timer!
- name: Add backup script as cronjob
become: true
cron:
ansible.builtin.cron:
name: backup postgres database
cron_file: 'wingcon_psql'
minute: "{{ postgresql_backup.cron_minute | default( '*' ) }}"
hour: "{{ postgresql_backup.cron_hour | default( '0' ) }}"
minute: "{{ postgresql_backup.cron_minute | default('*') }}"
hour: "{{ postgresql_backup.cron_hour | default('0') }}"
job: /opt/postgresql-backup-script.sh >/dev/null 2>&1
user: "{{ postgresql_backup.user }}"
when: postgresql_backup.cron

View file

@ -1,13 +1,13 @@
---
- name: "Install PostgreSQL client"
become: true
package:
ansible.builtin.package:
name: "{{ postgresql_backup.required_packages }}"
state: present
- name: "Install tar and gzip"
become: true
package:
ansible.builtin.package:
name: "{{ postgresql_backup.optional_packages }}"
state: present
when: postgresql_backup.create_tar_gz | bool

View file

@ -8,37 +8,38 @@
path: '/etc/.ansible-version'
state: directory
mode: 0755
when: submodules_versioncheck|bool
when: submodules_versioncheck | bool
- name: check playbook version
- name: Check playbook version
become: true
ansible.builtin.slurp:
src: "/etc/.ansible-version/{{ playbook_version_path }}"
register: playbook_version
when: submodules_versioncheck|bool
when: submodules_versioncheck | bool
ignore_errors: true
failed_when: false
- name: Print remote role version
ansible.builtin.debug:
msg: "Remote role version: {{ playbook_version.content | default('Y3VycmVudGx5IG5vdCBkZXBsb3llZAo=') | b64decode | string }}"
when: submodules_versioncheck|bool
when: submodules_versioncheck | bool
- name: Print locale role version
ansible.builtin.debug:
msg: "Local role version: '{{ playbook_version_number|string }}'."
when: submodules_versioncheck|bool
msg: "Local role version: '{{ playbook_version_number | string }}'."
when: submodules_versioncheck | bool
- name: Check if your version is outdated
ansible.builtin.fail:
msg: "Your ansible module has the version '{{ playbook_version_number }}' and is outdated. You need to update it!"
when:
- playbook_version.content|default("Mgo=")|b64decode|int - 1 >= playbook_version_number|int and submodules_versioncheck|bool
- playbook_version.content|default("Mgo=")|b64decode|int - 1 >= playbook_version_number|int and submodules_versioncheck | bool
- name: write new version to remote disk
- name: Write new version to remote disk
become: true
ansible.builtin.copy:
content: "{{ playbook_version_number }}"
dest: "/etc/.ansible-version/{{ playbook_version_path }}"
mode: '0644'
when: submodules_versioncheck|bool
when: submodules_versioncheck | bool
tags: skip_ansible_lint_template-instead-of-copy

View file

@ -20,11 +20,11 @@ backup_databases() {
{% endif %}
if (umask 077 && pg_dump -F {{ postgresql_backup.export_option | string }} -h "{{ db.host | default( 'localhost' ) }}" -U "{{ db.user | default( 'postgres' ) }}" -p "{{ db.port | default( '5432' ) }}" "{{ db.name }}" -f "{{ db.name }}.in_progress.psql"); then
{% if postgresql_backup.create_tar_gz %}
mv "{{ db.name }}.in_progress.psql" "{{ db.name }}.psql"
tar -czvf "{{ db.name }}.psql.tar.gz" "{{ db.name }}.psql"
rm "{{ db.name }}.psql"
mv "{{ db.name }}.in_progress.psql" "{{ db.name }}_{{ db.host | default( 'localhost' )}}.psql"
tar -czvf "{{ db.name }}_{{ db.host | default( 'localhost' )}}.psql.tar.gz" "{{ db.name }}_{{ db.host | default( 'localhost' )}}.psql"
rm "{{ db.name }}_{{ db.host | default( 'localhost' )}}.psql"
{% else %}
mv "{{ db.name }}.in_progress.psql" "{{ db.name }}.psql"
mv "{{ db.name }}.in_progress.psql" "{{ db.name }}_{{ db.host | default( 'localhost' )}}.psql"
{% endif %}
echo "backup of {{ db.name }} successful"
else

View file

@ -17,5 +17,5 @@ _postgresql_backup:
databases: []
# versionscheck
playbook_version_number: 25 # should be int
playbook_version_number: 26 # should be int
playbook_version_path: 'role-postgresql-backup_roles-ansible_github.com.version'