Skip to content

Instantly share code, notes, and snippets.

@EvilWolf
Created February 9, 2019 16:03
Show Gist options
  • Save EvilWolf/ac9c3d80ea52b6bd6c0676f96606802c to your computer and use it in GitHub Desktop.
Save EvilWolf/ac9c3d80ea52b6bd6c0676f96606802c to your computer and use it in GitHub Desktop.
Ansible playbook to backup compresed databases to directory
- hosts: mysql
vars:
backup_to: /root/backups/databases/
backup_current_dir: "{{ backup_to }}{{ ansible_date_time.date }}"
backup_age: "7d"
tasks:
- name: Collect mysql databases
shell: 'mysql -u root -e "select schema_name from information_schema.schemata where schema_name not in (\"information_schema\", \"mysql\", \"performance_schema\");"'
register: dblist
become: yes
- name: Create directory for databases
file:
state: directory
path: "{{ backup_current_dir }}"
mode: 0750
become: yes
- name: Backup databases by sepearated file
mysql_db:
state: dump
name: "{{ item }}"
target: "{{ backup_current_dir }}/{{ item }}.gz"
loop: "{{ dblist.stdout_lines | difference('schema_name') }}"
become: yes
- name: Collect old backups
find:
file_type: directory
paths: "{{ backup_to }}"
age: "{{ backup_age }}"
register: old_backups
become: yes
- name: Remove old backups
file:
path: "{{ item.path }}"
state: absent
force: yes
loop: "{{ old_backups.files }}"
become: yes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment