linux - 使用 mysqlbinlog 从 mysql 数据库表中的特定日期恢复数据

标签 linux mysql mariadb mysqlbinlog

我有一个名为“radacct”的 mysql 数据库表,其中包含从 2016-01-01(一月)到 2016-06-29(六月)的数据。由于某种原因,从 2016-06-20 到 2016-06-25 的数据丢失了。我启用了 bin-log,所以我使用“mysqlbinlog”将 bin 文件导出到 .sql 文件,这些 sql 文件包含来自一月、二月、三月的旧数据。以及在 2016-06-20 到 2016-06-25 丢失的数据。如何在不丢失当前数据的情况下恢复丢失的数据。

我正在考虑选择包含缺失数据的 .sql 文件(我使用日期字段来识别缺失数据)并将 INSERT 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>

关于linux - 使用 mysqlbinlog 从 mysql 数据库表中的特定日期恢复数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38090898/

相关文章:

linux - 用于列出终端前台进程组中所有进程的 Bash 脚本

c - 原始硬盘访问/dev/sda] vs/dev/sg[y]?

android - 服务器响应状态与服务器接收时间的关系

python - 为什么mysql.connecter python准备语句返回字节数组中的字符串

mysql - csv导入没有导入行

MYSQL phpmyadmin 错误更新/添加用户帐户

Linux - 可打印字符

c - setuid 和 seteuid 函数的区别

Mysql查询包含

php - 解析并显示MySQL数据到php中