100 lines
2.7 KiB
Bash
Executable File
100 lines
2.7 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
## SQLDump Script
|
|
## Author: Ze'ev Schurmann
|
|
## Reddit: u/thisiszeev
|
|
## License: GPL-3.0 or later
|
|
|
|
|
|
## 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
|
|
|
|
## 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.
|
|
|
|
|
|
|
|
## 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"
|
|
|
|
|
|
## 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
|
|
|
|
|
|
## Removes previous SQL dumps and list files.
|
|
|
|
rm -f *.sql
|
|
rm -f *.list
|
|
|
|
|
|
## Creates a list file of all the databases.
|
|
|
|
mysql $sqlup -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
|
|
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 $sqlup $dbname > "$dumppath/$dbname.sql"
|
|
done
|
|
|
|
rm db.list
|