實戰-MySQL定時增量備份(2)

閱讀本文大約須要 9 分鐘html

實戰-MySQL定時全量備份(1)node

實戰-MySQL定時增量備份(2)mysql

實戰-將MySQL備份上傳到私有云(3)git

概要

  • 引言github

  • 增量備份redis

  • 恢復增量備份sql

  • 定時備份數據庫

引言

在產品上線以後,咱們的數據是至關重要的,容不得半點閃失,應該作好萬全的準備,搞很差哪一天被黑客入侵或者惡意刪除,那就 gg 了。因此要對咱們的線上數據庫定時作全量備份與增量備份。例如:天天作一次增量備份,每週作一次全量備份。vim

GitHub 地址:點擊閱讀原文進入性能優化

https://github.com/zonezoen/MySQL_backup
複製代碼

增量備份

首先在進行增量備份以前須要查看一下配置文件,查看 log_bin 是否開啓,由於要作增量備份首先要開啓 log_bin 。首先,進入到 myslq 命令行,輸入以下命令:

show variables like '%log_bin%';
複製代碼

以下命令所示,則爲未開啓

mysql> show variables like '%log_bin%';
+---------------------------------+-------+
| Variable_name                   | Value |
+---------------------------------+-------+
| log_bin                         | OFF   |
| log_bin_basename                |       |
| log_bin_index                   |       |
| log_bin_trust_function_creators | OFF   |
| log_bin_use_v1_row_events       | OFF   |
| sql_log_bin                     | ON    |
+---------------------------------+-------+
複製代碼

修改 MySQL 配置項到以下代碼段:vim /etc/mysql/mysql.conf.d/mysqld.cnf

# Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA

#
# The MySQL  Server configuration file.
#
# For explanations see
# http://dev.mysql.com/doc/mysql/en/server-system-variables.html

[mysqld]
pid-file	= /var/run/mysqld/mysqld.pid
socket		= /var/run/mysqld/mysqld.sock
datadir		= /var/lib/mysql
#log-error	= /var/log/mysql/error.log
# By default we only accept connections from localhost
#bind-address	= 127.0.0.1
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

#binlog setting,開啓增量備份的關鍵
log-bin=/var/lib/mysql/mysql-bin
server-id=123454
複製代碼

修改以後,重啓 mysql 服務,輸入:

show variables like '%log_bin%';
複製代碼

狀態以下:

mysql> show variables like '%log_bin%';
+---------------------------------+--------------------------------+
| Variable_name                   | Value                          |
+---------------------------------+--------------------------------+
| log_bin                         | ON                             |
| log_bin_basename                | /var/lib/mysql/mysql-bin       |
| log_bin_index                   | /var/lib/mysql/mysql-bin.index |
| log_bin_trust_function_creators | OFF                            |
| log_bin_use_v1_row_events       | OFF                            |
| sql_log_bin                     | ON                             |
+---------------------------------+--------------------------------+
複製代碼

好了,作好了充足的準備,那咱們就開始學習增量備份了。

查看當前使用的 mysql_bin.000*** 日誌文件,

show master status;
複製代碼

狀態以下:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000015 |      610 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
複製代碼

當前正在記錄日誌的文件名爲 mysql-bin.000015 。

當前數據庫中有以下數據:

mysql> select * from users;
+-------+------+----+
| name  | sex  | id |
+-------+------+----+
| zone  | 0    |  1 |
| zone1 | 1    |  2 |
| zone2 | 0    |  3 |
+-------+------+----+
複製代碼

咱們插入一條數據:

insert into `zone`.`users` ( `name`, `sex`, `id`) values ( 'zone3', '0', '4');
複製代碼

查看效果:

mysql> select * from users;
+-------+------+----+
| name  | sex  | id |
+-------+------+----+
| zone  | 0    |  1 |
| zone1 | 1    |  2 |
| zone2 | 0    |  3 |
| zone3 | 0    |  4 |
+-------+------+----+
複製代碼

咱們執行以下命令,使用新的日誌文件:

mysqladmin -uroot -123456 flush-logs
複製代碼

日誌文件從 mysql-bin.000015 變爲 mysql-bin.000016,而 mysql-bin.000015 則記錄着剛剛 insert 命令的日誌。上句代碼的效果以下:

mysql> show master status;
+------------------+----------+--------------+------------------+-------------------+
| File             | Position | Binlog_Do_DB | Binlog_Ignore_DB | Executed_Gtid_Set |
+------------------+----------+--------------+------------------+-------------------+
| mysql-bin.000016 |      154 |              |                  |                   |
+------------------+----------+--------------+------------------+-------------------+
複製代碼

那麼到如今爲止,其實已經完成了增量備份了。

恢復增量備份

那麼如今將剛剛插入的數據刪除,效果以下:

delete from `zone`.`users` where `id`='4' 

mysql> select * from users;
+-------+------+----+
| name  | sex  | id |
+-------+------+----+
| zone  | 0    |  1 |
| zone1 | 1    |  2 |
| zone2 | 0    |  3 |
+-------+------+----+
複製代碼

那麼如今就是重點時間了,從 mysql-bin.000015 中恢復數據:

mysqlbinlog /var/lib/mysql/mysql-bin.000015 | mysql -uroot -p123456 zone;
複製代碼

上一句代碼指定了,須要恢復的 mysql_bin 文件,指定了用戶名:root 、密碼:123456 、數據庫名:zone。效果以下:

mysql> select * from users;
+-------+------+----+
| name  | sex  | id |
+-------+------+----+
| zone  | 0    |  1 |
| zone1 | 1    |  2 |
| zone2 | 0    |  3 |
| zone3 | 0    |  4 |
+-------+------+----+
複製代碼

OK,整一個增量備份的操做流程都在這裏了,那麼咱們如何將它寫成腳本文件呢,代碼以下:

#!/bin/bash
#在使用以前,請提早建立如下各個目錄
backupDir=/usr/local/work/backup/daily
#增量備份時複製mysql-bin.00000*的目標目錄,提早手動建立這個目錄
mysqlDir=/var/lib/mysql
#mysql的數據目錄
logFile=/usr/local/work/backup/bak.log
BinFile=/var/lib/mysql/mysql-bin.index
#mysql的index文件路徑,放在數據目錄下的

mysqladmin -uroot -p123456 flush-logs
#這個是用於產生新的mysql-bin.00000*文件
# wc -l 統計行數
# awk 簡單來講awk就是把文件逐行的讀入,以空格爲默認分隔符將每行切片,切開的部分再進行各類分析處理。
Counter=`wc -l $BinFile |awk '{print $1}'`
NextNum=0
#這個for循環用於比對$Counter,$NextNum這兩個值來肯定文件是否是存在或最新的
for file in `cat $BinFile`
do
    base=`basename $file`
    echo $base
    #basename用於截取mysql-bin.00000*文件名,去掉./mysql-bin.000005前面的./
    NextNum=`expr $NextNum + 1`
    if [ $NextNum -eq $Counter ]
    then
        echo $base skip! >> $logFile
    else
        dest=$backupDir/$base
        if(test -e $dest)
        #test -e用於檢測目標文件是否存在,存在就寫exist!到$logFile去
        then
            echo $base exist! >> $logFile
        else
            cp $mysqlDir/$base $backupDir
            echo $base copying >> $logFile
         fi
     fi
done
echo `date +"%Y年%m月%d日 %H:%M:%S"` $Next Bakup succ! >> $logFile

#NODE_ENV=$backUpFolder@$backUpFileName /root/node/v8.11.3/bin/node /usr/local/upload.js

複製代碼

定時備份

輸入以下命令,進入定時任務編輯界面:

crontab -e
複製代碼

添加以下命令,其意思爲:每分鐘執行一次備份腳本,crontab 的具體規則就另外寫文了,與本文主題不太相關。

* * * * * sh /usr/your/path/mysqlbackup.sh
複製代碼

關於 crontab 的介紹,在上一篇推文中就有了,詳情請看上一篇推文


關注微信公衆號,回覆【mysql資源】,獲取 mysql 性能優化高級視頻教程

image
相關文章
相關標籤/搜索