Backup and Restore using ansible playbook

This commit finishes following to-do items for Backup and Restore:
  - Calculate disk size requirement for backup
  - Banner backup and restore

Change-Id: I7be3dc5633bced41be48aa0faaed7a95d613c32a
Story: 2004761
Task: 36044
Signed-off-by: Wei Zhou <wei.zhou@windriver.com>
This commit is contained in:
Wei Zhou 2019-07-31 12:16:24 -04:00
parent a945f4f128
commit 009d8ee096
2 changed files with 84 additions and 21 deletions

View File

@ -8,10 +8,9 @@
# This role is to backup system data.
#
# TODO:
# 1. Check required disk size for backup
# 2. Generate an alarm during backup
# 1. Generate an alarm during backup
#
- name: do backup
- name: Do system backup
block:
- debug:
msg:
@ -20,23 +19,10 @@
- name: Create temp dir
tempfile:
path: /opt/backups
path: "{{ backup_dir }}"
state: directory
register: tempdir
- name: Create ldap temp dir
file:
path: "{{ tempdir.path }}/ldap"
state: directory
register: ldap_dir
- name: Name ldap db backup
set_fact:
ldap_db_backup: "{{ ldap_dir.path }}/ldap.db"
- name: Backup ldap db
command: "slapcat -d 0 -F /etc/openldap/schema -l {{ ldap_db_backup }}"
- name: Create postgres temp dir
file:
path: "{{ tempdir.path }}/postgres"
@ -151,12 +137,63 @@
failed_when: false
register: check_openstack
- name: Fail the backup if maridb is not running
- name: Fail the backup if MariaDB is not running
fail:
msg: "WARNING: {{ mariadb_pod }} is not running. Cannot backup mariadb data."
when: check_openstack.rc == 0
when: check_mariadb_pod.rc != 0
# Now Postgres data and MariaDB data are stored in staging dir, we can estimate
# the disk size requirement for the backup archive.
- name: Check the size (in kilobyte) of directories that will be backed up
shell: "du -sh -k {{ item }} | awk '{print $1}'"
with_items:
- /etc
- /home
- "{{ config_permdir }}"
- "{{ puppet_permdir }}/hieradata"
- "{{ keyring_permdir }}"
- "{{ patching_permdir }}"
- "{{ patching_repo_permdir }}"
- "{{ extension_permdir }}"
- "{{ patch_vault_permdir }}"
- "{{ postgres_dir.path }}"
- "{{ armada_permdir }}"
- "{{ helm_charts_permdir }}"
- "{{ mariadb_dir.path }}"
register: size_output
# Estimate the backup size. We add 128M overhead for things like ceph crushmap,
# ldap data, etc. that will be generated and stored in the staging dir later on.
- name: Estimate the total required disk size for backup archive
set_fact:
total_size_estimation: "{{ total_size_estimation|default(1024*128)|int + item.stdout|int }}"
with_items: "{{ size_output.results }}"
- name: Check if there is enough free space in the archive dir to create backup
shell: "df -k /opt/backups --output=avail | tail -1"
register: available_disk_size
- name: Fail if there is not enough free space in the archive dir to create backup
fail:
msg: >-
Not enough free space in {{ backup_dir }}. It has {{ available_disk_size }}K.
It needs at least {{ total_size_estimation }}K.
when: available_disk_size < total_size_estimation
- name: Create ldap temp dir
file:
path: "{{ tempdir.path }}/ldap"
state: directory
register: ldap_dir
- name: Name ldap db backup
set_fact:
ldap_db_backup: "{{ ldap_dir.path }}/ldap.db"
- name: Backup ldap db
command: "slapcat -d 0 -F /etc/openldap/schema -l {{ ldap_db_backup }}"
- name: Create ceph temp dir
file:
path: "{{ tempdir.path }}/ceph"

View File

@ -31,6 +31,7 @@
- name: Set config path facts for restore
set_fact:
branding_permdir: "{{ config_permdir }}/branding"
banner_permdir: "{{ config_permdir }}/banner/etc"
ssh_config_permdir: "{{ config_permdir }}/ssh_config"
pxe_config_permdir: "{{ config_permdir }}/pxelinux.cfg"
armada_permdir: "{{ platform_path }}/armada/"
@ -39,9 +40,10 @@
# To work around an ansible quirk that regex_replace filter
# is ignored when it is applied to variables in the command module
- name: Strip the leading slash in branding dirname and assign it to a new variable
- name: Strip the leading slash in dirname and assign it to a new variable
set_fact:
short_branding_permdir: "{{ branding_permdir | regex_replace('^\\/', '') }}"
short_banner_permdir: "{{ banner_permdir | regex_replace('^\\/', '') }}"
- name: Restore branding tar file
command: >-
@ -55,7 +57,31 @@
path: /opt/branding/branding
state: absent
# TODO (Wei): Need to backup /opt/banner if it exists and restore it
# before banner optimization is done in persist-config.
- name: Look for banner directory in the backup tarball
shell: "tar -tf {{ staging_dir }}/{{ backup_filename }} | grep -F 'banner/etc'"
args:
warn: false
failed_when: false
register: banner_result
- block:
- name: Create banner directory
file:
path: /opt/banner
state: directory
- name: Restore banner files if they exist in the backup tarball
command: >-
tar -C /opt/banner -xpf {{ staging_dir }}/{{ backup_filename }} --transform='s,.*/,,'
{{ short_banner_permdir }}
args:
warn: false
- name: Remove unwanted directory
file:
path: /opt/banner/etc
state: absent
when: banner_result.rc == 0
become: yes
become_user: root