Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

issue #9 recovery #13

Merged
merged 4 commits into from
May 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,18 @@ openproject_backup_cron:
minute: 15
job: "{{ openproject_backup_command }}"

# recover from backup
openproject_recover_from_backup: false

# expects the files in /var/db/openproject/backup/
openproject_recover_from_backup_dir: /var/db/openproject/backup/

# set this to a defined timestamp, i.e. 20240508065009
# if 0, we will consider the lates fond in
# /var/db/openproject/backup/
openproject_recover_from_backup_timestamp: 0

# db restore option
openproject_recover_from_backup_dbrestorargs: "--no-owner"

# vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 smartindent nu ft=yaml
17 changes: 17 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,21 @@
minute: "{{ openproject_backup_cron.minute }}"
job: "{{ openproject_backup_cron.job }}"

- name: run all notified handlers
ansible.builtin.meta: flush_handlers
tags:
- always

- name: run recover task
ansible.builtin.include_tasks:
file: recover.yml
apply:
tags:
- recover
- openproject-recover
- openproject_recover_from_backup
when:
- openproject_recover_from_backup is defined
- openproject_recover_from_backup | bool

# vim: tabstop=2 expandtab shiftwidth=2 softtabstop=2 smartindent nu ft=yaml
152 changes: 152 additions & 0 deletions tasks/recover.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
---
- name: determine lates timestamp # noqa key-order
block:
- name: get files from {{ openproject_recover_from_backup_dir }}
ansible.builtin.find:
paths: "{{ openproject_recover_from_backup_dir }}"
recurse: false
file_type: file
patterns: conf-*.tar.gz
register: _find_conf

- name: get latest conf file
ansible.builtin.set_fact:
openproject_recover_from_backup_timestamp: "{{ (_find_conf.files | sort(attribute='ctime') | last).path | regex_replace('.*/conf-', '') | replace('.tar.gz', '') }}"
when: _find_conf.matched | int > 0

- name: debug determined timestamp
ansible.builtin.debug:
var: openproject_recover_from_backup_timestamp
verbosity: 1

when: openproject_recover_from_backup_timestamp | default(0) | int == 0

- name: assert we have a timestamp
ansible.builtin.assert:
that:
- openproject_recover_from_backup_timestamp | int > 0

- name: find backup files
ansible.builtin.find:
paths: "{{ openproject_recover_from_backup_dir }}"
recurse: false
file_type: file
patterns:
- "*-{{ openproject_recover_from_backup_timestamp }}.tar.gz"
- "postgresql-dump-{{ openproject_recover_from_backup_timestamp }}.pgdump"
register: _find_backup_files

- name: extract filenames
ansible.builtin.set_fact:
_files_to_process: "{{ _find_backup_files.files | map(attribute='path') }}"

- name: debug _files_to_process
ansible.builtin.debug:
var: _files_to_process
verbosity: 1

- name: define required files
ansible.builtin.set_fact:
_required_postgresql: "{{ [openproject_recover_from_backup_dir, 'postgresql-dump-' ~ openproject_recover_from_backup_timestamp ~ '.pgdump'] | path_join }}"
_required_conf: "{{ [openproject_recover_from_backup_dir, 'conf-' ~ openproject_recover_from_backup_timestamp ~ '.tar.gz'] | path_join }}"
_required_attachments: "{{ [openproject_recover_from_backup_dir, 'attachments-' ~ openproject_recover_from_backup_timestamp ~ '.tar.gz'] | path_join }}"

- name: define optioonal files
ansible.builtin.set_fact:
_optional_svn: "{{ [openproject_recover_from_backup_dir, 'svn-repositories-' ~ openproject_recover_from_backup_timestamp ~ '.tar.gz'] | path_join }}"
_optional_git: "{{ [openproject_recover_from_backup_dir, 'git-repositories-' ~ openproject_recover_from_backup_timestamp ~ '.tar.gz'] | path_join }}"

- name: debug required files
ansible.builtin.debug:
var: item
verbosity: 1
with_items:
- "{{ _required_postgresql }}"
- "{{ _required_conf }}"
- "{{ _required_attachments }}"

- name: debug optional files
ansible.builtin.debug:
var: item
verbosity: 1
with_items:
- "{{ _optional_svn }}"
- "{{ _optional_git }}"

- name: assert important files are present
ansible.builtin.assert:
that:
- item in _files_to_process
with_items:
- "{{ _required_postgresql }}"
- "{{ _required_conf }}"
- "{{ _required_attachments }}"

- name: register DATABASE_URL # noqa no-changed-when
ansible.builtin.command: openproject config:get DATABASE_URL
register: _database_url

- name: debug DATABASE_URL
ansible.builtin.debug:
var: _database_url
verbosity: 1

- name: ensure openproject service is stopped
ansible.builtin.service:
name: openproject
state: stopped

- name: restoring config # noqa command-instead-of-module no-changed-when
ansible.builtin.command:
cmd: "tar xzf {{ _required_conf }} -C /etc/openproject"

- name: ensure file directory exists
ansible.builtin.file:
path: /var/db/openproject/files
state: directory
owner: openproject
group: openproject
mode: '0755'

- name: restoring attachments # noqa command-instead-of-module no-changed-when
ansible.builtin.command:
cmd: "tar xzf {{ _required_attachments }} -C /var/db/openproject/files"

- name: ensure svn directory exists
ansible.builtin.file:
path: /var/db/openproject/svn
state: directory
owner: openproject
group: openproject
mode: '0755'
when: _optional_svn in _files_to_process

- name: restoring svn repositories # noqa command-instead-of-module no-changed-when
ansible.builtin.command:
cmd: "tar xzf {{ _optional_svn }} -C /var/db/openproject/svn"
when: _optional_svn in _files_to_process

- name: ensure git directory exists
ansible.builtin.file:
path: /var/db/openproject/git
state: directory
owner: openproject
group: openproject
mode: '0755'
when: _optional_git in _files_to_process

- name: restoring git repositories # noqa command-instead-of-module no-changed-when
ansible.builtin.command:
cmd: "tar xzf {{ _optional_git }} -C /var/db/openproject/git"
when: _optional_git in _files_to_process

# TODO remove and recreate the db using community.general.postgresql_db

- name: restoring database # noqa no-changed-when
ansible.builtin.command:
cmd: "pg_restore --clean --if-exists {{ openproject_recover_from_backup_dbrestorargs }} --dbname {{ _database_url.stdout }} {{ _required_postgresql }}"

- name: ensure openproject service is started
ansible.builtin.service:
name: openproject
state: started