MySQL restore and apply scripts example using Ansible

This is an example of a script I use to roll down database environments from production to uat or dev for example.

After the rolldown is complete, this will also automatically apply any upgrade scripts should they exist.

---
- name: Copy the file to the server
  hosts: dbtest01
  tasks: 
    - name: Copy the file
      copy: src=/app/prod_backup.bz2 dest=/backup/prod_backup.bz2
    - name: Remove the existing database
      mysql_db:
        name: test_db
        state: absent
    - name: Create the database
      mysql_db:
        name: test_db
        state: present
    - name: Import the database
      mysql_db:
        state: import
        name: test_db
        target: /backup/prod_backup.bz2

    - name: Check Upgrade Script
      stat: path=/tmp/upgrade.sql
      register: update_script

    - name: Run the Upgrade script if it exists
      mysql_db:
        state: import
        name: test_db
        target: /tmp/upgrade.sql
      when: update_script.stat.exists == True

I hope you find this helpful, I have this called from another shell script (modified for this post) so you can select which machine as in some examples I have two sides that have different data and depending on which one they want rolling down, happy to share all that as well just feel free to get into contact.

I am always interested in your thoughts so if you have any comments or feedback then please feel free to add any comments, or you can mail me  here.

Related Posts Plugin for WordPress, Blogger... 

 

I feel like I should clean my desk more

Apple Watch Data used in a modern trial

Apple Watch Data used in a modern trial