main.yml 1.0 KB

12345678910111213141516171819202122232425
  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. # $command .= ' | sed \'s/ AUTO_INCREMENT=[0-9]*\b//\'';
  7. backup_command: |-
  8. {% if item.source_host is defined %}
  9. ssh -i {{item.ssh_key_path}} {{item.ssh_user}}@{{item.source_host}}
  10. {%- endif %}
  11. /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"
  12. with_items: '{{mysql_backups}}'
  13. register: mysql_backup_commands
  14. - name: setup cron jobs
  15. blockinfile:
  16. dest: '/etc/crontab'
  17. marker: '# ANSIBLE MYSQL BACKUPS {mark}'
  18. block: |-
  19. {% for backup in mysql_backup_commands.results %}
  20. {% for backup_job in backup.item.cron_jobs %}
  21. {{backup_job.minute}} {{backup_job.hour}} * * * {{backup.item.local_user}} {{backup.ansible_facts.backup_command}}
  22. {% endfor %}
  23. {% endfor %}
  24. become: yes