Mysql-數(shù)據(jù)恢復(fù)

要求實(shí)現(xiàn):當(dāng)前mysql數(shù)據(jù)庫恢復(fù)到指定時(shí)間時(shí)的數(shù)據(jù)鸭叙。
前提:一:有基礎(chǔ)備份 二:log-bin=mysql-bin 開啟
模擬場景:

  1. 對當(dāng)前數(shù)據(jù)庫test做備份 -假設(shè)時(shí)間為2016年9月29日00點(diǎn)
  2. 10月2日對test庫做過了增刪改查等操作
  3. 10月3日對test庫做過了增刪改查等操作-但是這里有操作失誤
  4. 10月4日對test庫做過了增刪改查等操作
  5. 先要求對數(shù)據(jù)做恢復(fù)-不要10月3日做過所有操作
    保留10月2日和10月4日操作
    已有日志
mysql-bin.000037--10月4日
mysql-bin.000036--10月3日
mysql-bin.000035--10月2日
mysql-bin.000034--9月29日
mysql-bin.000033--9月28日
mysql-bin.000032--9月27日
  1. 導(dǎo)入備份前數(shù)據(jù)
    導(dǎo)入9月29日備份數(shù)據(jù)
  2. 將數(shù)據(jù)恢復(fù)到10月2日
mysqlbinlog /var/lib/mysql/mysql-bin.000035 | mysql -uroot -p123456
  1. 將數(shù)據(jù)恢復(fù)到10月4日
mysqlbinlog  /var/lib/mysql/mysql-bin.000037 | mysql -uroot -p123456

這個(gè)最簡潔的,當(dāng)mysql-bin文件分的很清楚時(shí)。
但當(dāng)mysql-bin文件很大-如10月2日-10月40日的數(shù)據(jù)都在一個(gè)文件中帜羊,就需要根據(jù)時(shí)間來恢復(fù)把沼。

  1. 指定恢復(fù)的時(shí)間段
mysqlbinlog   --start-datetime="2016-10-2 00:00:00" --stop-datetime="2016-10-2 23:59:59" /var/lib/mysql/mysql-bin.000001 | mysql
mysqlbinlog   --start-datetime="2016-10-4 00:00:00" --stop-datetime="2016-10-4 23:59:59" /var/lib/mysql/mysql-bin.000001 | mysql
  1. 根據(jù)數(shù)據(jù)點(diǎn)來恢復(fù)
    這里有個(gè)疑問-如何知道數(shù)據(jù)錯(cuò)誤時(shí)間的同步點(diǎn)呢寥院?
    show binlog events;
    這個(gè)命令可以查到,如果數(shù)據(jù)量多的話摄悯,感覺也不怎么可行呀,還是按時(shí)間點(diǎn)靠譜點(diǎn)愧捕。

  2. 在錯(cuò)誤之后的數(shù)據(jù)備份上 恢復(fù)是否靠譜射众?
    有之前的增量文件,但是數(shù)據(jù)備份卻是在錯(cuò)誤之后基礎(chǔ)上備份的晃财,
    不知道能不能恢復(fù)叨橱,這個(gè)有待測試。

mysqlbinlog --help
mysqlbinlog Ver 3.4 for Linux at x86_64
Copyright (c) 2000, 2016, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Dumps a MySQL binary log in a format usable for viewing or for piping to
the mysql command line client.

