Skip to content

Instantly share code, notes, and snippets.

@guweigang
Forked from zhouyl/mysql-backup.sh
Created July 21, 2014 10:55
Show Gist options
  • Save guweigang/5557076256d5834f8edf to your computer and use it in GitHub Desktop.
Save guweigang/5557076256d5834f8edf to your computer and use it in GitHub Desktop.
#!/bin/bash
# mysql 自动备份
TIMESTAMP=$(date +"%Y%m%d")
BACKUP_DIR="/usr/local/mysql/backup"
MYSQL_USER="root"
MYSQL=/usr/local/mysql/bin/mysql
MYSQL_PASSWORD="root"
MYSQLDUMP=/usr/local/mysql/bin/mysqldump
mkdir -p "$BACKUP_DIR/$TIMESTAMP"
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql|test)"`
for db in $databases; do
FILE=$BACKUP_DIR/$TIMESTAMP/$db.sql
echo -n "$db > $FILE ..."
if [ ! -f $FILE ]; then
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD --databases $db > $FILE
echo -e " \033[32;49;2mdone!\033[39;49;0m"
else
echo -e " \033[31;49;2malready exists!\033[39;49;0m"
fi
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment