Skip to content

Instantly share code, notes, and snippets.

@khasky
Last active February 21, 2021 19:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khasky/28cbc8b1a00ed98b84348d6e5c37b078 to your computer and use it in GitHub Desktop.
Save khasky/28cbc8b1a00ed98b84348d6e5c37b078 to your computer and use it in GitHub Desktop.
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