Skip to content

Commit 00a098c

Browse files
committed
Update datadump and filedump
1 parent 4346734 commit 00a098c

File tree

2 files changed

+109
-99
lines changed

2 files changed

+109
-99
lines changed

tasks/commands/moodle-datadump.yml

Lines changed: 64 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,73 @@
11
---
2+
- name: Create a temporary directory for database dump
3+
ansible.builtin.tempfile:
4+
state: directory
5+
suffix: moodle_db_dump
6+
register: temp_dump_dir
7+
delegate_to: localhost
28

3-
- name: Install necessary packages to do the dump (Postgres)
4-
apt:
5-
pkg:
6-
- postgresql-client
7-
- python-psycopg2
8-
- libpq-dev
9-
state: present
10-
when: ansible_distribution == 'Debian' and dbengine == 'postgres'
11-
12-
- name: Install necessary packages to do the dump (Postgres)
13-
yum:
14-
name:
15-
- postgresql-lib
16-
- postgresql-devel
17-
state: present
18-
when: ansible_distribution == 'Redhat' and dbengine == 'postgres'
19-
20-
- name: Install necessary packages to do the dump (Mysql)
21-
apt:
22-
pkg:
23-
- python-pymysql
24-
state: present
25-
when: ansible_distribution == 'Debian' and dbengine == 'mysql'
26-
27-
- name: Install necessary packages to do the dump (Mysql)
28-
yum:
29-
name:
30-
- python2-PyMySQL
31-
state: present
32-
when: ansible_distribution == 'Redhat' and dbengine == 'mysql'
9+
- name: Perform Moodle database dump
10+
block:
11+
- name: Install necessary packages for Postgres dump
12+
package:
13+
name:
14+
- postgresql-client
15+
- python3-psycopg2
16+
- libpq-dev
17+
state: present
18+
when: ansible_distribution == 'Debian' and dbengine == 'postgres'
3319

34-
- name: Set default dump folder if not set
35-
set_fact:
36-
local_dump_folder: "dumps"
37-
when: not local_dump_folder is defined
20+
- name: Install necessary packages for MySQL dump
21+
package:
22+
name:
23+
- python3-pymysql
24+
state: present
25+
when: ansible_distribution == 'Debian' and dbengine == 'mysql'
3826

39-
- name: Create local installation directory if it does not exist
40-
file:
41-
path: "{{ local_dump_folder }}"
42-
state: directory
43-
delegate_to: localhost
44-
changed_when: false
27+
- name: Dump MySQL database
28+
community.mysql.mysql_db:
29+
name: "{{ moodle_database.dbname }}"
30+
login_host: "{{ moodle_database.dbhost }}"
31+
login_user: "{{ moodle_database.dbuser }}"
32+
login_password: "{{ moodle_database.password }}"
33+
state: dump
34+
target: "{{ temp_dump_dir.path }}/dump.sql"
35+
when: dbengine == 'mysql'
4536

46-
- name: Do a database dump (Mysql)
47-
# This assumes that mysqldump is installed (this is the case on a frontend normally)
48-
mysql_db:
49-
name: "{{ moodle_database.dbname }}"
50-
login_host: "{{ moodle_database.dbhost }}"
51-
login_user: "{{ moodle_database.dbuser }}"
52-
login_password: "{{ moodle_database.password }}"
53-
state: dump
54-
target: /tmp/dump.sql.bz2
55-
changed_when: false
56-
when: dbengine == 'mysql'
37+
- name: Dump PostgreSQL database
38+
community.postgresql.postgresql_db:
39+
name: "{{ moodle_database.dbname }}"
40+
login_host: "{{ moodle_database.dbhost }}"
41+
login_user: "{{ moodle_database.dbuser }}"
42+
login_password: "{{ moodle_database.password }}"
43+
state: dump
44+
target: "{{ temp_dump_dir.path }}/dump.sql"
45+
become: true
46+
become_user: "{{ postgresql_user }}"
47+
when: dbengine == 'postgres'
5748

