playbooks/mongo backup.yml

36 lines
1.3 KiB
YAML
Raw Normal View History

2025-02-05 20:45:47 +03:30
- name: Backup MongoDB Databases
hosts: all
become: yes
tasks:
- name: Backup MongoDB databases
command: >
docker exec {{ mongodb_container }} mongodump
--db {{ item }}
--username {{ mongodb_user }}
--password {{ mongodb_password }}
--authenticationDatabase admin
2025-02-05 21:07:36 +03:30
--out {{ mongo_backup_dir }}
2025-02-05 20:45:47 +03:30
with_items: "{{ db_names }}"
2025-02-05 21:07:36 +03:30
- name: Archive each directory separately
command: >
2025-02-05 21:25:25 +03:30
tar -czf "{{ backup_dir }}/{{server}}-{{mongodb_container}}-{{ item }}.tar.gz" -C "{{ backup_dir }}" "{{ item }}"
2025-02-05 21:12:46 +03:30
loop: "{{ db_names }}"
- name: Delete backed-up directories
file:
path: "{{ backup_dir }}/{{ item }}"
state: absent
2025-02-05 21:25:25 +03:30
loop: "{{ db_names }}"
- name: Get the current weekday
shell: date +%a
register: weekday
changed_when: false
- name: send them to s3
command: >
s3cmd put {{ backup_dir }}/{{server}}-{{mongodb_container}}-{{ item }}.tar.gz
2025-02-05 21:30:43 +03:30
{{ s3_folder }}/{{ weekday.stdout }}/{{server}}-{{mongodb_container}}-{{ item }}.tar.gz
2025-02-05 21:25:25 +03:30
with_items: "{{ db_names }}"
- name: Delete backed-up files
file:
path: "{{ backup_dir }}/{{server}}-{{mongodb_container}}-{{ item }}.tar.gz"
state: absent
2025-02-05 21:07:36 +03:30
loop: "{{ db_names }}"