Skip to content

Instantly share code, notes, and snippets.

@khasky
Last active Feb 21, 2021
Embed
What would you like to do?
Bash MySQL backup for all databases
#!/bin/bash
USER=root
PASSWORD=root
DATE_NOW="`date '+%Y-%m-%d_%H-%M-%S'`"
DIR=/var/local/backup
DATABASES=`mysql -u$USER -p$PASSWORD -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
echo "Databases found: $DATABASES"
for db in $DATABASES; do
if [[ "$db" != "information_schema" ]] && [[ "$db" != "performance_schema" ]] && [[ "$db" != "mysql" ]] && [[ "$db" != _* ]] ; then
echo "Exporting $db"
path=$DIR/"$db"_"$DATE_NOW".sql
mysqldump -u $USER -p$PASSWORD $db > $path --skip-opt --extended-insert --quick --compact --compress
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment