1.创建一个shell脚本文件
cd /usr
mkdir dbbackup
cd /usr/dbbackup
vim backup.sh
echo "-----------------------------" >> /usr/dbbackup/dbbackup.log
echo `date +%Y%m%d`"erms Database backup start" >> /usr/dbbackup/dbbackup.log
/usr/local/mysql/bin/mysqldump -uroot -ppwd erms > /usr/dbbackup/backup_`date +%Y%m%d`\
.sql
if [ $? -eq 0 ];then
if [ -f "/usr/dbbackup/backup_`date +%Y%m%d`.sql" ];then
echo `date +%Y%m%d`"erms Database backup success" >> /usr/dbbackup/dbbackup.log
else
echo `date +%Y%m%d`"erms Database backup fail!" >> /usr/dbbackup/dbbackup.log
fi
else
echo `date +%Y%m%d`"erms Database backup error" >> /usr/dbbackup/dbbackup.log
fi
echo "-----------------------------" >> /usr/dbbackup/dbbackup.log
find /usr/dbbackup/ -mtime +7 -name backup_\* -exec rm -rf {} \;
更改backup.sh文件权限 chmod 755 /usr/dbbackup/backup.sh
2.添加到定时任务
每天凌晨3点半备份一次
crontab -e
30 3 * * * /usr/dbbackup/backup.sh