Skip to content

Instantly share code, notes, and snippets.

@zhouyl
Last active August 29, 2015 13:58
Show Gist options
  • Save zhouyl/10104637 to your computer and use it in GitHub Desktop.
Save zhouyl/10104637 to your computer and use it in GitHub Desktop.
mysql 自动备份
#!/usr/bin/env bash
# 数据库自动备份
root_dir=$(cd "$(dirname "$0")"; cd ..; pwd)
timestamp=$(date +"%Y%m%d_%H%M")
backup_dir=$root_dir/data/dbbackup
# 需要备份的库
databases=(db1, db2)
# mysql 配置
mysql=/usr/local/mysql/bin/mysql
mysql_host=127.0.0.1
mysql_user=root
mysql_port=3310
mysql_password=mypasswd
mysqldump=/usr/bin/mysqldump
for db in ${databases[@]}; do
file=$backup_dir/$db.$timestamp.sql
echo -n "$db > $file ..."
$mysqldump \
--force --opt --skip-lock-tables --skip-comments \
-u$mysql_user -p$mysql_password -h $mysql_host -P$mysql_port \
--databases $db > $file
echo -e " \033[32;49;2mdone!\033[39;49;0m"
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment