大表的增量备份如何做?

解决方案 »

  1.   

    有2个表, 第1个表 有一个日期字段  可以根据日期选择当天新增shuju
    另外1个表没有日期字段
      

  2.   

    二进制日志+mysqldump实现、如何使用?
      

  3.   

    启用二进制日志(binlog)采用 binlog 的方法相对来说更灵活,省心省力,而且还可以支持增量备份。
    启用 binlog 时必须要重启 mysqld。首先,关闭 mysqld,打开 my.cnf,加入以下几行: 
    server-id = 1 
    log-bin = binlog
    log-bin-index = binlog.index
    然后启动 mysqld 就可以了。运行过程中会产生 binlog.000001 以及 binlog.index,前面的文件是 mysqld 记录所有对数据的更新操作,后面的文件则是所有 binlog 的索引,都不能轻易删除。关于 binlog 的信息请查看手册。 
    需要备份时,可以先执行一下 SQL 语句,让 mysqld 终止对当前 binlog 的写入,就可以把文件直接备份,这样的话就能达到增量备份的目的了: FLUSH LOGS;如果是备份复制系统中的从服务器,还应该备份 master.info 和 relay-log.info 文件。 
    备份出来的 binlog 文件可以用 MySQL 提供的工具 mysqlbinlog 来查看,如: /usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001该工具允许你显示指定的数据库下的所有 SQL 语句,并且还可以限定时间范围,相当的方便,详细的请查看手册。 
    恢复时,可以采用类似以下语句来做到: /usr/local/mysql/bin/mysqlbinlog /tmp/binlog.000001 | mysql -uyejr -pyejr db_name把 mysqlbinlog 输出的 SQL 语句直接作为输入来执行它。 
    如果你有空闲的机器,不妨采用这种方式来备份。由于作为 slave 的机器性能要求相对不是那么高,因此成本低,用低成本就能实现增量备份而且还能分担一部分数据查询压力,何乐而不为呢?
      

  4.   

    如你所说利用二进制日志来实现mysqldump 是做全备,比如你可以在每周日凌晨 2:00 做 mysqldump -uroot -p1234 mydatabase > backup.sql
    然后每天做一次把当日产生的binlog 文件直接复制就行了.
    建议阅读一下mysql的官方文档中的介绍。http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#disaster-prevention
    5.9. 备份与恢复
    5.9.1. 数据库备份
    5.9.2. 示例用备份与恢复策略
    5.9.3. 自动恢复
    5.9.4. 表维护和崩溃恢复http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html#binary-log
    5.11.3. 二进制日志
    
      

  5.   

    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #090707  9:48:13 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090707  9:48:13 at startup
    # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
    ROLLBACK/*!*/;
    # at 98
    #090707 14:37:00 server id 1  end_log_pos 206   Query   thread_id=2     exec_time=0     error_code=0
    use test/*!*/;
    SET TIMESTAMP=1246948620/*!*/;
    SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
    SET @@session.sql_mode=0/*!*/;
    /*!\C utf8 *//*!*/;
    SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
    CREATE TABLE `a` ( `a` int NULL COMMENT ''  )/*!*/;
    # at 206
    #090707 14:37:12 server id 1  end_log_pos 303   Query   thread_id=4     exec_time=0     error_code=0
    SET TIMESTAMP=1246948632/*!*/;
    INSERT INTO `a` (`a`) VALUES ('3')/*!*/;
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    [root@localhost mysql]# mysqlbinlog binlog.000001
    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #090707  9:48:13 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090707  9:48:13 at startup
    # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
    ROLLBACK/*!*/;
    # at 98
    #090707 14:37:00 server id 1  end_log_pos 206   Query   thread_id=2     exec_time=0     error_code=0
    use test/*!*/;
    SET TIMESTAMP=1246948620/*!*/;
    SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
    SET @@session.sql_mode=0/*!*/;
    /*!\C utf8 *//*!*/;
    SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
    CREATE TABLE `a` ( `a` int NULL COMMENT ''  )/*!*/;
    # at 206
    #090707 14:37:12 server id 1  end_log_pos 303   Query   thread_id=4     exec_time=0     error_code=0
    SET TIMESTAMP=1246948632/*!*/;
    INSERT INTO `a` (`a`) VALUES ('3')/*!*/;
    # at 303
    #090707 14:38:19 server id 1  end_log_pos 382   Query   thread_id=3     exec_time=0     error_code=0
    SET TIMESTAMP=1246948699/*!*/;
    CREATE DATABASE `a`/*!*/;
    # at 382
    #090707 14:38:33 server id 1  end_log_pos 524   Query   thread_id=5     exec_time=0     error_code=0
    use a/*!*/;
    SET TIMESTAMP=1246948713/*!*/;
    SET @@session.foreign_key_checks=0/*!*/;
    CREATE TABLE `a`(
      `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1/*!*/;
    # at 524
    #090707 14:38:33 server id 1  end_log_pos 624   Query   thread_id=5     exec_time=0     error_code=0
    SET TIMESTAMP=1246948713/*!*/;
    INSERT INTO `a` SELECT * FROM `test`.`a`/*!*/;
    # at 624
    #090707 14:38:43 server id 1  end_log_pos 771   Query   thread_id=6     exec_time=0     error_code=0
    SET TIMESTAMP=1246948723/*!*/;
    CREATE TABLE `a_copy`(
      `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1/*!*/;
    # at 771
    #090707 14:38:43 server id 1  end_log_pos 876   Query   thread_id=6     exec_time=0     error_code=0
    SET TIMESTAMP=1246948723/*!*/;
    INSERT INTO `a_copy` SELECT * FROM `test`.`a`/*!*/;
    # at 876
    #090707 14:39:06 server id 1  end_log_pos 982   Query   thread_id=7     exec_time=0     error_code=0
    SET TIMESTAMP=1246948746/*!*/;
    SET @@session.foreign_key_checks=1/*!*/;
    CREATE TABLE `a2` ( `a` int NULL COMMENT ''  )/*!*/;
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    [root@localhost mysql]# mysqlbinlog binlog.000001
    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #090707  9:48:13 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090707  9:48:13 at startup
    # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
    ROLLBACK/*!*/;
    # at 98
    #090707 14:37:00 server id 1  end_log_pos 206   Query   thread_id=2     exec_time=0     error_code=0
    use test/*!*/;
    SET TIMESTAMP=1246948620/*!*/;
    SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
    SET @@session.sql_mode=0/*!*/;
    /*!\C utf8 *//*!*/;
    SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
    CREATE TABLE `a` ( `a` int NULL COMMENT ''  )/*!*/;
    # at 206
    #090707 14:37:12 server id 1  end_log_pos 303   Query   thread_id=4     exec_time=0     error_code=0
    SET TIMESTAMP=1246948632/*!*/;
    INSERT INTO `a` (`a`) VALUES ('3')/*!*/;
    # at 303
    #090707 14:38:19 server id 1  end_log_pos 382   Query   thread_id=3     exec_time=0     error_code=0
    SET TIMESTAMP=1246948699/*!*/;
    CREATE DATABASE `a`/*!*/;
    # at 382
    #090707 14:38:33 server id 1  end_log_pos 524   Query   thread_id=5     exec_time=0     error_code=0
    use a/*!*/;
    SET TIMESTAMP=1246948713/*!*/;
    SET @@session.foreign_key_checks=0/*!*/;
    CREATE TABLE `a`(
      `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1/*!*/;
    # at 524
    #090707 14:38:33 server id 1  end_log_pos 624   Query   thread_id=5     exec_time=0     error_code=0
    SET TIMESTAMP=1246948713/*!*/;
    INSERT INTO `a` SELECT * FROM `test`.`a`/*!*/;
    # at 624
    #090707 14:38:43 server id 1  end_log_pos 771   Query   thread_id=6     exec_time=0     error_code=0
    SET TIMESTAMP=1246948723/*!*/;
    CREATE TABLE `a_copy`(
      `a` int(11) default NULL) ENGINE=MyISAM DEFAULT CHARSET=latin1/*!*/;
    # at 771
    #090707 14:38:43 server id 1  end_log_pos 876   Query   thread_id=6     exec_time=0     error_code=0
    SET TIMESTAMP=1246948723/*!*/;
    INSERT INTO `a_copy` SELECT * FROM `test`.`a`/*!*/;
    # at 876
    #090707 14:39:06 server id 1  end_log_pos 982   Query   thread_id=7     exec_time=0     error_code=0
    SET TIMESTAMP=1246948746/*!*/;
    SET @@session.foreign_key_checks=1/*!*/;
    CREATE TABLE `a2` ( `a` int NULL COMMENT ''  )/*!*/;
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    如何 用 命令直接定位到自己想要的时间点。  mysqlbinlog 。。
      

  6.   

    复制下面的 内容# at 4
    #090707  9:12:14 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090707  9:12:14 at startup
    # Warning: this binlog was not closed properly. Most probably mysqld crashed writing it.
    ROLLBACK/*!*/;
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    [root@bruce mysql]# mysqlbinlog log_bin.000067
    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #090706 11:44:46 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090706 11:44:46 at startup
    ROLLBACK/*!*/;
    # at 98
    #090706 11:58:15 server id 1  end_log_pos 117   Stop
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    [root@bruce mysql]# mysqlbinlog log_bin.000048
    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #090701 20:03:43 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090701 20:03:43 at startup
    ROLLBACK/*!*/;
    # at 98
    #090701 20:11:23 server id 1  end_log_pos 183   Query   thread_id=3     exec_time=0     error_code=0
    use ccxyaccount/*!*/;
    SET TIMESTAMP=1246450283/*!*/;
    SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1/*!*/;
    SET @@session.sql_mode=0/*!*/;
    /*!\C utf8 *//*!*/;
    SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
    delete from `a`/*!*/;
    # at 183
    #090701 20:15:05 server id 1  end_log_pos 202   Stop
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
    [root@bruce mysql]# mysqlbinlog log_bin.000009;
    /*!40019 SET @@session.max_insert_delayed_threads=0*/;
    /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/;
    DELIMITER /*!*/;
    # at 4
    #090604 13:47:39 server id 1  end_log_pos 98    Start: binlog v 4, server v 5.0.45-log created 090604 13:47:39 at startup
    ROLLBACK/*!*/;
    # at 98
    #090604 16:02:19 server id 1  end_log_pos 117   Stop
    DELIMITER ;
    # End of log file
    ROLLBACK /* added by mysqlbinlog */;
    /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/;
      

  7.   

    http://dev.mysql.com/doc/refman/5.1/zh/client-side-scripts.html#mysqlbinlog
    8.6. mysqlbinlog:用于处理二进制日志文件的实用工具服务器生成的二进制日志文件写成二进制格式。要想检查这些文本格式的文件,应使用mysqlbinlog实用工具。应这样调用mysqlbinlog:shell> mysqlbinlog [options] log-files...例如,要想显示二进制日志binlog.000003的内容,使用下面的命令:shell> mysqlbinlog binlog.0000003输出包括在binlog.000003中包含的所有语句,以及其它信息例如每个语句花费的时间、客户发出的线程ID、发出线程时的时间戳等等。....·         --start-datetime=datetime从二进制日志中第1个日期时间等于或晚于datetime参量的事件开始读取。datetime值相对于运行mysqlbinlog的机器上的本地时区。该值格式应符合DATETIME或TIMESTAMP数据类型。例如:shell> mysqlbinlog --start-datetime="2004-12-25 11:25:56" binlog.000003该选项可以帮助点对点恢复。·         --stop-datetime=datetime从二进制日志中第1个日期时间等于或晚于datetime参量的事件起停止读。关于datetime值的描述参见--start-datetime选项。该选项可以帮助及时恢复。