技术解析
问题: 目录下直接执行脚本可以正常备份,使用 crontab 失败,没有日志打出
脚本位置:/home/script/backup_db.sh
网上搜的试了都不行 求大佬们帮忙看看问题在哪?
#!/bin/bash
# 定时备份数据库
BACKUP_PATH=/home/backup/db
DATETIME=`date +"%Y_%m_%d_%H%M%S"`
# DATETIME_4_PATH=`date +"%Y_%m_%d_%H%M%S"`
#mysql 信息
MYSQL_HOST=127.0.0.1
MYSQL_USER=root
MYSQL_PASSWD=123456
DATEBASE_NAME=test2
# 判断目录是否存在
[ ! -d "$BACKUP_PATH" ] && mkdir -p "$BACKUP_PATH"
# 备份数据库
# mysqldump -u"$MYSQL_USER" -p"$MYSQL_PASSWD" --host=$MYSQL_HOST $DATEBASE_NAME | gzip > $BACKUP_PATH/$DATETIME.sql.gz
/usr/bin/mysqldump -u"$MYSQL_USER" -p"$MYSQL_PASSWD" --host=$MYSQL_HOST $DATEBASE_NAME > $BACKUP_PATH/$DATETIME.sql
[ $? -eq 0 ] && echo "备份成功" || echo "备份失败";
#在$backup 目录下按照时间找 2 天前的名称为*.sql 的文件,-exec 表示执行找到的文件
find $BACKUP_PATH -mtime +2 -name "*.sql" -exec rm rf {} \;
echo "备份完成";
[[email protected]_0_6_centos script]# crontab -l
*/1 * * * * flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &'
0 0 * * * /usr/local/qcloud/YunJing/YDCrontab.sh > /dev/null 2>&1 &
0 */1 * * * ? /home/script/backup_db.sh
没有备份脚本的信息,只有腾讯云自带的一个脚本
[[email protected]_0_6_centos script]# tailf /var/log/cron
Jul 9 16:01:01 VM_0_6_centos run-parts(/etc/cron.hourly)[14387]: finished 0anacron
Jul 9 16:02:01 VM_0_6_centos CROND[14557]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:03:01 VM_0_6_centos CROND[14736]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:04:01 VM_0_6_centos CROND[14923]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:05:01 VM_0_6_centos CROND[15098]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:06:01 VM_0_6_centos CROND[15287]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:07:01 VM_0_6_centos CROND[15465]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:08:01 VM_0_6_centos CROND[15686]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:09:01 VM_0_6_centos CROND[15865]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')
Jul 9 16:10:02 VM_0_6_centos CROND[16066]: (root) CMD (flock -xn /tmp/stargate.lock -c '/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &')