1.創(chuàng)建并編輯文件 /usr/sbin/bakmysql古劲,命令:
vi /usr/sbin/bakmysql.sh
內(nèi)容如下:
db_user="root"
db_passwd="root"
db_name="db_test"
# the directory for story your backup file.you shall change this dir
backup_dir="/usr/software/backup/mysqlbackup"
mysqldump -u$db_user? -p$db_passwd $db_name? > "$backup_dir/$db_name"_$(date +%Y%m%d%_H%M%S).sql
注意 -p 和?$db_passwd之間沒有空格,不然$db_passwd會(huì)被當(dāng)做參數(shù)[數(shù)據(jù)庫名]來解析
2.修改文件bakmysql屬性尽棕,使其可執(zhí)行;
chmod +x /usr/sbin/bakmysql.sh
3.創(chuàng)建定時(shí)任務(wù)
crontab –e
#進(jìn)入編輯界面,內(nèi)容如下
00 23 ?* ?* ?* ?./usr/sbin/./bakmysql.sh
對應(yīng) 分 時(shí) 日 月 周 command
#上一行命令的 " . " 不要也可
# 00 23?是每天23:00執(zhí)行 bakmysql文件,備份數(shù)據(jù)庫在路徑/usr/software/backup/mysqlbackup下