backup-and-restore: use archive/unarchive approach

current approach is too complex and causes too many issues permission
issues.

Closes: https://bugzilla.redhat.com/show_bug.cgi?id=2051640

Signed-off-by: Guillaume Abrioux <gabrioux@redhat.com>
(cherry picked from commit dffe7b47de)
pull/7249/head
Guillaume Abrioux 2022-07-07 17:03:34 +02:00
parent d07615398c
commit 7b531514ce
1 changed files with 39 additions and 60 deletions

View File

@ -54,74 +54,53 @@
- name: backup mode - name: backup mode
when: mode == 'backup' when: mode == 'backup'
block: block:
- name: find files - name: create a temp directory
find: ansible.builtin.tempfile:
paths: state: directory
- /etc/ceph suffix: ansible-archive-ceph
- /var/lib/ceph register: tmp_dir
recurse: yes
register: file_to_backup
delegate_to: "{{ target_node }}" delegate_to: "{{ target_node }}"
- name: backup files - name: archive files
fetch: archive:
src: "{{ item.path }}" path: "{{ item }}"
dest: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/{{ item.path }}" dest: "{{ tmp_dir.path }}/backup{{ item | replace('/', '-') }}.tar"
flat: yes format: tar
loop: "{{ file_to_backup.files }}"
delegate_to: "{{ target_node }}" delegate_to: "{{ target_node }}"
- name: preserve mode on files
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: stat directories
stat:
path: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}{{ item }}"
register: dir_stat
loop: loop:
- /etc/ceph - /etc/ceph
- /var/lib/ceph - /var/lib/ceph
- name: get a list of files to be restored - name: create backup directory
find: become: false
paths:
- "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}"
recurse: yes
register: file_to_restore
- name: create a list of directories to create
set_fact:
dir_to_create: "{{ dir_to_create | default([]) | union([{'path': item.item | replace(backup_dir + '/' + hostvars[target_node]['ansible_facts']['hostname'], ''), 'uid': item.stat.uid, 'gid': item.stat.gid, 'mode': item.stat.mode}]) }}"
loop: "{{ dir_stat.results }}"
delegate_to: "{{ target_node }}"
- name: create a liste of sub-directories to create
set_fact:
subdir_to_create: "{{ subdir_to_create | default([]) | union([{'path': item.path | dirname | replace(backup_dir + '/' + hostvars[target_node]['ansible_facts']['hostname'], ''), 'uid': item.uid, 'gid': item.gid, 'mode': item.mode}]) }}"
loop: "{{ file_to_restore.files }}"
- name: ensure directories are created
file: file:
path: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}"
state: directory state: directory
path: "{{ item.path }}"
mode: "{{ item.mode }}" - name: backup files
owner: "{{ item.uid }}" fetch:
group: "{{ item.gid }}" src: "{{ tmp_dir.path }}/backup{{ item | replace('/', '-') }}.tar"
loop: "{{ dir_to_create + subdir_to_create }}" dest: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/backup{{ item | replace('/', '-') }}.tar"
flat: yes
loop:
- /etc/ceph
- /var/lib/ceph
delegate_to: "{{ target_node }}" delegate_to: "{{ target_node }}"
- name: restore files - name: remove temp directory
copy: file:
src: "{{ item.path }}" path: "{{ tmp_dir.path }}"
dest: "{{ item.path | replace(backup_dir + '/' + hostvars[target_node]['ansible_facts']['hostname'], '') }}" state: absent
mode: preserve delegate_to: "{{ target_node }}"
loop: "{{ file_to_restore.files }}"
delegate_to: "{{ target_node }}" - name: restore mode
when: mode == 'restore'
block:
- name: unarchive files
ansible.builtin.unarchive:
src: "{{ backup_dir }}/{{ hostvars[target_node]['ansible_facts']['hostname'] }}/backup{{ item | replace('/', '-') }}.tar"
dest: "{{ item | dirname }}"
loop:
- /etc/ceph
- /var/lib/ceph
delegate_to: "{{ target_node }}"