Skip to content

Instantly share code, notes, and snippets.

@richardfeliciano
Last active February 3, 2020 19:57
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save richardfeliciano/33c91b5e6139fac3f733 to your computer and use it in GitHub Desktop.
Save richardfeliciano/33c91b5e6139fac3f733 to your computer and use it in GitHub Desktop.
Backup ALL MySql
#!/bin/bash
#your db settings
USER="db_root"
PASS="db_pass"
PATH="/path/to/backup/folder"
HOST="mysql_host"
DATE=`date +%d%m%y%H%M`
#get databases list
databases=`mysql --host=$HOST --user=$USER --password=$PASS -e "SHOW DATABASES;" | tr -d "| " | grep -v Database`
#loop trough results
for db in $databases; do
#exclude performance_schema and information_schema databases
if [ "$db" != "performance_schema" ] && [ "$db" != "information_schema" ] ; then
#dump database, and gzip
mysqldump -u $USER -p${PASS} $db | gzip > ${PATH}dbbackup_${db}_${DATE}.bak.gz
fi
done
#remove backups older than 7 days(+7) modify this value if you want.
find /path/to/backup/folder/db* -mtime +7 -exec rm {} \;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment