Added SQLDumps, a tool for dumping every required MySQL or MariaDB database to a SQL file for backup purposes.

This commit is contained in:
Ze'ev Schurmann 2023-09-27 09:52:20 +02:00
parent 034e910cb7
commit f1842b68b3
1 changed files with 61 additions and 0 deletions

61
sqldumps/sqldumps.sh Normal file
View File

@ -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