Skip to content

Commit fe28080

Browse files
committed
feat(backup_restore): restore backup files
!26
1 parent 48de278 commit fe28080

File tree

7 files changed

+246
-1
lines changed

7 files changed

+246
-1
lines changed

docs/projects/ansible/playbooks/backup.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,9 @@ backup for:
1515

1616
- mariaDB
1717

18-
- PostgresDB
18+
- PostgresDB
19+
20+
21+
## Files Backup
22+
23+
Backup files created by the playbook can be restored with our [restore](restore.md) playbook

docs/projects/ansible/playbooks/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Available Playbooks:
2323

2424
1. [postgresdb.yaml](postgresdb.md)
2525

26+
1. [restore.yaml](restore.md)
27+
2628

2729
## Docs ToDo
2830

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
---
2+
title: Restore Backup Playbook
3+
description: No Fuss Computings Ansible playbook restore backup
4+
date: 2023-10-30
5+
template: project.html
6+
about: https://gitlab.com/nofusscomputing/projects/ansible/ansible_playbooks
7+
---
8+
9+
!!! Note
10+
Docs Still under development
11+
12+
This Playbook is designed to restore backups to various systems. It uses the configuration as defined in the Ansible Inventory. The specific backup details are from our [backup playbook](backup.md). Currently the following restoration of backups is supported:
13+
14+
- Files
15+
16+
17+
## Files Restoration Task
18+
19+
The file restoration tasks within the playbook will restore backups created by our [backup playbook](backup.md).
20+
21+
22+
### Playbook Variables
23+
24+
The following is the minimum required defined variables in inventory
25+
26+
``` yaml
27+
28+
restore:
29+
backup_storage_directory: /backup
30+
files:
31+
- name: "" # Mandatory, String. {backup_file_name}-{inventory_hostname}
32+
root_directory: '' # Optional, string. root directory where to restore. default=/
33+
34+
backup:
35+
encryption_algorithm: aes256
36+
37+
```
38+
39+
during the running of this playbook, you must specify additional variables `backup_storage_directory` and `restore_backup_date`. for example
40+
41+
``` bash
42+
clear; ansible-playbook --limit {the ansible host to restore the backup to} \
43+
-i inventory/production \
44+
/playbooks/restore.yaml \
45+
--vault-id recovery@recovery_vault \
46+
--extra-vars "restore_backup_date=2023-10-24-13:14:08-+0000" \
47+
--extra-vars "backup_storage_directory=/backup" \
48+
--tags files
49+
```

mkdocs.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ nav:
3737

3838
- projects/ansible/playbooks/postgresdb.md
3939

40+
- projects/ansible/playbooks/restore.md
41+
4042
- Roles:
4143

4244
- projects/ansible/roles/index.md

restore.yaml

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
---
2+
#
3+
# No Fuss Computing
4+
# https://gitlab.com/nofusscomputing/projects/ansible/ansible_playbooks
5+
#
6+
# Playbook for restoring backups various systems.
7+
# tags are optional, with each task only running if applicable variabls are set.
8+
#
9+
# image: nofusscomputing/ansible-ee >= 0.1.0
10+
#
11+
# Tags:
12+
#
13+
# - files
14+
# Restore Specified backup files to path
15+
#
16+
#
17+
- name: Conduct Restoration of Backup(s)
18+
hosts: all
19+
gather_facts: "{% if task_nfc_common_required | default(false) | bool %}false{% else %}true{% endif %}"
20+
21+
tasks:
22+
23+
- name: Common tasks
24+
ansible.builtin.include_role:
25+
name: nfc_common
26+
vars:
27+
common_gather_facts: true
28+
when: task_nfc_common_required | default(false) | bool
29+
30+
31+
- name: Restore Files
32+
ansible.builtin.include_tasks:
33+
file: tasks/restore-files.yaml
34+
apply:
35+
tags:
36+
- files
37+
when: restore.files | default([]) | length | int > 0
38+
tags:
39+
- files
40+
41+
vars:
42+
# restore_backup_date:
43+
# backup_storage_directory: /backup
44+
45+
nfc_pb_awx_tower_template:
46+
- name: "Restore specified backup from Inventory config"
47+
ask_limit_on_launch: true
48+
ask_tags_on_launch: true
49+
ask_credentials_on_launch: true
50+
description: |
51+
Playbook to restore backups to various systems using config from inventory
52+
Available tags:
53+
'files' Restore Specified file(s),
54+
If no tag is specified, all tasks will run if the applicable variables are set.
55+
execution_environment: "No Fuss Computing EE"
56+
job_type: "run"
57+
job_tags: files
58+
labels:
59+
- restore
60+
- files
61+
# credentials:
62+
# - "Local"
63+
verbosity: 2
64+
use_fact_cache: true
65+
survey_enabled: true
66+
survey: |
67+
{
68+
"max": 25,
69+
"min": 25,
70+
"type": "text",
71+
"choices": "",
72+
"default": "",
73+
"required": true,
74+
"variable": "restore_backup_date",
75+
"new_question": true,
76+
"question_name": "Date of backup to restore",
77+
"question_description": "Enter the backup Date-Time stamp. this is the suffix of the backup file."
78+
},
79+
{
80+
"max": 100,
81+
"min": 1,
82+
"type": "text",
83+
"choices": "",
84+
"default": "/backup",
85+
"required": true,
86+
"variable": "backup_storage_directory",
87+
"new_question": true,
88+
"question_name": "Backup Storage Directory",
89+
"question_description": "The root directory of where the backups are stored"
90+
}

tasks/restore-files.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
---
2+
#
3+
# No Fuss Computing
4+
# https://gitlab.com/nofusscomputing/projects/ansible/ansible_playbooks
5+
#
6+
# description:
7+
# Task file for Restoring files to specified path.
8+
#
9+
# image: nofusscomputing/ansible-ee >= 0.1.0
10+
# tags: nil
11+
#
12+
- name: Set Global Facts
13+
ansible.builtin.set_fact:
14+
filename_date_time: "{{ '%Y-%m-%d-%H:%M:%S-%z' | strftime(ansible_date_time.epoch) }}"
15+
when: filename_date_time | default('does_not_exist') == 'does_not_exist'
16+
17+
18+
- name: File Restore
19+
block:
20+
21+
22+
- name: Set Task Facts
23+
ansible.builtin.set_fact:
24+
directory_tasks: '/tmp/task_restore'
25+
26+
27+
- name: Create Task Directory
28+
ansible.builtin.file:
29+
name: "{{ directory_tasks }}"
30+
state: directory
31+
mode: '700'
32+
33+
34+
- name: Prepare Decryption Certificate
35+
ansible.builtin.copy:
36+
content: "{{ file_decryption_key | indent(0) }}"
37+
dest: "{{ directory_tasks }}/decryption_certificate"
38+
mode: '700'
39+
40+
41+
- name: Backup Files
42+
ansible.builtin.include_tasks:
43+
file: restore/files.yaml
44+
loop: "{{ restore.files }}"
45+
loop_control:
46+
loop_var: restore_file
47+
48+
always:
49+
50+
- name: Remove Task Temp Files
51+
ansible.builtin.file:
52+
name: "{{ directory_tasks }}"
53+
state: absent
54+
55+
# Secure erase cert. possible???

tasks/restore/files.yaml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
3+
- name: File Restoration
4+
block:
5+
6+
7+
- name: Check/Create Restore Path
8+
ansible.builtin.file:
9+
name: "{{ restore_file.root_directory | default('/') }}"
10+
state: directory
11+
12+
13+
- name: Restore file
14+
ansible.builtin.shell:
15+
cmd: |
16+
openssl smime -decrypt \
17+
-in {{ backup_storage_directory | default('/backup') }}/{{ restore_backup_date }}-files-{{ restore_file.name }}-{{ backup.encryption_algorithm }}.enc.tar.gz \
18+
-binary -inform DEM \
19+
-inkey {{ directory_tasks }}/decryption_certificate \
20+
-out {{ directory_tasks }}/{{ restore_backup_date }}-files-{{ restore_file.name }}.tar.gz; \
21+
tar -xzf \
22+
{{ directory_tasks }}/{{ restore_backup_date }}-files-{{ restore_file.name }}.tar.gz \
23+
-C {{ restore_file.root_directory | default('/') }};
24+
# rm {{ directory_tasks }}/{{ restore_backup_date }}-files-{{ restore_file.name }}.tar.gz
25+
args:
26+
executable: bash
27+
changed_when: false
28+
register: file_restoration
29+
become: true
30+
failed_when: "'Error' in file_restoration.stderr"
31+
32+
33+
always:
34+
35+
- name: Remove Task Temp Files
36+
ansible.builtin.file:
37+
name: "{{ item }}"
38+
state: absent
39+
loop: "{{ tmp_files }}"
40+
vars:
41+
tmp_files:
42+
- "{{ directory_tasks }}/{{ restore_backup_date }}-files-{{ restore_file.name }}.tar.gz"

0 commit comments

Comments
 (0)