Automate backup using Ansible

I have been reading Ansible and it is a configuration management tool that is widely used in IT industries for automation purposes. Which makes automate and CI?CD process easier.

In this blog post, I will explain how to automate the backup on folders across a group of servers and storing them in the backup server using Ansible. The whole architecture looks like below,
Let's Dive into the Backup
1. To run, our playbook we need to enable shh-key authentication 
 from Ansible Master to All Hosts (Host A, Host B, Host C)
 from Ansible Master to Backup Server

Note- Run from Ansible mater-shh-keygenssh-copy-id <user name>@<ip address or name of the host>-ssh-copy-id fayasak@Backup Server-ssh-copy-id fayasak@Host A-ssh-copy-id fayasak@Host B-ssh-copy-id fayasak@Host C
2. Enable ssh-key authentication from the backup server to other remote Host
Note- Run from Backup server to host contain file to be backup-shh-keygenssh-copy-id <user name>@<ip address or name of the host>-ssh-copy-id fayasak@Host A-ssh-copy-id fayasak@Host B-ssh-copy-id fayasak@Host C
3. Add remote server names to “/etc/hosts”
It’s always easy to remember the name rather than remembering the IP address, therefore open the “/etc/hosts” file and add entries like below “<ip address> <name of server>”
[Ansible@controller]$ cat etc/hosts127.0.0.1 localhost192.168.8.102 Host A192.168.8.103 Host B192.168.8.104 Host C
Do this in both Ansible Master and Backup server

4. Assume Host A and Host B belongs to Infra Team and Host C belongs to DB Team. Therefore, I created an inventory file like below
[Ansible@controller]$ cat ansible-backup/inventory[infra]192.168.8.102 Host A192.168.8.103 Host B[db]192.168.8.104 Host C[bk_server]backup server
5. Ansible Synchronize module is used to copy the contents from the host machine to the backup server using pull and push.
(Regardless of “Push” or “Pull” method, SSH authentication must be created between remote hosts and Backup server in order to enable secure transfer of backup files).
The playbook is executed with respect to a backup server, therefore the mode we are using is “Pull”. That means the backup server is pulling the folder contents from remote hosts. If the playbook is executed with respect to remote hosts, then “Push” mode can be utilized to push the folder contents from remote hosts to the backup server
6. To handle multiple folder locations from the same remote server and to handle other parameters relevant to hosts, create a host variable.
[Ansible@controller]$ cat ansible-backup/host_vars/hostAname: host AIp : 192.168.8.102folders: /home/downloads/xxxx/
7. 2 sets of YAML files were created. 1 set of a file is dedicated to each category (DB group and Infra group) and another YAML file is dedicated to taking backup on servers (This YAML file is called as an external YAML file with respect to earlier specified YAML files which were dedicated to each category)
[Ansible@controller]$ cat ansible-backup/db.yml--- - name: This playbook is dedicated to taking backup related to DB servers hosts: DB gather_facts: False tasks: - name: Executing tasks via external yml include_tasks: backup.yml with_items: - "{{ folders }}" #Obtained from the host_vars


[Ansible@controller]$ cat ansible-backup/backup.yml--- - name: Copying files from source server to destination synchronize: src: "{{item}}" dest: "/home/sonic/wordpress/" mode: pull delegate_to: "{{groups['Backup'][0]}}"


Find the full code here

Question guys!
How are you acknowledge that every folder is being backed up since ansible show its code execution?
----create CSV report on each execution against it source folders from remote host---------
, , ,

No comments:

Post a Comment