added mariadb dump to backup

This commit is contained in:
= 2022-11-04 09:10:35 +01:00
parent 38fbe9613f
commit 0a0f8558fe
5 changed files with 90 additions and 1 deletions

View File

@ -0,0 +1,12 @@
{%- set user = salt['pillar.get']('podman:user', 'root') %}
{%- set home = salt['user.info'](user).home %}
[Unit]
Description=Dump all mariadb databases
[Service]
Type=oneshot
User={{ user }}
ExecStart={{ home }}/bin/mysql-dump.sh
[Install]
WantedBy=default.target

View File

@ -0,0 +1,15 @@
#!/bin/bash
umask 0077
BACKUP_DIR={{ pillar.containers.mariadb.backup_dir }}
databases=$(podman exec -it mariadb mysql -B -u root -p{{ pillar.containers.mariadb.env.MYSQL_ROOT_PASSWORD }} -e "SHOW DATABASES;" | tr -d "| " | grep -v Database)
for db in ${databases[@]}; do
db=${db::-1}
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] && [[ "$db" != "sys" ]]; then
echo "Dumping database: $db"
podman exec -it mariadb mysqldump -u root -p{{ pillar.containers.mariadb.env.MYSQL_ROOT_PASSWORD }} --databases $db | gzip > ${BACKUP_DIR}/$(date +"%Y-%m-%d_%H-%M-%S")_$db-sql.gz
fi
done
# Delete the files older than 3 days
find $BACKUP_DIR/* -type d -name *-sql.gz -mtime +3 -exec rm {} \;

View File

@ -0,0 +1,9 @@
[Unit]
Description=Restic backup timer
[Timer]
OnCalendar={{ pillar.containers.mariadb.OnCalendar }}
Unit=restic-backup.service
[Install]
WantedBy=timers.target

View File

@ -1,4 +1,52 @@
{% from 'lib.sls' import container_deploy with context %} {% from 'lib.sls' import container_deploy with context %}
{% set user = salt['pillar.get']('podman:user', 'root') %}
{% set home = salt['user.info'](user).home %}
Create backup destination for mariadb backups:
file.directory:
- name: {{ pillar.containers.mariadb.backup_dir }}
- user: {{ user }}
- group: root
- mode: "0750"
Create backup script:
file.managed:
- name: {{ home }}/bin/mysql-dump.sh
- source: salt://podman/files/mysql-dump.sh.jinja
- template: jinja
- user: {{ user }}
- group: root
- mode: "0700"
Create mariadb dump service:
file.managed:
- name: /etc/systemd/system/mysql-dump.service
- source: salt://podman/files/mysql-dump.service.jinja
- template: jinja
- user: root
- group: root
- mode: "0644"
Create mariadb dump timer:
file.managed:
- name: /etc/systemd/system/mysql-dump.timer
- source: salt://podman/files/mysql-dump.timer.jinja
- template: jinja
- user: root
- group: root
- mode: "0644"
Reload systemd daemon for mariadb dump:
cmd.run:
- name: systemctl daemon-reload
- onchanges:
- file: Create mariadb dump service
- file: Create mariadb dump timer
Start mariadb dump timer:
service.running:
- name: mysql-dump.timer
- enable: True
{{ container_deploy('mariadb') }} {{ container_deploy('mariadb') }}

View File

@ -40,7 +40,12 @@ restic backup \
--exclude="/home/*/Nextcloud" \ --exclude="/home/*/Nextcloud" \
--exclude="/home/*/git" \ --exclude="/home/*/git" \
--verbose \ --verbose \
{% if pillar.podman is defined %}{{ salt['cmd.run']('podman info -f "{{.Store.VolumePath}}"', runas=pillar.podman.user ) }} \{% endif %} {%- if pillar.podman is defined %}
{{ salt['cmd.run']('podman info -f "{{.Store.VolumePath}}"', runas=pillar.podman.user ) }} \
{%- endif %}
{%- if pillar.containers.mariadb is defined %}
{{ pillar.containers.mariadb.backup_dir }} \
{%- endif %}
/etc \ /etc \
/root \ /root \
/home /home