58-
- name: Do a database dump (Postgres)
59-
# This assumes that pg_dump is installed (this is the case on a frontend normally)
60-
postgresql_db:
61-
name: "{{ moodle_database.dbname }}"
62-
login_host: "{{ moodle_database.dbhost }}"
63-
login_user: "{{ moodle_database.dbuser }}"
64-
login_password: "{{ moodle_database.password }}"
65-
state: dump
66-
target: /tmp/dump.sql.bz2
67-
changed_when: false
68-
when: dbengine == 'postgres'
49+
- name: Compress the database dump
50+
archive:
51+
path: "{{ temp_dump_dir.path }}/dump.sql"
52+
dest: "{{ temp_dump_dir.path }}/dump.sql.gz"
53+
format: gz
54+
force_archive: true
55+
delegate_to: localhost
6956

70-
- name: Compress archive
71-
archive:
72-
path: "/tmp/dbdump.sql"
73-
dest: "/tmp/dbdump.sql.gz"
74-
format: gz
75-
force_archive: true
76-
changed_when: false
57+
- name: Fetch database dump
58+
fetch:
59+
src: "{{ temp_dump_dir.path }}/dump.sql.gz"
60+
dest: "dumps/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.gz"
61+
flat: true
7762

78-
- name: Fetch archive
79-
fetch:
80-
src: "/tmp/dbdump.sql.gz"
81-
flat: true
82-
dest: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.gz"
63+
rescue:
64+
- name: Debug - Database dump process failed, cleaning up
65+
ansible.builtin.debug:
66+
msg: "An error occurred during the database dump. Cleaning up temporary files."
8367

84-
- name: Delete Archive
85-
file:
86-
state: absent
87-
path: "/tmp/dbdump.sql.gz"
88-
changed_when: false
68+
always:
69+
- name: Cleanup temporary directory
70+
file:
71+
state: absent
72+
path: "{{ temp_dump_dir.path }}"
73+
delegate_to: localhost

tasks/commands/moodle-filedump.yml

Lines changed: 45 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,50 @@
11
---
2-
3-
- name: Set default dump folder if not set
4-
set_fact:
5-
local_dump_folder: "dumps"
6-
when: not local_dump_folder is defined
7-
8-
- name: Create local installation directory if it does not exist
9-
file:
10-
path: "{{ local_dump_folder }}"
2+
- name: Create a temporary directory for backup
3+
ansible.builtin.tempfile:
114
state: directory
5+
suffix: moodle_backup
6+
register: temp_backup_dir
127
delegate_to: localhost
138

14-
- name: Sync data folder locally
15-
synchronize:
16-
mode: pull
17-
src: "{{ moodle_sitedata }}"
18-
dest: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}"
9+
- name: Perform Moodle data backup
10+
block:
11+
- name: Sync Moodle data folder locally
12+
synchronize:
13+
mode: pull
14+
src: "{{ moodle_sitedata }}"
15+
dest: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}"
16+
register: sync_result
1917

20-
- name: Archive data folder
21-
archive:
22-
path: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}/"
23-
dest: "{{ local_dump_folder }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.bz2"
24-
format: bz2
25-
delegate_to: localhost
18+
- name: Verify sync was successful
19+
fail:
20+
msg: "Data synchronization failed!"
21+
when: sync_result is failed
22+
23+
- name: Archive Moodle data folder
24+
archive:
25+
path: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}"
26+
dest: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.bz2"
27+
format: bz2
28+
mode: '0644'
29+
delegate_to: localhost
30+
register: archive_result
31+
32+
- name: Verify archive was created
33+
stat:
34+
path: "{{ temp_backup_dir.path }}/moodledata-{{ inventory_hostname }}.{{ ansible_date_time.epoch }}.tar.bz2"
35+
register: archive_stat
36+
37+
- name: Fail if archive was not created
38+
fail:
39+
msg: "Archive creation failed!"
40+
when: not archive_stat.stat.exists
41+
rescue:
42+
- name: Debug - Backup process failed, cleaning up
43+
ansible.builtin.debug:
44+
msg: "An error occurred during backup. Cleaning up temporary directory."
45+
always:
46+
- name: Cleanup temporary directory
47+
file:
48+
state: absent
49+
path: "{{ temp_backup_dir.path }}"
50+
delegate_to: localhost

0 commit comments

Comments
 (0)