实现自动备份MySQL数据库

时间:2020-12-10 02:42:15
#!/bin/bash

base="/zsjdata/mysql/data"
date=$(date +%Y%m%d)
hour=$(date +%H)
time=$(date +%H%M%S) charset="UTF8"
username="root"
password="sinykk123"
database="online_diarydbnew"
filename="$database.$date$time.sql" cd $base if [ ! -d $date ]
then
mkdir ./$date
fi dir="./$date/$date$hour"
if [ ! -d $dir ]
then
mkdir $dir
fi path="$base/$dir" /usr/local/mysql/bin/mysqldump -u$username -p$password --opt --default-character-set=$charset -q $database > $path/$filename cd $path
tar zcf ./$filename.tar.gz ./$filename
rm -r ./$filename

经过亲自测试应用,目前没有发现问题,

/usr/local/mysql/bin/mysqldump 这里请填写您的服务器 mysql 安装的实际路径

MySQL的备份参数可以去这里看看

http://blog.csdn.net/zsjangel/article/details/18902285