Usage: mysqlbinlog [options] log-files
  -?, --help          Display this help and exit.
  --base64-output=name 
                      Determine when the output statements should be
                      base64-encoded BINLOG statements: 'never' disables it and
                      works only for binlogs without row-based events;
                      'decode-rows' decodes row events into commented
                      pseudo-SQL statements if the --verbose option is also
                      given; 'auto' prints base64 only when necessary (i.e.,
                      for row-based events and format description events).  If
                      no --base64-output[=name] option is given at all, the
                      default is 'auto'.
  --bind-address=name IP address to bind to.
  --character-sets-dir=name 
                      Directory for character set files.
  -d, --database=name List entries for just this database (local log only).
  --debug-check       Check memory and open file usage at exit .
  --debug-info        Print some debug info at exit.
  --default-auth=name Default authentication client-side plugin to use.
  -D, --disable-log-bin 
                      Disable binary log. This is useful, if you enabled
                      --to-last-log and are sending the output to the same
                      MySQL server. This way you could avoid an endless loop.
                      You would also like to use it when restoring after a
                      crash to avoid duplication of the statements you already
                      have. NOTE: you will need a SUPER privilege to use this
                      option.
  -F, --force-if-open Force if binlog was not closed properly.
                      (Defaults to on; use --skip-force-if-open to disable.)
  -f, --force-read    Force reading unknown binlog events.
  -H, --hexdump       Augment output with hexadecimal and ASCII event dump.
  -h, --host=name     Get the binlog from server.
  -l, --local-load=name 
                      Prepare local temporary files for LOAD DATA INFILE in the
                      specified directory.
  -o, --offset=#      Skip the first N entries.
  -p, --password[=name] 
                      Password to connect to remote server.
  --plugin-dir=name   Directory for client-side plugins.
  -P, --port=#        Port number to use for connection or 0 for default to, in
                      order of preference, my.cnf, $MYSQL_TCP_PORT,
                      /etc/services, built-in default (3306).
  --protocol=name     The protocol to use for connection (tcp, socket, pipe,
                      memory).
  -R, --read-from-remote-server 
                      Read binary logs from a MySQL server. This is an alias
                      for read-from-remote-master=BINLOG-DUMP-NON-GTIDS.
  --read-from-remote-master=name 
                      Read binary logs from a MySQL server through the
                      COM_BINLOG_DUMP or COM_BINLOG_DUMP_GTID commands by
                      setting the option to either BINLOG-DUMP-NON-GTIDS or
                      BINLOG-DUMP-GTIDS, respectively. If
                      --read-from-remote-master=BINLOG-DUMP-GTIDS is combined
                      with --exclude-gtids, transactions can be filtered out on
                      the master avoiding unnecessary network traffic.
  --raw               Requires -R. Output raw binlog data instead of SQL
                      statements, output is to log files.
  -r, --result-file=name 
                      Direct output to a given file. With --raw this is a
                      prefix for the file names.
  --secure-auth       Refuse client connecting to server if it uses old
                      (pre-4.1.1) protocol.
                      (Defaults to on; use --skip-secure-auth to disable.)
  --server-id=#       Extract only binlog entries created by the server having
                      the given id.
  --server-id-bits=#  Set number of significant bits in server-id
  --set-charset=name  Add 'SET NAMES character_set' to the output.
  -s, --short-form    Just show regular queries: no extra info and no row-based
                      events. This is for testing only, and should not be used
                      in production systems. If you want to suppress
                      base64-output, consider using --base64-output=never
                      instead.
  -S, --socket=name   The socket file to use for connection.
  --start-datetime=name 
                      Start reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  -j, --start-position=# 
                      Start reading the binlog at position N. Applies to the
                      first binlog passed on the command line.
  --stop-datetime=name 
                      Stop reading the binlog at first event having a datetime
                      equal or posterior to the argument; the argument must be
                      a date and time in the local time zone, in any format
                      accepted by the MySQL server for DATETIME and TIMESTAMP
                      types, for example: 2004-12-25 11:25:56 (you should
                      probably use quotes for your shell to set it properly).
  --stop-never        Wait for more data from the server instead of stopping at
                      the end of the last log. Implicitly sets --to-last-log
                      but instead of stopping at the end of the last log it
                      continues to wait till the server disconnects.
  --stop-never-slave-server-id=# 
                      The slave server_id used for --read-from-remote-server
                      --stop-never.
  --stop-position=#   Stop reading the binlog at position N. Applies to the
                      last binlog passed on the command line.
  -t, --to-last-log   Requires -R. Will not stop at the end of the requested
                      binlog but rather continue printing until the end of the
                      last binlog of the MySQL server. If you send the output
                      to the same MySQL server, that may lead to an endless
                      loop.
  -u, --user=name     Connect to the remote server as username.
  -v, --verbose       Reconstruct pseudo-SQL statements out of row events. -v
                      -v adds comments on column data types.
  -V, --version       Print version and exit.
  --open-files-limit=# 
                      Used to reserve file descriptors for use by this program.
  -c, --verify-binlog-checksum 
                      Verify checksum binlog events.
  --binlog-row-event-max-size=# 
                      The maximum size of a row-based binary log event in
                      bytes. Rows will be grouped into events smaller than this
                      size if possible. This value must be a multiple of 256.
  --skip-gtids        Do not print Global Transaction Identifier information
                      (SET GTID_NEXT=... etc).
  --include-gtids=name 
                      Print events whose Global Transaction Identifiers were
                      provided.
  --exclude-gtids=name 
                      Print all events but those whose Global Transaction
                      Identifiers were provided.

