How to backup your Direct Admin server? post
Backup
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.
Instructions
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:
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
with10.0.0.200
ip address and selectedroot
uservps ansible_host=10.0.0.200 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.
[webservers] server1 ansible_host=10.0.0.200 ansible_port=22 ansible_user=root server2 ansible_host=10.0.0.201 ansible_port=22 ansible_user=root server3 ansible_host=10.0.0.202 ansible_port=22 ansible_user=root server4 ansible_host=10.0.0.203 ansible_port=22 ansible_user=root [databases] mysql1 ansible_host=10.0.10.200 ansible_port=22 ansible_user=root mysql2 ansible_host=10.0.10.201 ansible_port=22 ansible_user=root
Create variable file with customized configuration. This file should contain list of directories and files which should be downloaded. More here
paths: - /home/backup/db - /home/root/user_backups
Prepare playbook - more info about playbooks
--- - name: Backup Direct Admin server hosts: all vars_files: - vars.yml gather_facts: false tasks: - name: Register timestamp variable shell: date +%Y_%m_%d register: timestamp - name: Create backup directory local_action: file path="{{ playbook_dir }}/backups/{{ timestamp.stdout }}" state=directory - name: Sync synchronize: recursive=yes mode=pull src={{ item }} dest="{{ playbook_dir }}/{{ timestamp.stdout }}" with_items: paths
This file has 3 tasks. Let me explain what they do:
Register timestamp variable
- initializingtimestamp
variable appropriate to current date e.g. 2016-04-15Create backup directory
- creating backup dir for giventimestamp
in the/backups
directorySync
- downloading each file/directory from given list (stored in/vars.yml
file). This task has enabledrecursive
option which allow for deep syncs
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
Output
PLAY [Backup Direct Admin server] ********************************************* TASK: [Register timestamp variable] ******************************************* changed: [vps] TASK: [Create backup directory] *********************************************** changed: [vps -> 127.0.0.1] TASK: [Sync] ****************************************************************** changed: [vps -> 127.0.0.1] => (item=/home/backup/db) changed: [vps -> 127.0.0.1] => (item=/home/root/user_backups) PLAY RECAP ******************************************************************** vps : ok=3 changed=3 unreachable=0 failed=0
Links & tools
Kategorie: devops