mysql自动备份
2019-12-18 本文已影响0人
胸口砕大石
编写脚本
#!/bin/bash
DATE=`date +%Y%m%d%H%M` #every minute
DATABASE=detonatorhelper #database name
DB_USERNAME=root #database username
DB_PASSWORD="******" #database password
BACKUP_PATH=/opt/mysqlbackup #backup path
#backup command
/usr/bin/mysqldump -u$DB_USERNAME -p$DB_PASSWORD -h 127.0.0.1 -R --opt $DATABASE | gzip > ${BACKUP_PATH}\/${DATABASE}_${DATE}.sql.gz
#just backup the latest 5 days
find ${BACKUP_PATH} -mtime +5 -name "${DATABASE}_*.sql.gz" -exec rm -f {} \;
加入以下内容 vi /etc/crontab
0 1 * * * sh /opt/mysqlbackup/backup.sh
重新读取配置
crontab /etc/crontab
重载服务
systemctl reload crond.service
- crontab -l查看任务
- crontab -e便捷定时编辑任务
启动、停止、重启服务
- systemctl start crond.service
- systemctl stop crond.service
- systemctl restart crond.service
设置成开机自启
在/etc/rc.d/rc.local 脚本中加入 systemctl start crond.service即可