首页 > 代码库 > Crontab定时备份数据库

Crontab定时备份数据库

1.创建一个shell脚本文件

cd usr

mkdir dabackup

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/msyqldump -uroot -ppwd erms >>/usr/dbbackup/backup_`date +%Y%m%d`.sql

if [ $? -eq 0 ];then
        if [ -f "/usr/dabackup/backup_`date +%Y%m%d`.sql"  ];then
     tar -czf /usr/dabackup/backup_`date +%Y%m%d`
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/dabackup/backup.sh

2.添加到定时任务

每天凌晨3点半备份一次

crontab -e

30 3 * * * /usr/dbbackup/backup.sh

 

Crontab定时备份数据库