From f1842b68b3c1fdf3b72325159da6909a0655d15b Mon Sep 17 00:00:00 2001 From: Ze'ev Schurmann Date: Wed, 27 Sep 2023 09:52:20 +0200 Subject: [PATCH] Added SQLDumps, a tool for dumping every required MySQL or MariaDB database to a SQL file for backup purposes. --- sqldumps/sqldumps.sh | 61 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 61 insertions(+) create mode 100644 sqldumps/sqldumps.sh diff --git a/sqldumps/sqldumps.sh b/sqldumps/sqldumps.sh new file mode 100644 index 0000000..ce5a5da --- /dev/null +++ b/sqldumps/sqldumps.sh @@ -0,0 +1,61 @@ +#!/bin/bash + +## SQLDump +## Author: Ze'ev Schurmann +## Reddit: u/thisiszeev + +## This script creates a list of all databases in MySQL or MariaDB and then adds to then +## list all the unwanted databases. It then creates a list of unique names which are the +## databases to be backed up. (If you have a root password for MySQL or MariaDB then you +## may need too include it in the code.) + +## Place this script in the folder /var/sqldumps + +## It's a good idea to run this in as a cron tab. Then you can time a remote backup from +## another server/machine. + +## Crontab example: (excluding the ##) + +## 0 22 * * * root cd /var/sqldumps && bash sqldumps.sh + + + + +## Put the names of the databases you do not want to backup in the following array. + +ignorelist=("mysql" "performance_schema" "dbispconfig" "phpmyadmin" "roundcube") + + +## Put the location for the SQL Dumps + +dumppath="/var/sqldumps" + + +## Removes previous SQL dumps and list files. + +rm -f *.sql +rm -f *.list + + +## Creates a list file of all the databases. + +mysql -e "show databases" | grep -Ev 'Database|information_schema' > db.list + + +## Add a list of the unwanted databases to the list file. + +for ((n=0; n<${#ignorelist[@]}; n++)) +do + echo ${ignorelist[$n]} >> db.list +done + + +## Generate a list of the databases to be backed up and pass it line by line +## into a while loop, exporting each database to dbname.sql + +sort db.list | uniq -u | while read -r dbname; do + echo "Dumping $dbname to $dumppath/$dbname.sql" + mysqldump $dbname > "$dumppath/$dbname.sql" +done + +rm db.list