From 0a0f8558fe3d4228adbf28672756e8e320df21c6 Mon Sep 17 00:00:00 2001 From: = Date: Fri, 4 Nov 2022 09:10:35 +0100 Subject: [PATCH] added mariadb dump to backup --- podman/files/mysql-dump.service.jinja | 12 +++++++ podman/files/mysql-dump.sh.jinja | 15 +++++++++ podman/files/mysql-dump.timer.jinja | 9 +++++ podman/mariadb.sls | 48 +++++++++++++++++++++++++++ restic/files/backup.sh.jinja | 7 +++- 5 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 podman/files/mysql-dump.service.jinja create mode 100644 podman/files/mysql-dump.sh.jinja create mode 100644 podman/files/mysql-dump.timer.jinja diff --git a/podman/files/mysql-dump.service.jinja b/podman/files/mysql-dump.service.jinja new file mode 100644 index 0000000..ea2e7d1 --- /dev/null +++ b/podman/files/mysql-dump.service.jinja @@ -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 diff --git a/podman/files/mysql-dump.sh.jinja b/podman/files/mysql-dump.sh.jinja new file mode 100644 index 0000000..131f970 --- /dev/null +++ b/podman/files/mysql-dump.sh.jinja @@ -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 {} \; + diff --git a/podman/files/mysql-dump.timer.jinja b/podman/files/mysql-dump.timer.jinja new file mode 100644 index 0000000..2073ac3 --- /dev/null +++ b/podman/files/mysql-dump.timer.jinja @@ -0,0 +1,9 @@ +[Unit] +Description=Restic backup timer + +[Timer] +OnCalendar={{ pillar.containers.mariadb.OnCalendar }} +Unit=restic-backup.service + +[Install] +WantedBy=timers.target diff --git a/podman/mariadb.sls b/podman/mariadb.sls index b100fac..ebc735e 100644 --- a/podman/mariadb.sls +++ b/podman/mariadb.sls @@ -1,4 +1,52 @@ {% 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') }} diff --git a/restic/files/backup.sh.jinja b/restic/files/backup.sh.jinja index b89d48d..07c48e0 100644 --- a/restic/files/backup.sh.jinja +++ b/restic/files/backup.sh.jinja @@ -40,7 +40,12 @@ restic backup \ --exclude="/home/*/Nextcloud" \ --exclude="/home/*/git" \ --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 \ /root \ /home