courseworkrep/roles/postgresql/tasks/backup.yml
2024-12-08 22:11:48 +03:00

33 lines
1.1 KiB
YAML

- name: Create backup directory
file:
path: '{{ backup_dir }}'
state: directory
owner: postgres
group: postgres
mode: '0755'
- 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 }}'
- name: Daily cron full backup
cron:
name: 'PostgreSQL daily full backup {{ postgres_user }}'
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 }}'
- name: Hourly cron incremental backup
cron:
name: 'PostgreSQL hourly incremental backup {{ postgres_user }}'
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 }}'