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
|
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Backup and restore Ceph files
|
|
|
|
hosts: localhost
|
2022-04-13 01:48:18 +08:00
|
|
|
become: true
|
|
|
|
gather_facts: true
|
|
|
|
tasks:
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Exit playbook, if user did not set the source node
|
|
|
|
ansible.builtin.fail:
|
2022-04-13 01:48:18 +08:00
|
|
|
msg: >
|
|
|
|
"You must pass the node name: -e target_node=<inventory_name>.
|
|
|
|
The name must match what is set in your inventory."
|
2022-06-29 14:59:55 +08:00
|
|
|
when:
|
|
|
|
- target_node is not defined
|
|
|
|
or target_node not in groups.get('all', [])
|
2022-04-13 01:48:18 +08:00
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Exit playbook, if user did not set the backup directory
|
|
|
|
ansible.builtin.fail:
|
2022-04-13 01:48:18 +08:00
|
|
|
msg: >
|
|
|
|
"you must pass the backup directory path: -e backup_dir=<backup directory path>"
|
|
|
|
when: backup_dir is not defined
|
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Exit playbook, if user did not set the playbook mode (backup|restore)
|
|
|
|
ansible.builtin.fail:
|
2022-04-13 01:48:18 +08:00
|
|
|
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
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Gather facts on source node
|
|
|
|
ansible.builtin.setup:
|
2022-04-13 01:48:18 +08:00
|
|
|
delegate_to: "{{ target_node }}"
|
|
|
|
delegate_facts: true
|
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Backup mode
|
2022-04-13 01:48:18 +08:00
|
|
|
when: mode == 'backup'
|
|
|
|
block:
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Create a temp directory
|
2022-07-07 23:03:34 +08:00
|
|
|
ansible.builtin.tempfile:
|
|
|
|
state: directory
|
|
|
|
suffix: ansible-archive-ceph
|
|
|
|
register: tmp_dir
|
|
|
|
delegate_to: "{{ target_node }}"
|
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Archive files
|
|
|
|
community.general.archive:
|
2022-07-07 23:03:34 +08:00
|
|
|
path: "{{ item }}"
|
|
|
|
dest: "{{ tmp_dir.path }}/backup{{ item | replace('/', '-') }}.tar"
|
|
|
|
format: tar
|
2024-02-14 18:14:02 +08:00
|
|
|
mode: "0644"
|
2022-04-13 01:48:18 +08:00
|
|
|
delegate_to: "{{ target_node }}"
|
2022-07-07 23:03:34 +08:00
|
|
|
loop:
|
|
|
|
- /etc/ceph
|
|
|
|
- /var/lib/ceph
|
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Create backup directory
|
2022-07-07 23:03:34 +08:00
|
|
|
become: false
|
2024-02-14 18:14:02 +08:00
|
|
|
ansible.builtin.file:
|
2022-07-07 23:03:34 +08:00
|
|
|
path: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}"
|
|
|
|
state: directory
|
2024-02-14 18:14:02 +08:00
|
|
|
mode: "0755"
|
2022-04-13 01:48:18 +08:00
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Backup files
|
|
|
|
ansible.builtin.fetch:
|
2022-07-07 23:03:34 +08:00
|
|
|
src: "{{ tmp_dir.path }}/backup{{ item | replace('/', '-') }}.tar"
|
|
|
|
dest: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/backup{{ item | replace('/', '-') }}.tar"
|
2024-02-14 18:14:02 +08:00
|
|
|
flat: true
|
2022-07-07 23:03:34 +08:00
|
|
|
loop:
|
|
|
|
- /etc/ceph
|
|
|
|
- /var/lib/ceph
|
2022-04-13 01:48:18 +08:00
|
|
|
delegate_to: "{{ target_node }}"
|
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Remove temp directory
|
|
|
|
ansible.builtin.file:
|
2022-07-07 23:03:34 +08:00
|
|
|
path: "{{ tmp_dir.path }}"
|
|
|
|
state: absent
|
|
|
|
delegate_to: "{{ target_node }}"
|
2022-04-13 01:48:18 +08:00
|
|
|
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Restore mode
|
2022-04-13 01:48:18 +08:00
|
|
|
when: mode == 'restore'
|
|
|
|
block:
|
2024-02-14 18:14:02 +08:00
|
|
|
- name: Unarchive files
|
2022-07-07 23:03:34 +08:00
|
|
|
ansible.builtin.unarchive:
|
|
|
|
src: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/backup{{ item | replace('/', '-') }}.tar"
|
|
|
|
dest: "{{ item | dirname }}"
|
2022-07-05 15:58:02 +08:00
|
|
|
loop:
|
|
|
|
- /etc/ceph
|
|
|
|
- /var/lib/ceph
|
|
|
|
delegate_to: "{{ target_node }}"
|