--- /dev/null
+---
+# 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
+ - mode not in ['backup', 'restore']
+
+ - 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:
+ - "{{ backup_dir }}/{{ hostvars[_node]['ansible_facts']['hostname'] }}"
+ recurse: yes
+ register: file_to_restore
+
+ - name: restore files
+ copy:
+ src: "{{ item.path }}"
+ dest: "{{ item.path | replace(backup_dir + '/' + hostvars[_node]['ansible_facts']['hostname'], '') }}"
+ mode: preserve
+ loop: "{{ file_to_restore.files }}"
+ delegate_to: "{{ target_node }}"
\ No newline at end of file