zoukankan      html  css  js  c++  java
  • MySQL备份脚本

    ✏️ 通过mysqldump备份数据库


    通过计划任务备份数据库,保留指定天数的数据


    #!/bin/bash
    #Desc: 用于进行数据库备份,基于旧版优化
    #Date: 2020-06-19
    #By: yanjie.li
    
    # 运行脚本前先创建一个备份用户,并授予权限  (建议创建一个专用的备份用户,尽量不要使用root用户)
    # mysql> grant select,insert,lock tables,show view,trigger  on *.* to back@localhost identified by "oodaeh7phoe1iboh7Jua";
    # mysql> flush privileges;
    
    back_user_name=root                     #备份账号
    back_user_pass=oodaeh7phoe1iboh7Jua     #备份账号密码
    back_path=/data/mysql_backup            #备份数据存放路径
    back_date=`date +%F-%H`                 #备份日期
    back_day=5                              #备份保留的天数
    
    
    # --------------------获取mysql中的数据库名--------------------
    #这里默认没有备份系统默认自带库:information_schema、performance_schema、mysql、test库。
    /usr/bin/mysql -u ${back_user_name} -p${back_user_pass} -e "show databases;" |grep -v Database |grep -v information_schema |grep -v performance_schema |grep -v "^mysql$" |grep -v "^test$" > /tmp/DBname
    
    # --------------------数据库备份--mysqldump备份--------------------
    function MysQLBack () {
        #创建备份目录
        if [ ! -d "${back_path}/${back_date}" ];then
            mkdir -p ${back_path}/${back_date}
        fi
    
        echo "--------------------${back_date}--------------------" >> ${back_path}/mysql_back.log
    	#循环备份每个库
        for db_name in `cat /tmp/DBname`; do
            /usr/bin/mysqldump --routines --triggers -u ${back_user_name} -p${back_user_pass} ${db_name} > ${back_path}/${back_date}/${db_name}-${back_date}.sql
            if [ $? -eq 0 ];then
                echo "$db_name   Back   OK!" >> ${back_path}/mysql_back.log
            else
                echo "$db_name   Back   ERROR!" >> ${back_path}/mysql_back.log
            fi
        done
        echo "" >> ${back_path}/mysql_back.log
    }
    
    MysQLBack
    
    # --------------------删除自定义保留天数之前的备份数据,并随机保留两份--------------------
    find /data/mysql_backup/ -maxdepth 1 -type d -and -ctime +${back_day} > /tmp/alldirname    #找出保留天数之前的数据
    # 过滤出所有的数据备份目录 如:2020-06-12-06
    for dir_name in `cat /tmp/alldirname`; do
        if echo ${dir_name} |grep "[0-9]" >> /dev/null ; then
            echo ${dir_name} >> /tmp/delfile
        fi
    done
    
    # 随机保留两份
    if [ ! -d "${back_path}/Oldest/" ];then  mkdir -p ${back_path}/Oldest/ ; fi
    if [ -f "/tmp/delfile" ]; then
        for save in `cat /tmp/delfile |sort -R |head -n2`; do
            if [ -d ${save} ]; then
                cp -r ${save} ${back_path}/Oldest/
            fi
        done
    
        # 删除自定义保留天数之前的备份数据
        for del in `cat /tmp/delfile`; do
            if [ -d ${del} ]; then
                rm -rf ${del}
            fi
        done
    fi
    
    # 删除临时文件
    rm -f /tmp/delfile && rm -f /tmp/alldirname && rm -f /tmp/DBname
    
  • 相关阅读:
    HDU_5688
    微服务的一致性问题
    我来博客园啦
    异常:This application has no explicit mapping for /error, so you are seeing this as a fallback.
    异常:Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException
    异常:Unknown lifecycle phase "mvn". You must specify a valid lifecycle
    SpringMVC中遇到页面跳转出现404错误的问题
    Could not resolve view with name '***' in servlet with name 'dispatcher'
    关于Could not resolve dependencies for project
    让我们相互交流
  • 原文地址:https://www.cnblogs.com/yanjieli/p/13162873.html
Copyright © 2011-2022 走看看