Skip to content

Automate backup using Ansible

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-keygen
ssh-copy-id <user name>@<ip address or name of the host>
-ssh-copy-id [email protected] Server
-ssh-copy-id fayasa[email protected] A
-ssh-copy-id [email protected] B
-ssh-copy-id [email protected] 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-keygen
ssh-copy-id <user name>@<ip address or name of the host>
-ssh-copy-id fayasa[email protected] A
-ssh-copy-id [email protected] B
-ssh-copy-id [email protected] C
  1. 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
[[email protected]]$ cat etc/hosts
127.0.0.1 localhost
192.168.8.102 Host A
192.168.8.103 Host B
192.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

[[email protected]]$ cat ansible-backup/inventory
[infra]
192.168.8.102 Host A
192.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.

[[email protected]]$ cat ansible-backup/host_vars/hostA
name: host A
Ip : 192.168.8.102
folders:
   /home/downloads/xxxx/

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)

[[email protected]]$ 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[[email protected]]$ 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]}}"

infra.yaml

---
- name: This playbook is dedicated to taking backup related to infra servers
  hosts: infra
  gather_facts: False
  tasks:
        - name: Executing tasks via external yml
          include_tasks: backup.yml
          with_items:
            - "{{ folders }}" #Obtained from the host_vars

db.yaml

---
- 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

backup.yaml

---
    - name: Copying files from source server to destination
      synchronize:
         src: "{{item}}"
         dest: "/home/sonic/wordpress/"
         mode: pull
      delegate_to: "{{groups['Backup'][0]}}"
Leave a Reply

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