美文网首页
mysql备份脚本

mysql备份脚本

作者: Ppnn13Yu | 来源:发表于2020-05-22 16:43 被阅读0次

    !/bin/bash

    desc: db_bk

    Author: kuvi kkxxmei1tian@foxmial.com

    db_install_dir=/usr/local/mysql
    mysql_data_dir=/data/mysql

    DBname=
    dbrootpwd=

    backup_dir=/data/backup
    expired_days=5

    LogFile={backup_dir}/db.log DumpFile={backup_dir}/DB_{DBname}_(date +%Y%m%d_%H).sql
    NewFile={backup_dir}/DB_{DBname}(date +%Y%m%d_%H).tgz OldFile={backup_dir}/DB{DBname}_(date +%Y%m%d --date="${expired_days} days ago")*.tgz

    [ ! -e "{backup_dir}" ] && mkdir -p{backup_dir}

    DB_tmp=${db_install_dir}/bin/mysql -uroot -p${dbrootpwd} -e "show databases\G" | grep ${DBname}
    [ -z "{DB_tmp}" ] && { echo "[{DBname}] not exist" >> ${LogFile} ; exit 1 ; }

    if [ -n "ls ${OldFile} 2>/dev/null" ]; then
    rm -f {OldFile} echo "[{OldFile}] Delete Old File Success" >> {LogFile} else echo "[{OldFile}] Delete Old Backup File" >> ${LogFile}
    fi

    if [ -e "{NewFile}" ]; then echo "[{NewFile}] The Backup File is exists, Can't Backup" >> {LogFile} else{db_install_dir}/bin/mysqldump -uroot -p{dbrootpwd} --databases{DBname} > {DumpFile} pushd{backup_dir} > /dev/null
    tar czf {NewFile}{DumpFile##*/} >> {LogFile} 2>&1 echo "[{NewFile}] Backup success ">> {LogFile} rm -f{DumpFile}
    popd > /dev/null
    fi

    image.png

    相关文章

      网友评论

          本文标题:mysql备份脚本

          本文链接:https://www.haomeiwen.com/subject/muxcahtx.html