litigerking 发表于 2016-07-28 08:43

crontab执行mysql备份,但是没有生成备份文件

本帖最后由 litigerking 于 2016-08-01 08:00 编辑

我配置定时执行任务备份mysql,在命令行执行能正常备份,但是放在crontab后,却没有备份,看日志crontab是正常执行了脚本的,也没有错误提示,

crontab加的15      8       *       *       *       root    /home/mysqlbackup/mysqlbackup.shmysqlbackup.sh内容
#!/bin/sh
db_user="tiger"
db_passwd="tiger"
db_host="localhost"

# directory for backup files.
backup_dir="/home/mysqlbackup"

# delete backup files created expdays ago
expdays=100

# date format for backup files (yyyy-mm-dd)
time="$(date +"%Y-%m-%d")"

# a comma delimited list of databases to backup, backup all databases if blank
dblist="tiger"

# get full path and name for the following programs
MYSQL="$(which mysql)"
if [ -z $MYSQL ]; then
echo "mysql not found ..."
exit 0
fi
MYSQLDUMP="$(which mysqldump)"
if [ -z $MYSQLDUMP ]; then
echo "mysqldump not found ..."
exit 0
fi
MKDIR="$(which mkdir)"
RM="$(which rm)"
GZIP="$(which gzip)"
###########END-USER-DEFINE###########

# create backup root directory if not found
test ! -d "$backup_dir/" && $MKDIR "$backup_dir/"

# check the directory for store backup is writeable
test ! -w $backup_dir && echo "Error: $backup_dir is un-writeable." && exit 0

# the directory for story the newest backup
test ! -d "$backup_dir/$time/" && $MKDIR "$backup_dir/$time/"

# get all databases
all_db="$($MYSQL -u $db_user -h $db_host -p$db_passwd -Bse 'show databases')"

for db in $all_db
do
goforit=$db
if [ ! -z "$dblist" ]; then
goforit=`echo ",$dblist," |grep ",$db,"`
fi
if [ ! -z "$goforit" ]; then
$MYSQLDUMP -u $db_user -h $db_host -p$db_passwd $db | $GZIP -9 > "$backup_dir/$time/$time.$db.gz"
fi
done

# delete the oldest backup
find $backup_dir -type d -mtime +$expdays -maxdepth 1 | xargs $RM -rf
log记录Jul 28 08:15:00 server2 /usr/sbin/cron: (root) CMD (/home/mysqlbackup/mysqlbackup.sh)

litigerking 发表于 2016-07-28 08:47

系统是FreeBSD 10 64位!

lsstarboy 发表于 2016-07-29 08:45

文件开始为什么没有#!/bin/sh呢?

litigerking 发表于 2016-07-31 18:51

没复制上,文件里有!
回复 3# lsstarboy


   

lsstarboy 发表于 2016-07-31 21:38

手工执行报错吗?

litigerking 发表于 2016-08-01 07:55

手工执行正常!
回复 5# lsstarboy


   

litigerking 发表于 2016-08-01 07:57

这个脚本在8.3时用过,当时都很正常,现在就是不行!
回复 5# lsstarboy


   

lsstarboy 发表于 2016-08-01 08:41

建议:
1、把which那几个换成绝对路径试试。
2、多加几个>>重定向,当调试信息写入到一个文件中。

litigerking 发表于 2016-08-01 10:18

不管用,全部写成绝对路径,帐号密码全部写到命令行里,最后生成一个0字节的空文件,但是在命令行中执行都正常,就是放在crontab不行!回复 8# lsstarboy


   

cl101001000 发表于 2016-08-01 14:04

回复 1# litigerking

不要加入用户试试。
‘15      8       *       *       *    /home/mysqlbackup/mysqlbackup.sh’


   
页: [1] 2 3 4
查看完整版本: crontab执行mysql备份,但是没有生成备份文件