美文网首页数据库设计mysql
Mysqldump备份数据库

Mysqldump备份数据库

作者: 词穷又词贫 | 来源:发表于2017-01-20 10:34 被阅读153次

    常用数据库备份工具:
    mysqldump:mysql工具包自带(数据库比较小)
    xtrabackup:比较好的备份工具(数据库比较大)

    备份用户授权:
    # select 选择表
    # reload 刷新日志
    # lock tables 锁表
    # show view 视图
    # trigger 触发器
    ###
    GRANT SELECT,RELOAD,SHOW VIEW,LOCK TABLES,TRIGGER ON . TO 'dumper'@'localhost' IDENTIFIED BY 'manager1';
    GRANT SELECT,RELOAD,SHOW VIEW,LOCK TABLES,TRIGGER ON . TO 'dumper'@'127.0.0.1' IDENTIFIED BY 'manager1';
    准备测试数据库:
    # mysql > CREATE DATABASE test DEFAULT CHARACTER SET utf8;
    # mysql > USE test;
    # mysql > SOURCE /root/test.sql;
    Mysqldump备份命令:
    #mysqldump --user=dumper --password=manager1 --databases test | gzip - | openssl des3 -salt -k manager1 -out /data/backup/test.sql.gz.des3
    # mysqldump: [Warning] Using a password on the command line interface can be insecure.
    删除测试数据库:
    # mysql > DROP DATABASE test;
    解密备份文件:
    # openssl des3 -d -salt -in test.sql.gz.des3 -out test.sql.gz
    # enter des-ede3-cbc decryption password: #输入加密时密码
    解压缩备份文件:
    # gunzip test.sql.gz
    恢复备份文件:(mysqldump备份会从最开始创建数据库的时候备份,所以无需提前创建数据库,亦可cat sql文件开头检查sql语句)
    # mysql -u root -hlocalhost -p < test.sql
    # Enter password: #输入root用户密码
    连接Mysql检查数据库是否已经恢复

    编写简单备份脚本:
    1、mysqldump备份数据库
    2、备份文件采用gzip压缩,并且采用3des加密
    3、备份文件留存7天,自动进行删除
    4、每天晚上2点准时开始备份
    #!/bin/bash
    #
    BACKDIR=/data/backup/mysql
    MYSQLDUMP=/usr/local/mysql5.7/bin/mysqldump
    DATEFORMAT=$(date +%Y%m%d%H%M)
    DBNAME="test mysql"
    BACKUSER=dumper
    BACKPASSWD=manager1
    LOG=/data/backup/mysql/backup.log
    [ -d $BACKDIR ] || mkdir -p $BACKDIR
    [ -f $LOG ] && touch $LOG
    #
    find "$BACKDIR" -name "mysql-20*" -type f -mmin +3|xargs rm -rf
    [ $? -eq 0 ] && echo -e "${DATEFORMAT} Delete 7 days ago backup file success" || echo -e "${DATEFORMAT} Delete Old backup file failure"
    #
    for DB in $DBNAME ;do
    $MYSQLDUMP --user=$BACKUSER --password=$BACKPASSWD --routines --triggers --single-transaction --flush-logs --databases ${DB} | gzip - | openssl des3 -salt -k manager1 -out ${BACKDIR}/mysql-${DATEFORMAT}-${DB}.sql.gz.des3
    [ $? -eq 0 ] && echo -e "${DATEFORMAT} mysqldump database ${DB} backup success" >> $LOG || echo -e "${DATEFORMAT} mysqldump database ${DB} backup failure" >> $LOG
    done
    #
    crontab -e
    0 2 * * * /bin/bash /root/script/mysql_backup.sh


    相关文章

      网友评论

        本文标题:Mysqldump备份数据库

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