1,注意:source /etc/profile 不能少,否则crontab自动执行时无法保留数据。
2,crontab -e 输入如下命令(每天3点执行):
1 3 * * * /data/sh/bakMysqlShell.sh >/dev/null 2>&1
脚本内容如下:
#!/bin/sh
##backup MysqlDataBase
source /etc/profile
user=root #DataBase userName
password=pwd@123 #DataBase password
targetDIR=/disk3/data/mysqlBak/ #the dir which you want the sql to generate in; "/" is needed!
targetDataBase=uei #the dataBase name chich you want to backup
if [ ! -d $targetDIR ]; then
echo "make new targetDIR"
mkdir -p $targetDIR
fi
folderName=`date +%Y%m%d%H%M%S`
echo $targetDataBase"数据库备份开始"
mysqldump -u$user -p$password --opt -q --default-character-set=UTF8 $targetDataBase>$targetDIR$targetDataBase$folderName.sql
echo $targetDataBase"数据库备份结束"
tar zcPvf $targetDIR$targetDataBase$folderName.sql.tar.gz $targetDIR$targetDataBase$folderName.sql
rm -fr $targetDIR$targetDataBase$folderName.sql