vim backup_mysqldata.sh
#!/bin/bash
# gzszwy计划任务,每天中午12点执行备份工作: 00 12 * * * bbders sh /home/gzszwy/shell/backup_mysqldata.sh
if test ! -d /data/backup/mysql;then
mkdir -p /data/backup/mysql
fi
cd /data/backup/mysql
dbhost="10.233.54.128"
outlog='backup.log'
#数据库名,可以定义多个数据库,中间以空格隔开,如:test test1 test2
dbname='user'
###dbname='asc'
DATETIME=$(date +'%Y%m%d_%H%M%S')
mkdir -p /data/backup/mysql/${DATETIME}
cd /data/backup/mysql/${DATETIME}
echo 'starting mysql backup ...' > ${outlog}
date >> ${outlog}
num=$(ps -ef|grep mysqldump |grep -v grep|wc -l)
if test ${num} -eq 0;then
for i in $dbname;do
mysqldump -h ${dbhost} -P 8306 -u root -p123456 --set-gtid-purged=OFF -B ${i} | gzip > ${i}_${DATETIME}.gz
if test $? -eq 0;then
echo "backup database ${i} ok" >> ${outlog}
else
echo "backup database ${i} failed" >> ${outlog}
fi
done
else
echo "last backup action is running , drop this backup action" >> ${outlog}
fi
find /data/backup/mysql/ -ctime +20 -exec rm -rf {} \;
date >> ${outlog}
echo 'end' >> ${outlog}
crontab -e
45 01 * * * /usr/bin/sh /data/backup/backup_mysqldata.sh
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
暂无评论内容