- 论坛徽章:
- 3
|
本帖最后由 litigerking 于 2016-08-01 08:00 编辑
我配置定时执行任务备份mysql,在命令行执行能正常备份,但是放在crontab后,却没有备份,看日志crontab是正常执行了脚本的,也没有错误提示,
crontab加的- 15 8 * * * root /home/mysqlbackup/mysqlbackup.sh
复制代码 mysqlbackup.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[7088]: (root) CMD (/home/mysqlbackup/mysqlbackup.sh)
复制代码 |
|