62 lines
1.5 KiB
Bash
62 lines
1.5 KiB
Bash
|
#!/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
|