定時任務執行mysql資料庫備份,crontab可執行。
阿新 • • 發佈:2019-01-10
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
[email protected] #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