用crontab自动备份MySQL的时候导出总是为空,原因是corn
的环境变量中没有mysqldump
,所以用crontab
执行mysqldump
的时候要加上绝对路径,如:/usr/bin/mysqldump -u root -p123456 --default-character-set=utf8 --databases solo > root/solo.sql
这样就可以了!
如果不知道绝对路径可以用which mysqldump
或者whereis mysqldump
查询
例如:
#!/bin/sh # mongodump备份文件执行路径 mysqldump=/usr/bin/mysqldump # 备份存放路径 backup_dir=/root/database_backup # 获取当前系统时间,作为文件名的一部分 date=`date -d "today" +"%Y-%m-%dT%H-%M-%S"` # mins=10080 代表删除7天前的备份, 即只保留最近7天的备份 mins=10080 # 最终保存的数据库备份文件名 tar_name="mysql_bak_${date}.tar.gz" cd ${backup_dir} mkdir -p ${backup_dir}/${date} # 备份项目数据库 ${mysqldump} -h HOST -u USERNAME -pPASSWORD --default-character-set=utf8 --databases DBNAME > ${backup_dir}/${date}/DBNAME.sql # 压缩为.tar.gz格式 tar -zcvf ${tar_name} ${date} # 删除7天前的备份文件 find ${backup_dir}/ -mmin +${mins} -exec rm -rf {} \;
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:MySQL使用mysqldump备份数据库脚本手工执行成功 crontab定时却失败的解决方法