Skip to content

Commit f92e65d

Browse files
committed
Add small MariaDB docker role, for running on a single node
1 parent 986646e commit f92e65d

File tree

3 files changed

+148
-0
lines changed

3 files changed

+148
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
docker_mariadb_network_range: "172.21.21.0/24"
2+
mysql_backup_user: backup_user
3+
backup_node: True

roles/mariadbdocker/tasks/main.yml

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
- name: Create MariaDB volume
3+
community.docker.docker_volume:
4+
name: openconext_mariadb
5+
state: present
6+
7+
- name: Create MariaDB network
8+
community.docker.docker_network:
9+
name: openconext_mariadb
10+
state: present
11+
internal: false
12+
ipam_config:
13+
- subnet: "{{ docker_mariadb_network_range }}"
14+
15+
- name: Create the MariaDB container
16+
community.docker.docker_container:
17+
name: openconext_mariadb
18+
image: mariadb:10.6
19+
state: started
20+
pull: true
21+
restart_policy: "always"
22+
ports: "127.0.0.1:3306:3306"
23+
networks:
24+
- name: "openconext_mariadb"
25+
mounts:
26+
- type: volume
27+
source: openconext_mariadb
28+
target: /var/lib/mysql
29+
env:
30+
MARIADB_ROOT_PASSWORD: "{{ mariadb_root_password }}"
31+
32+
- name: Create database
33+
community.mysql.mysql_db:
34+
name: "{{ item }}"
35+
state: present
36+
login_user: root
37+
login_host: localhost
38+
login_password: "{{ mariadb_root_password }}"
39+
with_items:
40+
- "{{ databases.names }}"
41+
42+
- name: Create database user
43+
community.mysql.mysql_user:
44+
name: "{{ item[0].name }}"
45+
host: "{{ item[1] }}"
46+
password: "{{ item[0].password }}"
47+
priv: "{{ item[0].db_name }}.*:{{ item[0].privilege }}"
48+
state: present
49+
append_privs: true
50+
login_user: root
51+
login_host: localhost
52+
login_password: "{{ mariadb_root_password }}"
53+
# no_log: true
54+
with_nested:
55+
- "{{ databases.users }}"
56+
- "{{ database_clients }}"
57+
58+
- name: Add mariadb backup user
59+
community.mysql.mysql_user:
60+
name: "{{ mysql_backup_user }}"
61+
password: "{{ mysql_backup_password }}"
62+
login_user: root
63+
login_password: "{{ mariadb_root_password }}"
64+
login_host: localhost
65+
priv: "*.*:SELECT,RELOAD,PROCESS,LOCK TABLES,BINLOG MONITOR,CONNECTION ADMIN,SHOW VIEW"
66+
state: present
67+
# no_log: true
68+
69+
- name: Create the backup directory
70+
ansible.builtin.file:
71+
path: /home/backup
72+
state: directory
73+
owner: root
74+
group: root
75+
mode: "0700"
76+
when:
77+
- backup_node | bool
78+
79+
- name: Put mariadb_backup script
80+
ansible.builtin.template:
81+
src: "mariadb_backup.sh.j2"
82+
dest: "/usr/local/sbin/mariadb_backup.sh"
83+
mode: "0700"
84+
owner: root
85+
when:
86+
- backup_node | bool
87+
88+
- name: Create cron symlink for backup script
89+
file:
90+
src: /usr/local/sbin/mariadb_backup.sh
91+
dest: /etc/cron.daily/db_backup
92+
state: link
93+
mode: 0700
94+
owner: root
95+
when:
96+
- backup_node | bool
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#!/bin/bash
2+
3+
umask 0077
4+
5+
declare -x PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin
6+
7+
MYSQL_USER="{{ mysql_backup_user }}"
8+
MYSQL_PASS="{{ mysql_backup_password }}"
9+
FOLDER="/home/backup"
10+
11+
DAY=$(/bin/date +'%a')
12+
13+
echo "-- Remove old backups --"
14+
find /home/backup/ -type f -ctime +2 -delete
15+
16+
echo "-- START new backups --"
17+
18+
echo "SET autocommit=0;SET unique_checks=0;SET foreign_key_checks=0;" > tmp_sqlhead.sql
19+
echo "SET autocommit=1;SET unique_checks=1;SET foreign_key_checks=1;" > tmp_sqlend.sql
20+
21+
if [ -z "$1" ]
22+
then
23+
echo "-- Dumping all DB ..."
24+
for I in $(docker exec openconext_mariadb mariadb -u $MYSQL_USER --password=$MYSQL_PASS -e 'show databases' -s --skip-column-names);
25+
do
26+
if [ "$I" = information_schema ] || [ "$I" = mysql ] || [ "$I" = sys ] || [ "$I" = performance_schema ] # exclude this DB
27+
then
28+
echo "-- Skip $I ..."
29+
continue
30+
fi
31+
echo "-- Dumping $I ..."
32+
# Pipe compress and concat the head/end with the stoutput of mysqlump ( '-' cat argument)
33+
docker exec openconext_mariadb mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I | cat tmp_sqlhead.sql - tmp_sqlend.sql | gzip -fc > "$FOLDER/$DAY-$I.sql.gz"
34+
done
35+
36+
else
37+
I=$1;
38+
echo "-- Dumping $I ..."
39+
# Pipe compress and concat the head/end with the stoutput of mysqlump ( '-' cat argument)
40+
docker exec openconext_mariadb mysqldump -u $MYSQL_USER --password=$MYSQL_PASS $I | cat tmp_sqlhead.sql - tmp_sqlend.sql | gzip -fc > "$FOLDER/$DAY-$I.sql.gz"
41+
fi
42+
43+
# remove tmp files
44+
rm tmp_sqlhead.sql
45+
rm tmp_sqlend.sql
46+
47+
echo "-- FINISH —"
48+
49+
umask 0022

0 commit comments

Comments
 (0)