Skip to content
Snippets Groups Projects
Commit a42b035e authored by nimrod's avatar nimrod
Browse files

Universal backup script (doesn't have to know about databases before running).

parent 0394c826
No related branches found
No related tags found
No related merge requests found
#!/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
...@@ -24,23 +24,29 @@ ...@@ -24,23 +24,29 @@
- cron - cron
- name: Create database - name: Create database
when: mysql_database is defined
mysql_db: mysql_db:
login_user: root
login_password: '{{ mysql_root_password }}'
collation: utf8_general_ci collation: utf8_general_ci
encoding: utf8 encoding: utf8
name: '{{ mysql_database }}' name: '{{ mysql_database }}'
state: present state: present
- name: Create account - name: Create account
when: mysql_user is defined and mysql_database is defined
mysql_user: mysql_user:
login_user: root
login_password: '{{ mysql_root_password }}'
name: '{{ mysql_user }}' name: '{{ mysql_user }}'
password: '{{ mysql_password }}' password: '{{ mysql_password|default(omit) }}'
priv: '{{ mysql_database }}.*:SELECT,INSERT,UPDATE,DELETE,CREATE,INDEX' priv: '{{ mysql_database }}.*:SELECT,INSERT,UPDATE,DELETE,CREATE,INDEX'
state: present state: present
- name: Add daily backup job - name: Add daily backup job
template: copy:
src: mysql.j2 src: backup.sh
dest: '/etc/cron.daily/mysql_{{ mysql_database }}' dest: /etc/cron.daily/mysql
owner: root owner: root
group: root group: root
mode: '0700' mode: '0755'
#!/bin/sh -e
mysqldump --user root --password {{ mysql_root_password }} --single-transaction --force {{ mysql_database }} > /var/backups/mysql_{{ mysql_database }}.sql
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment