centos下mysql自动备份

创建脚本

#!/bin/bash
db_user="root"
db_passwd="root"
db_name="data_center"
#备份目录
backup_dir="/home/backup/mysql"
#时间格式
time=$(date +"%Y-%m-%d")
#mysql 备份的命令,注意有空格和没有空格
mysqldump -u$db_user -p$db_passwd $db_name  > "$backup_dir/$db_name"-"$time.sql"
echo "your database backup successfully completed"
#这里将7天之前的备份文件删掉
SevenDays=$(date -d -7day  +"%Y-%m-%d")
if [ -f /home/backup/mysql/$db_name-$SevenDays.sql ]
then
rm -rf /home/backup/mysql/$db_name-$SevenDays.sql
echo "you have delete 7days ago bak sql file "
else
echo "7days ago bak sql file not exist "
echo "bash complete"
fi

创建定时任务

crontab -e

0 2 * * * cd /root/ && ./data_center.sh

 

经过测试发现备份出来的文件都是0kb

经过排查是  mysqldump 路径文件

备份脚本改为

#!/bin/bash
db_user="root"
db_passwd="root"
db_name="data_center"
#备份目录
backup_dir="/home/backup/mysql"
#时间格式
time=$(date +"%Y-%m-%d")
#mysql 备份的命令,注意有空格和没有空格
/usr/local/mysql/bin/mysqldump -u$db_user -p$db_passwd $db_name  > "$backup_dir/$db_name"-"$time.sql"
echo "your database backup successfully completed"
#这里将7天之前的备份文件删掉
SevenDays=$(date -d -7day  +"%Y-%m-%d")
if [ -f /home/backup/mysql/$db_name-$SevenDays.sql ]
then
rm -rf /home/backup/mysql/$db_name-$SevenDays.sql
echo "you have delete 7days ago bak sql file "
else
echo "7days ago bak sql file not exist "
echo "bash complete"
fi

解决!

posted on 2019-03-20 15:01  小小程序员的梦想  阅读(215)  评论(0编辑  收藏  举报

导航