How to backup your Direct Admin server? post


Today I'd like to show you how to prepare very simple backup script using Ansible. But before we start please look on the "backup" definition below.

In information technology, a backup, or the process of backing up, refers to the copying and archiving of computer data so it may be used to restore the original after a data loss event. The verb form is to back up in two words, whereas the noun is backup. - Wikipedia

High level overview

We want to download files from the server with minimal effort. Script can be run once a day (preffered night hours). Tasks should download given list of files/directories onto localhost.

You need to have installed Ansible +1.8.


We must prepare our backup script files structure

mkdir -p backupier/backups && cd backupier
touch backup.yml hosts vars.yml
# result:
/backup.yml     # ansible playbook - with backup instructions
/hosts          # list of nodes which should be backuped
/backups/       # directory storage for backups
/vars.yml       # configuration file

Follow by steps:

  1. Prepare list of servers - inventory file

    Ansible allow to define list of servers as a simple file. In the below sample we have defined one server called vps with ip address and selected root user

    vps ansible_host= ansible_port=22 ansible_user=root

    But if we need backup some servers once a week and others once a day you can group them by time period.

    server1 ansible_host= ansible_port=22 ansible_user=root
    server2 ansible_host= ansible_port=22 ansible_user=root
    server3 ansible_host= ansible_port=22 ansible_user=root
    server4 ansible_host= ansible_port=22 ansible_user=root
    mysql1 ansible_host= ansible_port=22 ansible_user=root
    mysql2 ansible_host= ansible_port=22 ansible_user=root
  2. Create variable file with customized configuration. This file should contain list of directories and files which should be downloaded. More here

      - /home/backup/db
      - /home/root/user_backups
  3. Prepare playbook - more info about playbooks

    - name: Backup Direct Admin server
      hosts: all
        - vars.yml
      gather_facts: false
        - name: Register timestamp variable
          shell: date +%Y_%m_%d
          register: timestamp
        - name: Create backup directory
              path="{{ playbook_dir }}/backups/{{ timestamp.stdout }}" 
        - name: Sync
            src={{ item }} 
            dest="{{ playbook_dir }}/{{ timestamp.stdout }}"
          with_items: paths

This file has 3 tasks. Let me explain what they do:

  • Register timestamp variable - initializing timestamp variable appropriate to current date e.g. 2016-04-15
  • Create backup directory - creating backup dir for given timestamp in the /backups directory
  • Sync - downloading each file/directory from given list (stored in /vars.yml file). This task has enabled recursive option which allow for deep syncs
  1. Let's backup your servers

    # backup all servers
    ansible-playbook -i hosts backup.yml
    # backup mysql servers
    ansible-playbook -i hosts --limit databases backup.yml
    # backup web servers
    ansible-playbook -i hosts --limit webservers backup.yml
  2. Output

    PLAY [Backup Direct Admin server] ********************************************* TASK: [Register timestamp variable] ******************************************* changed: [vps] TASK: [Create backup directory] *********************************************** changed: [vps ->] TASK: [Sync] ****************************************************************** changed: [vps ->] => (item=/home/backup/db) changed: [vps ->] => (item=/home/root/user_backups) PLAY RECAP ******************************************************************** vps : ok=3 changed=3 unreachable=0 failed=0

Links & tools

Categories: devops

Tags: ansible, backup, tools