aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rwxr-xr-xscripts/backup-mariadb30
-rw-r--r--system/backup-mariadb.service12
-rw-r--r--system/backup-mariadb.timer10
3 files changed, 52 insertions, 0 deletions
diff --git a/scripts/backup-mariadb b/scripts/backup-mariadb
new file mode 100755
index 0000000..ae5d0ec
--- /dev/null
+++ b/scripts/backup-mariadb
@@ -0,0 +1,30 @@
+#!/usr/bin/env bash
+
+# Set required settings
+timestamp="$(date +"%Y%m%d-%H%M%S")-"
+
+# If the backup location doesn't exist yet, create it
+if [ ! -d $mariadb_folder_destination ]; then
+ mkdir -p $mariadb_folder_destination
+fi
+
+# Get the names of all databases
+databases=`mysql --user=$mariadb_user -p$mariadb_password -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|tmp)"`
+
+cd $tmp
+# Iterate over all database names and create a backup for each, encrypting them afterwards
+for db in $databases; do
+ echo "$db -> $mariadb_folder_destination$timestamp$db$sql_suffix$tar_suffix$gpg_suffix"
+ # dump .sql to /tmp
+ mysqldump --force --opt --user=$mariadb_user -p$mariadb_password --databases $db > $tmp$timestamp$db$sql_suffix
+ # tar in /tmp
+ tar cfJ $timestamp$db$sql_suffix$tar_suffix $timestamp$db$sql_suffix
+ # gpg to mariadb_folder_destination
+ gpg -e \
+ -r "$gpg_public_key" \
+ -o $mariadb_folder_destination$timestamp$db$sql_suffix$tar_suffix$gpg_suffix \
+ $timestamp$db$sql_suffix$tar_suffix
+ # delete tar and sql in /tmp
+ rm -f $timestamp$db$sql_suffix*
+done
+
diff --git a/system/backup-mariadb.service b/system/backup-mariadb.service
new file mode 100644
index 0000000..8aa6622
--- /dev/null
+++ b/system/backup-mariadb.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Backup MariaDB databases
+ConditionFileIsExecutable=/usr/lib/systemd/scripts/backup-mariadb
+ConditionFileIsExecutable=/usr/bin/mysql
+
+[Service]
+EnvironmentFile=/etc/backup-settings
+Nice=19
+IOSchedulingClass=2
+IOSchedulingPriority=7
+ExecStart=/usr/lib/systemd/scripts/backup-mariadb
+
diff --git a/system/backup-mariadb.timer b/system/backup-mariadb.timer
new file mode 100644
index 0000000..1f9fca6
--- /dev/null
+++ b/system/backup-mariadb.timer
@@ -0,0 +1,10 @@
+[Unit]
+Description=Backup MariaDB databases every Monday at 3:00
+
+[Timer]
+# Every Monday at 3:00
+OnCalendar=Mon *-*-* 03:00
+Persistent=true
+
+[Install]
+WantedBy=timers.target