Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - hosts: localhost
- vars:
- remote_host: '...'
- dump_name: '{{ ansible_date_time.date }}_{{ ansible_date_time.epoch }}_ansible_dump.sql'
- remote_dump_path: '.../{{ dump_name }}'
- local_dump_path: '/var/www/var/database/{{ dump_name }}'
- local_db_config: 'login_user=root login_password=password encoding=utf8'
- local_db_name: '...'
- remote_db_name: '...'
- remote_db_config: '-u... -p... {{ remote_db_name }}'
- tasks:
- - name: Install/update packages
- apt: name={{ item }} state=latest
- with_items:
- - python-dev
- - libmysqlclient-dev
- - python-pip
- become: yes
- - name: Install the Python MySQLB module
- pip: name=MySQL-python
- become: yes
- - name: Create remote db dump
- command: ssh {{ remote_host }} 'mysqldump {{ remote_db_config }} > {{ remote_dump_path }}'
- - name: Compress dump
- command: ssh {{ remote_host }} 'gzip {{ remote_dump_path }}'
- - name: Get remote dump
- command: scp {{ remote_host }}:'{{ remote_dump_path }}.gz' '{{ local_dump_path }}.gz'
- - name: Remove remote dump
- command: ssh {{ remote_host }} 'rm {{ remote_dump_path }}.gz'
- - name: Unpack remote dump
- command: gzip -d '{{ local_dump_path }}.gz'
- - name: Backup local db
- mysql_db: state=dump name={{ local_db_name }} target='{{ local_dump_path }}_bak.sql' {{ local_db_config }}
- - name: Restore dump
- mysql_db: state=import name={{ local_db_name }} target={{ local_dump_path }} {{ local_db_config }}
Add Comment
Please, Sign In to add comment