courseworkrep/roles/postgresql/tasks/backup.yml
2024-11-11 00:17:33 +03:00

41 lines
1.2 KiB
YAML

- name: Create backup directory
file:
path: '{{ backup_dir }}'
state: directory
owner: postgres
group: postgres
mode: '0755'
tags:
- backup
- name: Perform database backup
command: >
pg_dump -U {{ postgres_user }} -F c -f "{{ backup_dir }}/db_backup_{{ postgres_db }}_{{ ansible_date_time.iso8601 }}.sql" {{ postgres_db }}
become_user: postgres
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Daily cron full backup
cron:
name: 'PostgreSQL daily full backup'
user: postgres
minute: '0'
hour: '1'
job: "pg_dump -U {{ postgres_user }} -F c {{ postgres_db }} > {{ backup_dir }}/full_db_backup_{{ postgres_db }}_$(date +\\%F-\\%H-%M).sql"
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup
- name: Hourly cron incremental backup
cron:
name: 'PostgreSQL hourly incremental backup'
user: postgres
minute: '0'
job: "pg_dump -U {{ postgres_user }} -F c --data-only --file=\"{{ backup_dir }}/incremental_db_backup_{{ postgres_db }}_$(date +\\%F-\\%H-%M).sql\" {{ postgres_db }}"
environment:
PGPASSWORD: '{{ postgres_password }}'
tags:
- backup