main.yml 938 B

123456789101112131415161718192021
  1. # create user 'mysql-backup'@localhost identified with auth_socket;
  2. # grant select, lock tables, show view on *.* to 'mysql-backup'@localhost;
  3. - name: generate commands
  4. set_fact:
  5. # escape %
  6. backup_command: |-
  7. ssh -i {{item.ssh_key_path}} {{item.ssh_user}}@{{item.source_host}} /usr/bin/mysqldump --opt --order-by-primary --skip-dump-date {{item.database}} | sed 's$),($),\n($g' >"{{item.target_dir_path}}/$(date -u +'\%Y-\%m-\%dT\%H:\%M:\%SZ').sql"
  8. with_items: '{{mysql_backups}}'
  9. register: mysql_backup_commands
  10. - name: setup cron jobs
  11. blockinfile:
  12. dest: '/etc/crontab'
  13. marker: '# ANSIBLE MYSQL BACKUPS {mark}'
  14. block: |-
  15. {% for backup in mysql_backup_commands.results %}
  16. {% for backup_job in backup.item.cron_jobs %}
  17. {{backup_job.minute}} {{backup_job.hour}} * * * {{backup.item.local_user}} {{backup.ansible_facts.backup_command}}
  18. {% endfor %}
  19. {% endfor %}
  20. become: yes