Bobcares

Steps to copy MySQL databases using Ansible

by | Oct 26, 2020

Webmasters can copy a MySQL database from one remote server to another using Ansible as the controller.

As a part of our Server Management Services, we help our Customers with SQL related requests regularly.

Let us today discuss the steps to copy MySQL databases using Ansible.

Copy MySQL databases using Ansible

Let us assume that we need to migrate the database “wordpress” which is hosted in the Production inventory to our development server called (test_development).

Here we will use the mysql_db module to create a dump of the database. Further, using rsync we will copy the database to the other server.

After successfully copying the database will use the mysql_db module to import the database.

---
- hosts: production
become: true
tasks:

- name: create a backup
mysql_db:
name: wordpress
state: dump
target: /var/www/html/wordpress.sql
login_host: localhost
login_user: root
login_password: 123456

Here, we want to store the backup file on the production server, not on the Ansible Controller. Thus, we will set the host to be “production“ in this case.

 

Transferring and importing the backup

The raw module lets us execute a raw shell command. For instance, it helps us to use the rsync command to transfer the backup to the development server (test_development).

- name: copy the database to the development server
raw: rsync /var/www/html/wordpress.sql root@IP_address_here:/home/username/wordpress.sql

 

Finally, we will import the database. Note to replace the IP address, username, and password with the actual values.

- name: import the database
mysql_db:
name: wordpress
state: import
target: /home/username/wordpress.sql
login_host: IP_address_here
login_user: root
login_password: '123456'
delegate_to: test_development

Here, Delegate_to lets us run the command on the remote host machine.

Further, below is the inventory file which we have used in our example to let Ansible identify production and development machines:

[production]
centos ansible_host=IP_address_here ansible_user=root

[development]
test_development ansible_host=IP_address_here ansible_user=root

[Need any further assistance to Copy MySQL databases using Ansible? – We’re available 24*7]

 

Conclusion

In short, database migration can be challenging in Devops, however using Ansible as the controller can help us to copy a MySQL database from one remote server to another. Today, we saw how our Support Engineers perform this task.

 

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.