Seems that you are wanting a bash script to run backup on dynamic databases that are created in MySQL. You can add the mysql root user account information in my.cnf in the root directory or within the bash script under the # [ Define Variables ].
you will need to chmod the bash script with
$sudo chmod +x backupmysql.sh
This will allow you to run the script with the following command.
$sudo ./backupmysql.sh
You can name the script whatever you like. In this example, I named it backupmysql.sh.
Here is the bash script:
#!/bin/bash
# [ Define Variables ]
HOST=`hostname -s`
syslogtag=MySQL-Backup
DEST=/var/dba/backup/
DBS="$(mysql -u root -Bse 'show databases' | egrep -v '^Database$|hold$' | grep -v 'performance_schema\|information_schema')"
DATE=$(date +'%F')
#[ Individually dump all databases with date stamps ]
for db in ${DBS[@]};
do
GZ_FILENAME=$HOST-$db-$DATE.sql.gz
mysqldump -u root --quote-names --opt --single-transaction --quick $db > $DEST$HOST-$db-$DATE.sql
ERR=$?
if [ $ERR != 0 ]; then
NOTIFY_MESSAGE="Error: $ERR, while backing up database: $db"
logger -i -t ${syslogtag} "MySQL Database Backup FAILED; Database: $db"
else
NOTIFY_MESSAGE="Successfully backed up database: $db "
logger -i -t ${syslogtag} "MySQL Database Backup Successful; Database: $db"
fi
echo $NOTIFY_MESSAGE
done
If you have large files for backup, you can replace the statement in the bash script for the mysqldump to compress the file using gzip.
mysqldump -u root --quote-names --opt --single-transaction --quick $db | gzip -cf > $DEST$HOST-$db-$DATE.sql.gz
you can use gunzip to uncompress the file.