Variables (--variable-name=value)
and boolean options {FALSE|TRUE}  Value (after reading options)
--------------------------------- ----------------------------------------
base64-output                     (No default value)
bind-address                      (No default value)
character-sets-dir                (No default value)
database                          (No default value)
debug-check                       FALSE
debug-info                        FALSE
default-auth                      (No default value)
disable-log-bin                   FALSE
force-if-open                     TRUE
force-read                        FALSE
hexdump                           FALSE
host                              (No default value)
local-load                        (No default value)
offset                            0
plugin-dir                        (No default value)
port                              0
read-from-remote-server           FALSE
read-from-remote-master           (No default value)
raw                               FALSE
result-file                       (No default value)
secure-auth                       TRUE
server-id                         0
server-id-bits                    32
set-charset                       (No default value)
short-form                        FALSE
socket                            (No default value)
start-datetime                    (No default value)
start-position                    4
stop-datetime                     (No default value)
stop-never                        FALSE
stop-never-slave-server-id        -1
stop-position                     18446744073709551615
to-last-log                       FALSE
user                              (No default value)
open-files-limit                  64
verify-binlog-checksum            FALSE
binlog-row-event-max-size         4294967040
skip-gtids                        FALSE
include-gtids                     (No default value)
exclude-gtids                     (No default value)
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末断盛,一起剝皮案震驚了整個(gè)濱河市罗洗,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌钢猛,老刑警劉巖伙菜,帶你破解...
    沈念sama閱讀 212,599評論 6 492
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異命迈,居然都是意外死亡贩绕,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,629評論 3 385
  • 文/潘曉璐 我一進(jìn)店門壶愤,熙熙樓的掌柜王于貴愁眉苦臉地迎上來淑倾,“玉大人,你說我怎么就攤上這事征椒〗慷撸” “怎么了?”我有些...
    開封第一講書人閱讀 158,084評論 0 348
  • 文/不壞的土叔 我叫張陵勃救,是天一觀的道長碍讨。 經(jīng)常有香客問我,道長蒙秒,這世上最難降的妖魔是什么勃黍? 我笑而不...
    開封第一講書人閱讀 56,708評論 1 284
  • 正文 為了忘掉前任,我火速辦了婚禮晕讲,結(jié)果婚禮上覆获,老公的妹妹穿的比我還像新娘榜田。我一直安慰自己,他們只是感情好锻梳,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,813評論 6 386
  • 文/花漫 我一把揭開白布箭券。 她就那樣靜靜地躺著,像睡著了一般疑枯。 火紅的嫁衣襯著肌膚如雪辩块。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 50,021評論 1 291
  • 那天荆永,我揣著相機(jī)與錄音废亭,去河邊找鬼。 笑死具钥,一個(gè)胖子當(dāng)著我的面吹牛豆村,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播骂删,決...
    沈念sama閱讀 39,120評論 3 410
  • 文/蒼蘭香墨 我猛地睜開眼掌动,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了宁玫?” 一聲冷哼從身側(cè)響起粗恢,我...
    開封第一講書人閱讀 37,866評論 0 268
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎欧瘪,沒想到半個(gè)月后眷射,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,308評論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡佛掖,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,633評論 2 327
  • 正文 我和宋清朗相戀三年妖碉,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片芥被。...
    茶點(diǎn)故事閱讀 38,768評論 1 341
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡欧宜,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出撕彤,到底是詐尸還是另有隱情鱼鸠,我是刑警寧澤猛拴,帶...
    沈念sama閱讀 34,461評論 4 333
  • 正文 年R本政府宣布羹铅,位于F島的核電站,受9級特大地震影響愉昆,放射性物質(zhì)發(fā)生泄漏职员。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 40,094評論 3 317
  • 文/蒙蒙 一跛溉、第九天 我趴在偏房一處隱蔽的房頂上張望焊切。 院中可真熱鬧扮授,春花似錦、人聲如沸专肪。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,850評論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽嚎尤。三九已至荔仁,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間芽死,已是汗流浹背乏梁。 一陣腳步聲響...
    開封第一講書人閱讀 32,082評論 1 267
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留关贵,地道東北人遇骑。 一個(gè)月前我還...
    沈念sama閱讀 46,571評論 2 362
  • 正文 我出身青樓,卻偏偏與公主長得像揖曾,于是被迫代替她去往敵國和親落萎。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,666評論 2 350

推薦閱讀更多精彩內(nèi)容