Browse Source

mysql-backups initial based on duplicity role

Fabian Peter Hammerle 7 years ago
parent
commit
66c69acd53
3 changed files with 22 additions and 32 deletions
  1. 1 1
      README.md
  2. 11 14
      defaults/main.yml
  3. 10 17
      tasks/main.yml

+ 1 - 1
README.md

@@ -1,2 +1,2 @@
-# ansible-role-duplicity
+# ansible-role-mysql-backup
 

+ 11 - 14
defaults/main.yml

@@ -1,15 +1,12 @@
-duplicity_package_name: duplicity
-duplicity_backups: []
+mysql_backups: []
 
-# duplicity_backups:
-# - source_path: /etc
-#   selectors:
-#   - option: include
-#     shell_pattern: /etc/apache2
-#   - option: exclude
-#     shell_pattern: /etc/**
-#   target_url: file:///backups/etc
-#   local_user: root
-#   backup_cron_jobs:
-#   - minute: 21
-#     hour: 19
+# mysql_backups:
+# - source_host: mysql.example.me
+#   ssh_user: mysql-backup
+#   ssh_key_path: ~/.ssh/id_rsa
+#   database: db_name
+#   target_dir_path: /backups/db_name
+#   local_user: fabianpeter
+#   cron_jobs:
+#   - hour: 22
+#     minute: 1

+ 10 - 17
tasks/main.yml

@@ -1,27 +1,20 @@
-- name: install duplicity
-  apt:
-    name: duplicity
-    state: present
-  become: yes
+# create user 'mysql-backup'@localhost identified with auth_socket;
+# grant select, lock tables, show view on *.* to 'mysql-backup'@localhost;
+
 - name: generate commands
   set_fact:
+    # escape %
     backup_command: |-
-      duplicity --no-encryption --no-print-statistics --verbosity warning
-      {%- for selector in item.selectors|default([]) %}
-      {% if selector['option'] in ['exclude', 'include'] %}
-       --{{selector['option']}} "{{selector['shell_pattern']}}"
-      {%- endif %}
-      {% endfor %}
-       "{{item.source_path}}" "{{item.target_url}}"
-  with_items: '{{duplicity_backups}}'
-  register: duplicity_backup_commands
+      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"
+  with_items: '{{mysql_backups}}'
+  register: mysql_backup_commands
 - name: setup cron jobs
   blockinfile:
     dest: '/etc/crontab'
-    marker: '# ANSIBLE DUPLICITY BACKUPS {mark}'
+    marker: '# ANSIBLE MYSQL BACKUPS {mark}'
     block: |-
-      {% for backup in duplicity_backup_commands.results %}
-      {% for backup_job in backup.item.backup_cron_jobs %}
+      {% for backup in mysql_backup_commands.results %}
+      {% for backup_job in backup.item.cron_jobs %}
       {{backup_job.minute}} {{backup_job.hour}} * * * {{backup.item.local_user}} {{backup.ansible_facts.backup_command}}
       {% endfor %}
       {% endfor %}