admin 2 months ago
commit
f4551d35e5
5 changed files with 56 additions and 0 deletions
  1. 2 0
      .env.template
  2. 2 0
      .gitignore
  3. 13 0
      README.md
  4. 22 0
      data/dump.sh
  5. 17 0
      docker-compose.yml

+ 2 - 0
.env.template

@@ -0,0 +1,2 @@
+DB_HOST=PutTheNameOfYourMariaDBcontainerHere
+MYSQL_ROOT_PASSWORD=PutYourDBrootPasswordHere

+ 2 - 0
.gitignore

@@ -0,0 +1,2 @@
+.env
+data/*.sql

+ 13 - 0
README.md

@@ -0,0 +1,13 @@
+## sqldump
+
+Dumps all DBs to ./data and then sleeps for the next 24h.
+
+### Setup
+1. Set variables DB name and DB root password in `.env`
+```
+echo -e \
+'DB_HOST=db'\
+'\n'\
+'MYSQL_ROOT_PASSWORD=PutYourPasswordHere'\
+> .env
+```

+ 22 - 0
data/dump.sh

@@ -0,0 +1,22 @@
+#!/bin/bash -xe
+# Daily dump all DBs in individual files
+
+while true; do
+  echo "Start backup at "`date`
+  # Get a list of all DBs to iterate in the next steps
+  databases=`mysql --host=$DB_HOST --user=root --password=$MYSQL_ROOT_PASSWORD --batch --disable-column-names --execute "show databases;"`
+
+  for db in $databases;do
+    # Skip internal databases which cannot be dumped
+    if [ $db = "information_schema" ]; then echo "--> skip $db"; continue; fi
+    if [ $db = "performance_schema" ]; then echo "--> skip $db"; continue; fi
+    echo "--> dump $db"
+    mysqldump --host=$DB_HOST --user=root --password=$MYSQL_ROOT_PASSWORD --databases $db > /mnt/backup/$db.sql
+  done
+
+  # Dump users, passwords and grants
+  mysqldump --host=$DB_HOST --user=root --password=$MYSQL_ROOT_PASSWORD --system=user --insert-ignore > /mnt/backup/grants.sql
+
+  echo "Sleeping for 1d ..................tzzzzz"
+  sleep 86400
+done

+ 17 - 0
docker-compose.yml

@@ -0,0 +1,17 @@
+version: '3.3'
+
+services:
+   sqldump:
+     container_name: db_sqldump
+     image: mariadb:10.6
+     volumes:
+             - ./data/:/mnt/backup/
+     restart: always
+     networks:
+       - db_default
+     env_file:
+       - .env
+     entrypoint: mnt/backup/dump.sh
+networks:
+     db_default:
+       external: true