-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4346734
commit 00a098c
Showing
2 changed files
with
109 additions
and
99 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,73 @@ | ||
--- | ||
- name: Create a temporary directory for database dump | ||
ansible.builtin.tempfile: | ||
state: directory | ||
suffix: moodle_db_dump | ||
register: temp_dump_dir | ||
delegate_to: localhost | ||
|
||
- name: Install necessary packages to do the dump (Postgres) | ||
apt: | ||
pkg: | ||
- postgresql-client | ||
- python-psycopg2 | ||
- libpq-dev | ||
state: present | ||
when: ansible_distribution == 'Debian' and dbengine == 'postgres' | ||
|
||
- name: Install necessary packages to do the dump (Postgres) | ||
yum: | ||
name: | ||
- postgresql-lib | ||
- postgresql-devel | ||
state: present | ||
when: ansible_distribution == 'Redhat' and dbengine == 'postgres' | ||
|
||
- name: Install necessary packages to do the dump (Mysql) | ||
apt: | ||
pkg: | ||
- python-pymysql | ||
state: present | ||
when: ansible_distribution == 'Debian' and dbengine == 'mysql' | ||
|
||
- name: Install necessary packages to do the dump (Mysql) | ||
yum: | ||
name: | ||
- python2-PyMySQL | ||
state: present | ||
when: ansible_distribution == 'Redhat' and dbengine == 'mysql' | ||
- name: Perform Moodle database dump | ||
block: | ||
- name: Install necessary packages for Postgres dump | ||
package: | ||
name: | ||
- postgresql-client | ||
- python3-psycopg2 | ||
- libpq-dev | ||
state: present | ||
when: ansible_distribution == 'Debian' and dbengine == 'postgres' | ||
|
||
- name: Set default dump folder if not set | ||
set_fact: | ||
local_dump_folder: "dumps" | ||
when: not local_dump_folder is defined | ||
- name: Install necessary packages for MySQL dump | ||
package: | ||
name: | ||
- python3-pymysql | ||
state: present | ||
when: ansible_distribution == 'Debian' and dbengine == 'mysql' | ||
|
||
- name: Create local installation directory if it does not exist | ||
file: | ||
path: "{{ local_dump_folder }}" | ||
state: directory | ||
delegate_to: localhost | ||
changed_when: false | ||
- name: Dump MySQL database | ||
community.mysql.mysql_db: | ||
name: "{{ moodle_database.dbname }}" | ||
login_host: "{{ moodle_database.dbhost }}" | ||
login_user: "{{ moodle_database.dbuser }}" | ||
login_password: "{{ moodle_database.password }}" | ||
state: dump | ||
target: "{{ temp_dump_dir.path }}/dump.sql" | ||
when: dbengine == 'mysql' | ||
|
||
- name: Do a database dump (Mysql) | ||
# This assumes that mysqldump is installed (this is the case on a frontend normally) | ||
mysql_db: | ||
name: "{{ moodle_database.dbname }}" | ||
login_host: "{{ moodle_database.dbhost }}" | ||
login_user: "{{ moodle_database.dbuser }}" | ||
login_password: "{{ moodle_database.password }}" | ||
state: dump | ||
target: /tmp/dump.sql.bz2 | ||
changed_when: false | ||
when: dbengine == 'mysql' | ||
- name: Dump PostgreSQL database | ||
community.postgresql.postgresql_db: | ||
name: "{{ moodle_database.dbname }}" | ||
login_host: "{{ moodle_database.dbhost }}" | ||
login_user: "{{ moodle_database.dbuser }}" | ||
login_password: "{{ moodle_database.password }}" | ||
state: dump | ||
target: "{{ temp_dump_dir.path }}/dump.sql" | ||
become: true | ||
become_user: "{{ postgresql_user }}" | ||
when: dbengine == 'postgres' | ||
|
||
- name: Do a database dump (Postgres) | ||
# This assumes that pg_dump is installed (this is the case on a frontend normally) | ||
postgresql_db: | ||
name: "{{ moodle_database.dbname }}" | ||
login_host: "{{ moodle_database.dbhost }}" | ||
login_user: "{{ moodle_database.dbuser }}" | ||
login_password: "{{ moodle_database.password }}" | ||
state: dump | ||
target: /tmp/dump.sql.bz2 | ||
changed_when: false | ||
when: dbengine == 'postgres' | ||
- name: Compress the database dump | ||
archive: | ||
path: "{{ temp_dump_dir.path }}/dump.sql" | ||
dest: "{{ temp_dump_dir.path }}/dump.sql.gz" | ||
format: gz | ||
force_archive: true | ||
delegate_to: localhost | ||
|
||
- name: Compress archive | ||
archive: | ||
path: "/tmp/dbdump.sql" | ||
dest: "/tmp/dbdump.sql.gz" | ||
format: gz | ||
force_archive: true | ||
changed_when: false | ||
- name: Fetch database dump | ||
fetch: | ||
src: "{{ temp_dump_dir.path }}/dump.sql.gz" | ||
dest: "dumps/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.gz" | ||
flat: true | ||
|
||
- name: Fetch archive | ||
fetch: | ||
src: "/tmp/dbdump.sql.gz" | ||
flat: true | ||
dest: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.gz" | ||
rescue: | ||
- name: Debug - Database dump process failed, cleaning up | ||
ansible.builtin.debug: | ||
msg: "An error occurred during the database dump. Cleaning up temporary files." | ||
|
||
- name: Delete Archive | ||
file: | ||
state: absent | ||
path: "/tmp/dbdump.sql.gz" | ||
changed_when: false | ||
always: | ||
- name: Cleanup temporary directory | ||
file: | ||
state: absent | ||
path: "{{ temp_dump_dir.path }}" | ||
delegate_to: localhost |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,50 @@ | ||
--- | ||
|
||
- name: Set default dump folder if not set | ||
set_fact: | ||
local_dump_folder: "dumps" | ||
when: not local_dump_folder is defined | ||
|
||
- name: Create local installation directory if it does not exist | ||
file: | ||
path: "{{ local_dump_folder }}" | ||
- name: Create a temporary directory for backup | ||
ansible.builtin.tempfile: | ||
state: directory | ||
suffix: moodle_backup | ||
register: temp_backup_dir | ||
delegate_to: localhost | ||
|
||
- name: Sync data folder locally | ||
synchronize: | ||
mode: pull | ||
src: "{{ moodle_sitedata }}" | ||
dest: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}" | ||
- name: Perform Moodle data backup | ||
block: | ||
- name: Sync Moodle data folder locally | ||
synchronize: | ||
mode: pull | ||
src: "{{ moodle_sitedata }}" | ||
dest: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}" | ||
register: sync_result | ||
|
||
- name: Archive data folder | ||
archive: | ||
path: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}/" | ||
dest: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.bz2" | ||
format: bz2 | ||
delegate_to: localhost | ||
- name: Verify sync was successful | ||
fail: | ||
msg: "Data synchronization failed!" | ||
when: sync_result is failed | ||
|
||
- name: Archive Moodle data folder | ||
archive: | ||
path: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}" | ||
dest: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.bz2" | ||
format: bz2 | ||
mode: '0644' | ||
delegate_to: localhost | ||
register: archive_result | ||
|
||
- name: Verify archive was created | ||
stat: | ||
path: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.bz2" | ||
register: archive_stat | ||
|
||
- name: Fail if archive was not created | ||
fail: | ||
msg: "Archive creation failed!" | ||
when: not archive_stat.stat.exists | ||
rescue: | ||
- name: Debug - Backup process failed, cleaning up | ||
ansible.builtin.debug: | ||
msg: "An error occurred during backup. Cleaning up temporary directory." | ||
always: | ||
- name: Cleanup temporary directory | ||
file: | ||
state: absent | ||
path: "{{ temp_backup_dir.path }}" | ||
delegate_to: localhost |