美文网首页
mysql自动备份

mysql自动备份

作者: 不知不怪 | 来源:发表于2020-06-11 13:17 被阅读0次

    vim mysqlbak.sh
    内容如下

    BACKUP=/home/backup/sql/
    DATETIME=$(date +%Y-%m-%d_%H%M%S)
    echo "===备份开始==="
    echo "备份文件存放于${BACKUP}/$DATETIME.sql.gz"
    DUMP=/usr/bin/mysqldump
    HOST=localhost
    PORT=3306
    DB_USER=root
    DB_PW=root
    [ ! -d "${BACKUP}/$DATETIME" ] && mkdir -p "${BACKUP}/$DATETIME"
    DATABASE=znk
    $DUMP -u${DB_USER} -p${DB_PW} -h${HOST} -P${PORT} -q -R --databases $DATABASE | gzip > ${BACKUP}/$DATETIME/$DATABASE.sql.gz
    cd $BACKUP
    tar -zcvf $DATETIME.tar.gz $DATETIME
    rm -rf ${BACKUP}/$DATETIME
    find $BACKUP -mtime +10 -name "*.sql.gz" -exec rm -rf {} \;
    echo "===备份成功==="
    

    crontab -e
    内容如下

    # */1 * * * * /home/backup/mysqlbak.sh
    0 14,21 * * * /home/backup/mysqlbak.sh
    

    相关文章

      网友评论

          本文标题:mysql自动备份

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