簡體   English   中英

使用mysqlbinlog從mysql數據庫表中的特定日期還原數據

[英]Restore data from specific date in mysql database table using mysqlbinlog

我有一個名為“ radacct”的mysql數據庫表,該表具有2016年1月1日(1月)至2016年6月29日(6月)之間的數據。 由於某些原因,從2016-06-20到2016-06-25的數據丟失了。 bin-log enabledbin-log enabled所以我使用“ mysqlbinlog ”將bin文件導出到.sql文件,這些sql文件包含從Jan,Feb,March ..到2016-06-20到2016年6月25日。 如何在不丟失當前數據的情況下恢復丟失的數據。

我正在考慮選擇包含缺失數據的.sql文件(我使用日期字段來標識缺失數據),然后將INSERT INTO替換為REPLACE INTO

這是sql文件的樣子

#160622 15:06:47 server id 1  end_log_pos 1073739089    Query   thread_id=235   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
UPDATE radacct SET              acctstoptime       = '2016-06-23 00:51:55',              acctsessiontime    = '1',              acctinputoctets    = '0' << 32 |                                   '0',              acctoutputoctets   = '0' << 32 |                                   '0',              acctterminatecause = 'User-Error',              acctstopdelay      = '0',              connectinfo_stop   = ''           WHERE acctsessionid   = '172784299'           AND username          = 'fusernamehighlands'           AND nasipaddress      = '11.11.11.234'
/*!*/;
# at 1073739089
#160622 15:06:47 server id 1  end_log_pos 1073739162    Query   thread_id=235   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
COMMIT
/*!*/;
# at 1073739162
#160622 15:06:47 server id 1  end_log_pos 1073739234    Query   thread_id=238   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
BEGIN
/*!*/;
# at 1073739234
#160622 15:06:47 server id 1  end_log_pos 1073739262    Intvar
SET INSERT_ID=264950750/*!*/;
# at 1073739262
#160622 15:06:47 server id 1  end_log_pos 1073740354    Query   thread_id=238   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
INSERT INTO radacct             (acctsessionid, acctuniqueid, username,              realm, nasipaddress, nasportid,              nasporttype, acctstarttime, acctstoptime,              acctsessiontime, acctauthentic, connectinfo_start,              connectinfo_stop, acctinputoctets, acctoutputoctets,              calledstationid, callingstationid, acctterminatecause,              servicetype, framedprotocol, framedipaddress,              acctstartdelay, acctstopdelay)           VALUES             ('172784282', '984f06cc1b00ee56',              'fusernamepranish',              '', '11.11.11.234', '297796676',              'Ethernet',              DATE_SUB('2016-06-23 00:51:55',                  INTERVAL (1 +                  0) SECOND),              '2016-06-23 00:51:55', '1', 'RADIUS', '',              '',              '0' << 32 |              '0',              '0' << 32 |              '0',              '', 'a8:32:9a:00:c3:17',              'User-Error',              'Framed-User', 'PPP', '',              '0', '0')
/*!*/;
# at 1073740354
#160622 15:06:47 server id 1  end_log_pos 1073740381    Xid = 8589937
COMMIT/*!*/;
# at 1073740381
#160622 15:06:47 server id 1  end_log_pos 1073740453    Query   thread_id=239   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
BEGIN
/*!*/;
# at 1073740453
#160622 15:06:47 server id 1  end_log_pos 1073740481    Intvar
SET INSERT_ID=264950749/*!*/;
# at 1073740481
#160622 15:06:47 server id 1  end_log_pos 1073741574    Query   thread_id=239   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
INSERT INTO radacct             (acctsessionid, acctuniqueid, username,              realm, nasipaddress, nasportid,              nasporttype, acctstarttime, acctstoptime,              acctsessiontime, acctauthentic, connectinfo_start,              connectinfo_stop, acctinputoctets, acctoutputoctets,              calledstationid, callingstationid, acctterminatecause,              servicetype, framedprotocol, framedipaddress,              acctstartdelay, acctstopdelay)           VALUES             ('172784297', '1afbaf4540c44f65',              'wusernamebadri158',              '', '11.11.11.234', '297796655',              'Ethernet',              DATE_SUB('2016-06-23 00:51:55',                  INTERVAL (1 +                  0) SECOND),              '2016-06-23 00:51:55', '1', 'RADIUS', '',              '',              '0' << 32 |              '0',              '0' << 32 |              '0',              '', '44:d9:e7:0c:a7:7e',              'User-Error',              'Framed-User', 'PPP', '',              '0', '0')
/*!*/;
# at 1073741574
#160622 15:06:47 server id 1  end_log_pos 1073741601    Xid = 8589935
COMMIT/*!*/;
# at 1073741601
#160622 15:06:47 server id 1  end_log_pos 1073741673    Query   thread_id=233   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
BEGIN
/*!*/;
# at 1073741673
#160622 15:06:47 server id 1  end_log_pos 1073742164    Query   thread_id=233   exec_time=0     error_code=0
SET TIMESTAMP=1466622407/*!*/;
UPDATE radacct           SET              framedipaddress = '10.4.72.235',              acctsessiontime     = '0',              acctinputoctets     = '0'  << 32 |                                    '0',              acctoutputoctets    = '0' << 32 |                                    '0'           WHERE acctsessionid = '172784288'           AND username        = 'btwamrit'           AND nasipaddress    = '11.11.11.234'
/*!*/;
# at 1073742164
#160622 15:06:47 server id 1  end_log_pos 1073742191    Xid = 8589941
COMMIT/*!*/;
# at 1073742191
#160622 15:06:47 server id 1  end_log_pos 1073742234    Rotate to mysql-bin.000003  pos: 4
DELIMITER ;
# End of log file
ROLLBACK /* added by mysqlbinlog */;
/*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;

您可以使用mysqlbinlog --start-datetime="2016-01-01 00:00:00" --stop-datetime="2016-06-29 23:59:59" <log file> > <sql file path>

我無法恢復已刪除表的數據[ data from specific days of the month eg. day 11,12,13 data from specific days of the month eg. day 11,12,13 ]使用二進制日志,但了解了一些有用的步驟,這些步驟在數據庫可能發生故障時可能很有用。

The MariaDB server's binary log is a set of files containing "events" 
which represent modifications to the contents of a MariaDB database.
These events are written in a binary (i.e. non-human-readable) format.
The mysqlbinlog utility is used to view these events in plain text.
Note: binary logging should be enabled on mysql server. to enable it edit  my.cnf file and under "[mysqld]" section
[mysqld]
....
log-bin  
....

重新啟動mysql服務。現在您將在/ var / lib / mysql目錄中創建mysql二進制日志文件。

For example if there are few binlog files like 
 ls -1   /var/lib/mysql
 mysql-bin.000051  
 mysql-bin.000052  
 mysql-bin.000053  
 mysql-bin.000054 

the sql file can be generated for those bin files and then later be imported into database

 mysqlbinlog mariadb-bin.000051 > /tmp/mariadb-bin51.sql 
 mysqlbinlog mariadb-bin.000052 > /tmp/mariadb-bin51.sql  
 ...
 OR
 for i in `seq 51 58`;do mysqlbinlog mysql-bin.0000${i} > ${i}-log.sql ; sleep 1 ;done

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM