2022-04-13 01:48:18 +08:00
|
|
|
---
|
|
|
|
# Copyright Red Hat
|
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
#
|
|
|
|
# This playbook can help in order to backup some Ceph files and restore them later.
|
|
|
|
#
|
|
|
|
# Usage:
|
|
|
|
#
|
|
|
|
# ansible-playbook -i <inventory> backup-and-restore-ceph-files.yml -e backup_dir=<backup directory path> -e mode=<backup|restore> -e target_node=<inventory_name>
|
|
|
|
#
|
|
|
|
# Required run-time variables
|
|
|
|
# ------------------
|
|
|
|
# backup_dir : a path where files will be read|write.
|
|
|
|
# mode : tell the playbook either to backup or restore files.
|
|
|
|
# target_node : the name of the node being processed, it must match the name set in the inventory.
|
|
|
|
#
|
|
|
|
# Examples
|
|
|
|
# --------
|
|
|
|
# ansible-playbook -i hosts, backup-and-restore-ceph-files.yml -e backup_dir=/usr/share/ceph-ansible/backup-ceph-files -e mode=backup -e target_node=mon01
|
|
|
|
# ansible-playbook -i hosts, backup-and-restore-ceph-files.yml -e backup_dir=/usr/share/ceph-ansible/backup-ceph-files -e mode=restore -e target_node=mon01
|
|
|
|
|
|
|
|
- hosts: localhost
|
|
|
|
become: true
|
|
|
|
gather_facts: true
|
|
|
|
tasks:
|
|
|
|
- name: exit playbook, if user did not set the source node
|
|
|
|
fail:
|
|
|
|
msg: >
|
|
|
|
"You must pass the node name: -e target_node=<inventory_name>.
|
|
|
|
The name must match what is set in your inventory."
|
|
|
|
when: target_node is not defined
|
|
|
|
|
|
|
|
- name: exit playbook, if user did not set the backup directory
|
|
|
|
fail:
|
|
|
|
msg: >
|
|
|
|
"you must pass the backup directory path: -e backup_dir=<backup directory path>"
|
|
|
|
when: backup_dir is not defined
|
|
|
|
|
|
|
|
- name: exit playbook, if user did not set the playbook mode (backup|restore)
|
|
|
|
fail:
|
|
|
|
msg: >
|
|
|
|
"you must pass the mode: -e mode=<backup|restore>"
|
|
|
|
when:
|
|
|
|
- mode is not defined
|
2022-06-29 14:40:13 +08:00
|
|
|
or mode not in ['backup', 'restore']
|
2022-04-13 01:48:18 +08:00
|
|
|
|
|
|
|
- name: gather facts on source node
|
|
|
|
setup:
|
|
|
|
delegate_to: "{{ target_node }}"
|
|
|
|
delegate_facts: true
|
|
|
|
|
|
|
|
- name: backup mode
|
|
|
|
when: mode == 'backup'
|
|
|
|
block:
|
|
|
|
- name: find files
|
|
|
|
find:
|
|
|
|
paths:
|
|
|
|
- /etc/ceph
|
|
|
|
- /var/lib/ceph
|
|
|
|
recurse: yes
|
|
|
|
register: file_to_backup
|
|
|
|
delegate_to: "{{ target_node }}"
|
|
|
|
|
|
|
|
- name: backup files
|
|
|
|
fetch:
|
|
|
|
src: "{{ item.path }}"
|
|
|
|
dest: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/{{ item.path }}"
|
|
|
|
flat: yes
|
|
|
|
loop: "{{ file_to_backup.files }}"
|
|
|
|
delegate_to: "{{ target_node }}"
|
|
|
|
|
|
|
|
- name: preserve mode
|
|
|
|
file:
|
|
|
|
path: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/{{ item.path }}"
|
|
|
|
mode: "{{ item.mode }}"
|
|
|
|
owner: "{{ item.uid }}"
|
|
|
|
group: "{{ item.gid }}"
|
|
|
|
loop: "{{ file_to_backup.files }}"
|
|
|
|
|
|
|
|
- name: restore mode
|
|
|
|
when: mode == 'restore'
|
|
|
|
block:
|
|
|
|
- name: get a list of files to be restored
|
|
|
|
find:
|
|
|
|
paths:
|
2022-06-15 16:46:52 +08:00
|
|
|
- "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}"
|
2022-04-13 01:48:18 +08:00
|
|
|
recurse: yes
|
|
|
|
register: file_to_restore
|
|
|
|
|
|
|
|
- name: restore files
|
|
|
|
copy:
|
|
|
|
src: "{{ item.path }}"
|
2022-06-15 16:46:52 +08:00
|
|
|
dest: "{{ item.path | replace(backup_dir + '/' + hostvars[target_node]['ansible_facts']['hostname'], '') }}"
|
2022-04-13 01:48:18 +08:00
|
|
|
mode: preserve
|
|
|
|
loop: "{{ file_to_restore.files }}"
|
|
|
|
delegate_to: "{{ target_node }}"
|