From 7dd38fd226f8c5e271b89be6d45dd0af2e357082 Mon Sep 17 00:00:00 2001 From: Florian RICHER Date: Sun, 21 May 2023 18:31:35 +0200 Subject: [PATCH] [CLOUD] First work of restore backup --- group_vars/all.yml | 3 +- roles/cloud/tasks/main.yml | 6 ++-- roles/cloud/tasks/restore.yml | 42 +++++++++++++++++++++++++ roles/cloud/tasks/restore_nextcloud.yml | 36 --------------------- roles/volume_restore/files/Dockerfile | 6 +++- roles/volume_restore/files/extract.sh | 3 ++ 6 files changed, 55 insertions(+), 41 deletions(-) create mode 100644 roles/cloud/tasks/restore.yml delete mode 100644 roles/cloud/tasks/restore_nextcloud.yml create mode 100644 roles/volume_restore/files/extract.sh diff --git a/group_vars/all.yml b/group_vars/all.yml index c0f6263..964a4f4 100644 --- a/group_vars/all.yml +++ b/group_vars/all.yml @@ -18,4 +18,5 @@ protonmail: restore: cloud: - nextcloud_archive: /home/florian/Documents/Projet/Perso/backup/cloud/nextcloud.latest.tar.gz \ No newline at end of file + nextcloud_archive: false + db_archive: false \ No newline at end of file diff --git a/roles/cloud/tasks/main.yml b/roles/cloud/tasks/main.yml index dd8bec7..555d6af 100644 --- a/roles/cloud/tasks/main.yml +++ b/roles/cloud/tasks/main.yml @@ -6,6 +6,6 @@ - ansible.builtin.import_tasks: cron.yml name: cron -- ansible.builtin.import_tasks: restore_nextcloud.yml - name: restore_nextcloud - when: restore.cloud.nextcloud_archive \ No newline at end of file +- ansible.builtin.import_tasks: restore.yml + name: restore + when: restore.cloud.nextcloud_archive and restore.cloud.db_archive \ No newline at end of file diff --git a/roles/cloud/tasks/restore.yml b/roles/cloud/tasks/restore.yml new file mode 100644 index 0000000..0d74cfe --- /dev/null +++ b/roles/cloud/tasks/restore.yml @@ -0,0 +1,42 @@ +--- + +- name: Copy archives + ansible.builtin.copy: + src: "{{ item.src }}" + dest: "{{ item.dest }}" + loop: + - { src: '{{ restore.cloud.nextcloud_archive }}', dest: 'nextcloud_archive.tar.gz' } + - { src: '{{ restore.cloud.db_archive }}', dest: 'db_archive.tar.gz' } + +- name: Stop nextcloud container + community.docker.docker_compose: + project_src: cloud + state: absent + become: true + +- name: Restore backup + community.docker.docker_container: + name: volume_restore + image: volume_restore:latest + auto_remove: yes + volumes: + - "{{ item.path }}:/backup/archive.tar.gz" + - "{{ item.volume }}:/backup/dest" + loop: + - { path: './nextcloud_archive.tar.gz', volume: 'cloud_nextcloud' } + - { path: './db_archive.tar.gz', volume: 'cloud_db' } + become: true + +- name: Start nextcloud container + community.docker.docker_compose: + project_src: cloud + state: present + become: true + +- name: Remove archive + ansible.builtin.file: + path: "{{ item.path }}" + state: absent + loop: + - { path: 'nextcloud_archive.tar.gz' } + - { path: 'db_archive.tar.gz' } \ No newline at end of file diff --git a/roles/cloud/tasks/restore_nextcloud.yml b/roles/cloud/tasks/restore_nextcloud.yml deleted file mode 100644 index be9d537..0000000 --- a/roles/cloud/tasks/restore_nextcloud.yml +++ /dev/null @@ -1,36 +0,0 @@ ---- - -- name: Copy archive - ansible.builtin.copy: - src: "{{ restore.cloud.nextcloud_archive }}" - dest: archive.tar.gz - -- name: Stop nextcloud container - community.docker.docker_compose: - project_src: cloud - state: absent - services: nextcloud - become: true - -- name: Restore backup - community.docker.docker_container: - name: volume_restore - image: volume_restore:latest - state: present - recreate: true - volumes: - - ./archive.tar.gz:/backup/archive.tar.gz - - cloud_nextcloud:/backup/dest - become: true - -- name: Start nextcloud container - community.docker.docker_compose: - project_src: cloud - state: present - services: nextcloud - become: true - -- name: Remove archive - ansible.builtin.file: - path: archive.tar.gz - state: absent \ No newline at end of file diff --git a/roles/volume_restore/files/Dockerfile b/roles/volume_restore/files/Dockerfile index f7c0bff..9a6da22 100644 --- a/roles/volume_restore/files/Dockerfile +++ b/roles/volume_restore/files/Dockerfile @@ -4,4 +4,8 @@ RUN apk add --update --no-cache tar RUN mkdir -p /backup/dest -CMD ["tar", "-xf", "/backup/archive.tar.gz", "--strip-components=1", "-C", "/backup/dest"] \ No newline at end of file +COPY ./extract.sh /usr/bin/extract + +RUN chmod +x /usr/bin/extract + +CMD ["restore"] \ No newline at end of file diff --git a/roles/volume_restore/files/extract.sh b/roles/volume_restore/files/extract.sh new file mode 100644 index 0000000..5cc231f --- /dev/null +++ b/roles/volume_restore/files/extract.sh @@ -0,0 +1,3 @@ +#!/bin/sh +rm -rf /backup/dest* +tar -xf /backup/archive.tar.gz --strip-components=1 -C /backup/dest \ No newline at end of file