码迷,mamicode.com
首页 > 数据库 > 详细

线上MySQL备份脚本

时间:2016-06-29 01:23:04      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:mysql   数据库备份   databases   

最近在研究数据库备份,定时执行备份任务,这里直接把备份脚本设置为crontab命令定时执行,脚本内容如下:

[root@mysql-db ~]# cat mysql-backup.sh

#!/bin/bash

dbpwd=‘possw0rd123‘

dbuser=root

host=127.0.0.1

port=3306

dbarg=" -u$dbuser -p$dbpwd -h$host -P$port --default-character=utf8 "

DATE=`date +%Y%m%d`

BAK_PATH=/AEBackup/backup/DB_Bak

LOG=$BAK_PATH/$DATE/backup.log

keep_backup_days=7


mkdir -p $BAK_PATH/$DATE/$port


for db in `echo show databases|mysql $dbarg|grep -v "information_schema\|test\|mysql\|Database" ` 

do 

    echo -e "$DATE `date +%T` DUMP $port $db  .. \c " >>$LOG

    mysqldump -B $dbarg -R $db |gzip >$BAK_PATH/$DATE/$port/${db}_${port}.sql.gz

    echo -e "`date +%T` OK " >>$LOG

done


for dir in `ls $BAK_PATH|grep [0-9]$`

do

    if [ `ls -l $BAK_PATH|grep [0-9]$|wc -l` -gt 2 ];then

        find ${BAK_PATH}/$dir -name "*.sql.gz*"  -ctime +${keep_backup_days}|xargs rm -f

        find ${BAK_PATH}/$dir -name backup.log  -ctime +${keep_backup_days}|xargs rm -f

        find ${BAK_PATH}/$dir -type d -empty |xargs rm -rf

#清除空目录

        find ${BAK_PATH}/$dir -type d -empty |xargs rm -rf

    fi

done


1 4 * * * sh /DB-Backup/shell/DB_backup.sh


本文出自 “平平淡淡才是真” 博客,请务必保留此出处http://ucode.blog.51cto.com/10837891/1793771

线上MySQL备份脚本

标签:mysql   数据库备份   databases   

原文地址:http://ucode.blog.51cto.com/10837891/1793771

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!