2023-09-27 09:52:20 +02:00
|
|
|
#!/bin/bash
|
|
|
|
|
2023-09-27 12:37:52 +02:00
|
|
|
## SQLDump Script
|
2023-09-27 09:52:20 +02:00
|
|
|
## Author: Ze'ev Schurmann
|
|
|
|
## Reddit: u/thisiszeev
|
2023-09-27 12:37:52 +02:00
|
|
|
## License: GPL-3.0 or later
|
|
|
|
|
2023-09-27 09:52:20 +02:00
|
|
|
|
|
|
|
## 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
|
|
|
|
|
2023-09-27 12:37:52 +02:00
|
|
|
## Please consider making me small donation. Even though my scripts are open source and free to use, I still need to eat. And the
|
|
|
|
## occasional bottle of wine also goes down well.
|
|
|
|
## $5 buys me a cup of coffee
|
|
|
|
## $10 buys me a nice burger
|
|
|
|
## $20 buys me a bottle of wine
|
|
|
|
## Anything above that will be awesome as well.
|
|
|
|
## You can send me a donation via Paypal https://www.paypal.com/paypalme/thisiszeev
|
|
|
|
## Drop me a message on Reddit if you do make a donation.
|
|
|
|
## Support is only offered freely to those who donate $20 or more.
|
|
|
|
## Your donation contributes to further development.
|
|
|
|
## If you need a custom script, contact me on Reddit for pricing.
|
2023-09-27 09:52:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## 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"
|
|
|
|
|
|
|
|
|
2023-10-16 17:59:49 +02:00
|
|
|
## MySQL Credentials
|
|
|
|
|
|
|
|
## If the username is left blank (null) then no MySQL login will be attempted.
|
|
|
|
## If the password is left blank (null) then no Password will be used in the login attempt.
|
|
|
|
|
|
|
|
sqlusername="root"
|
|
|
|
sqlpassword="djhfasdghfkjlahsdfjkh"
|
|
|
|
|
|
|
|
|
|
|
|
## Determines login method.
|
|
|
|
|
|
|
|
if [[ -z $sqlusername ]]
|
|
|
|
then
|
|
|
|
sqlup=""
|
|
|
|
else
|
|
|
|
if [[ -z $sqlpassword ]]
|
|
|
|
then
|
|
|
|
sqlup="-u $sqlusername"
|
|
|
|
else
|
|
|
|
sqlup="-u $sqlusername -p$sqlpassword"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
|
|
|
|
|
2023-09-27 09:52:20 +02:00
|
|
|
## Removes previous SQL dumps and list files.
|
|
|
|
|
|
|
|
rm -f *.sql
|
|
|
|
rm -f *.list
|
|
|
|
|
|
|
|
|
|
|
|
## Creates a list file of all the databases.
|
|
|
|
|
2023-10-16 17:59:49 +02:00
|
|
|
mysql $sqlup -e "show databases" | grep -Ev 'Database|information_schema' > db.list
|
2023-09-27 09:52:20 +02:00
|
|
|
|
|
|
|
|
|
|
|
## Add a list of the unwanted databases to the list file.
|
|
|
|
|
|
|
|
for ((n=0; n<${#ignorelist[@]}; n++))
|
|
|
|
do
|
|
|
|
echo ${ignorelist[$n]} >> db.list
|
2023-10-16 17:59:49 +02:00
|
|
|
echo ${ignorelist[$n]} >> db.list
|
2023-09-27 09:52:20 +02:00
|
|
|
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"
|
2023-10-16 17:59:49 +02:00
|
|
|
mysqldump $sqlup $dbname > "$dumppath/$dbname.sql"
|
2023-09-27 09:52:20 +02:00
|
|
|
done
|
|
|
|
|
|
|
|
rm db.list
|