MySQLDump with Ansible – How to Backup Database

mysqldump

I am trying to backup remote database using ansible and mysqldump. I have installed ansible in my local server.Generated the ssh keys. I also sent the public key to the remote server.
Everything is fine in ansible part but can't backup using ansible and mysqldump.

I tried this:

ansible -m shell -a 'mysqldump -h 192.168.10.155 -u root -proot database_name  > database_name.sql' remote_server

But the backup is being created in home directory of remote_server. My requirement is that I would like to take the backup of remote_server into my local_server.

edit:

Now i using playbook:


  • hosts: remote_server
    become: yes
    tasks:

    • name: do a mysqldump
      ignore_errors: yes
      shell: mysqldump -h 192.168.10.155 -u root -proot database_name > dbname.sql
  • hosts: remote_server
    tasks:

    • name: Transfer file from remote to local

      synchronize:

      src: /home/user/test.txt
      dest: /home/user/backup
      delegate_to: local_server

Backup is happening but how can i get the backup in the server i am running ansible from?

I studied that this can be done using playbook as well.

How can I achieve it with playbook?

Best Answer

I am really sorry to post this one. AS i later realized, this was more of a ansible solutions i was looking for rather than mysqldump.

I tried many alternatives and finally this one worked for me.

enter image description here What this playbook does is, it on the first part takes the database backup using mysqldump and creates the backup in remote_server.

In the later part, it copies the backup from the remote_server to local server(the one hosting the ansible) with fetch command to local directory.

Once it is in the local directory, this can be copied to which ever server we want.

Hope this one helps to someone else as well.

Thank you everyone for the help.