diff --git a/files/backup.sh b/files/backup.sh new file mode 100644 index 0000000000000000000000000000000000000000..5b34c25c82b8aacb256ffb03ee3b2e0fb269db31 --- /dev/null +++ b/files/backup.sh @@ -0,0 +1,21 @@ +#!/bin/sh -e +# Back up all databases, each to a seperate file. + +backup() { + mysqldump --defaults-file=/etc/mysql/debian.cnf \ + --single-transaction \ + --force \ + --result-file=/var/backups/mysql_$1.sql $1 +} + +# TODO: Find a way to remove table formatting from this command. +#alias show='mysqlshow --defaults-file=/etc/mysql/debian.cnf' +alias show="mysql --defaults-file=/etc/mysql/debian.cnf -e 'show databases;'" + +# The reason for dropping the first 4 lines is that the first line is the +# heading and 3 following lines are databases internal to MySQL and thus their +# backup is not needed. +for database in $(show | tail -n+5) +do + backup $database +done diff --git a/tasks/main.yml b/tasks/main.yml index d20fcad29d13eda7c47673ed06b24fe1fd1f8455..f8cb0c4849836d77e7ba7c04132b82df0da6e813 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -24,23 +24,29 @@ - cron - name: Create database + when: mysql_database is defined mysql_db: + login_user: root + login_password: '{{ mysql_root_password }}' collation: utf8_general_ci encoding: utf8 name: '{{ mysql_database }}' state: present - name: Create account + when: mysql_user is defined and mysql_database is defined mysql_user: + login_user: root + login_password: '{{ mysql_root_password }}' name: '{{ mysql_user }}' - password: '{{ mysql_password }}' + password: '{{ mysql_password|default(omit) }}' priv: '{{ mysql_database }}.*:SELECT,INSERT,UPDATE,DELETE,CREATE,INDEX' state: present - name: Add daily backup job - template: - src: mysql.j2 - dest: '/etc/cron.daily/mysql_{{ mysql_database }}' + copy: + src: backup.sh + dest: /etc/cron.daily/mysql owner: root group: root - mode: '0700' + mode: '0755' diff --git a/templates/mysql.j2 b/templates/mysql.j2 deleted file mode 100644 index 2867b901835fb732f590783d52acb2beda04c8d2..0000000000000000000000000000000000000000 --- a/templates/mysql.j2 +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/sh -e -mysqldump --user root --password {{ mysql_root_password }} --single-transaction --force {{ mysql_database }} > /var/backups/mysql_{{ mysql_database }}.sql