Created
September 17, 2020 17:04
-
-
Save logan2211/05e1b65041b92631ab8ab51f807c8134 to your computer and use it in GitHub Desktop.
mysql backup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- name: Backup MariaDB | |
hosts: "{{ mariadb_group }}[-1]" | |
pre_tasks: | |
- name: Install parallel bzip2 | |
package: | |
name: pbzip2 | |
- name: Copy the backup SSH key | |
copy: | |
src: ssh/production/db_backup_id_rsa | |
dest: /root/db_backup_id_rsa | |
owner: root | |
group: root | |
mode: '0600' | |
- name: Create the backup staging directory | |
file: | |
path: "{{ backup_galera_staging_path }}" | |
state: directory | |
owner: root | |
group: root | |
tasks: | |
- name: Backup the databases | |
shell: > | |
TMOUT=0; | |
mysqldump | |
{% for table in backup_galera_ignored_tables %} | |
--ignore-table={{ table }} | |
{% endfor %} | |
--opt | |
--single-transaction | |
{{ item }} | |
| {{ backup_compress_exec }} > {{ backup_galera_staging_path }}/{{ ansible_date_time.date }}-{{ item }}.sql.bz2 | |
loop: "{{ backup_galera_databases }}" | |
changed_when: false | |
tags: | |
- skip_ansible_lint | |
- name: Copy the backups to the target | |
shell: > | |
TMOUT=0; | |
rsync -avz | |
-e "ssh -i /root/db_backup_id_rsa -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" | |
{{ backup_galera_staging_path }}/ | |
{{ backup_remote_ssh_user }}@{{ hostvars[item]['ansible_host'] }}: | |
loop: "{{ groups['backup_hosts_all'] }}" | |
changed_when: false | |
post_tasks: | |
- name: Remove the backup artifacts | |
file: | |
path: "{{ item }}" | |
state: absent | |
loop: | |
- "{{ backup_galera_staging_path }}" | |
- /root/db_backup_id_rsa | |
vars: | |
backup_compress_exec: pbzip2 | |
backup_remote_ssh_user: galerabackup | |
backup_galera_staging_path: /root/backup_staging | |
backup_galera_databases: | |
- database1 | |
- database2 | |
- etc | |
backup_galera_ignored_tables: | |
- database1.ignored1 | |
- database2.wut |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment