Browse Source

delete new backup if previous one matches

Fabian Peter Hammerle 7 years ago
parent
commit
1d7d2da4cf
1 changed files with 4 additions and 3 deletions
  1. 4 3
      tasks/main.yml

+ 4 - 3
tasks/main.yml

@@ -6,10 +6,11 @@
     # escape %
     # $command .= ' | sed \'s/ AUTO_INCREMENT=[0-9]*\b//\'';
     backup_command: |-
-      {% if item.source_host is defined %}
-      ssh -i {{item.ssh_key_path}} {{item.ssh_user}}@{{item.source_host}}
+      target_path="{{item.target_dir_path}}/$(date -u +'\%Y-\%m-\%dT\%H:\%M:\%SZ').sql"; previous_path="{{item.target_dir_path}}/$(ls --sort=time --format=single-column "{{item.target_dir_path}}" | head --lines 1)";
+      {%- if item.source_host is defined %}
+       ssh -i {{item.ssh_key_path}} {{item.ssh_user}}@{{item.source_host}}
       {%- endif %}
-       /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"
+       /usr/bin/mysqldump --opt --order-by-primary --skip-dump-date {{item.database}} | sed 's$),($),\n($g' >"$target_path"; [ "$previous_path" ] && cmp --quiet "$previous_path" "$target_path" && rm "$target_path"
   with_items: '{{mysql_backups}}'
   register: mysql_backup_commands
 - name: setup cron jobs