Skip to content

Instantly share code, notes, and snippets.

@andriyun
Last active January 7, 2021 03:06
Show Gist options
  • Save andriyun/083580966f16cee7d0bb to your computer and use it in GitHub Desktop.
Save andriyun/083580966f16cee7d0bb to your computer and use it in GitHub Desktop.
ansible backup playbook
---
- hosts: [hostgroup]
# hostgroup name to aplly playbook from ~/.ansible/hosts
vars:
project_name: [project_name]
# project name for backup files
document_root_location: [document_root_location]
# document root location path without trailing slash
# Example: /home/dtest01
document_root_folder: [document_root_folder]
# document_root_folder name without trailing slash
# Example: public_html
tasks:
- name: Getting timestamp
shell: date +'%Y%m%d%H%M%S'
register: timestamp_raw
- name: Getting name for backup
set_fact: timestamp={{ timestamp_raw.stdout }}
- name: Deleting old backup files
file: path={{ document_root_location }}/backup state=absent
- name: Create backup directory
file: path={{ document_root_location }}/backup state=directory mode=755
- name: Zipping files
shell: "zip -r {{ document_root_location }}/backup/{{ project_name }}_files_{{ timestamp }}.zip {{ document_root_location }}/{{ document_root_folder }}"
- name: Dumping DB
shell: "drush sql-dump | gzip -9 > {{ document_root_location }}/backup/{{ project_name }}_db_{{ timestamp }}.sql.gz"
args:
chdir: "{{ document_root_location }}/{{ document_root_folder }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment