Centos 自动备份mysql
时间:2014-05-07 14:01来源:linux.it.net.cn 作者:it
1、创建备份文件夹
-
cd /mnt/cp_disk/
-
mkdir backup
-
cd backup
-
mkdir databak
-
mkdir db
-
mkdir logs
2、编写执行脚本
-
#!/bin/bash
-
backup_dir=/mnt/cp_disk/backup/databak #备份临时文件存放目录
-
backup_target_dir=/mnt/cp_disk/backup/db #备份文件存放目录
-
backup_logs_dir=/mnt/cp_disk/backup/logs #备份日志目录
-
db=gzcpc
-
DATE=$(date +%Y%m%d)
-
#得到10天前的日期
-
ccDATE=$(date "-d 10 day ago" +%Y%m%d)
-
echo "开始复制数据表" >> $backup_logs_dir/$db$DATE
-
echo "-----------`date +"%Y-%m-%d %H:%M:%S"`--------------------" >> $backup_logs_dir/$db$DATE
-
cp -R /var/lib/mysql/$db $backup_dir/ #mysql数据库的数据目录为/var/lib/mysql/
-
echo "开始压缩数据表" >> $backup_logs_dir/$db$DATE
-
echo "------------------------" >> $backup_logs_dir/$db$DATE
-
cd $backup_dir
-
tar -zcvf $db$DATE.tar.gz $db/ && echo "压缩表结束" >> $backup_logs_dir/$db$DATE
-
mv $db$DATE.tar.gz $backup_target_dir/ && echo "转移成功" >> $backup_logs_dir/$db$DATE
-
if [ $? -eq 0 ]
-
then
-
echo "backup succeed" >> $backup_logs_dir/$db$DATE
-
else
-
echo "backup fail" >> $backup_logs_dir/$db$DATE
-
fi
-
echo "开始删除原数据表" >> $backup_logs_dir/$db$DATE
-
echo "-----------------------" >> $backup_logs_dir/$db$DATE
-
rm -rf $backup_dir/* && echo "删除原数据表" >> $backup_logs_dir/$db$DATE
-
echo "删除10天前数据" >>$backup_logs_dir/$db$DATE
-
if [ -e $backup_target_dir/db$ccDATE.tar.gz ]
-
then
-
rm -rf $backup_target_dir/db$ccDATE.tar.gz
-
echo "Delete $backup_target_dir/db$ccDATE.tar.gz succeed" >>$backup_logs_dir/$db$DATE
-
else
-
echo "Not found $backup_target_dir/db$ccDATE.tar.gz file" >>$backup_logs_dir/$db$DATE
-
fi
-
if [ -e $backup_logs_dir/$db$ccDATE ]
-
then
-
rm -rf $backup_logs_dir/$db$ccDATE
-
echo "Delete $backup_logs_dir/$db$ccDATE succeed" >>$backup_logs_dir/$db$DATE
-
else
-
echo "Not found $backup_logs_dir/$db$ccDATE file" >>$backup_logs_dir/$db$DATE
-
fi
3、赋予脚本权限
-
chmod 755 /home/backup_mysql.sh
4、创建定时任务
-
crontab –e
-
1 0 * * * /bin/bash /home/backup_mysql.sh
(责任编辑:IT) |
------分隔线----------------------------