数据库备份-mydumper

 

数据库的使用中,备份以及恢复是非常重要的一项内容

下面简单介绍一下关于数据库备份工具-mydumper

mydumper官方GitHub地址

关于mydumper的安装,可以在官方地址下包安装,wget 等众多方法都可以让你把包装到需要的服务器

需要注意的是,下载rpm包时,确定好系统版本

安装的话,yum或者rpm -ivh都可以,不再赘述

Mydumper介绍

MySQL在备份方面包含了自身的mysqldump工具,但其只支持单线程工作,这就使得它无法迅速的备份数据。而mydumper作为一个实用工具,能够良好支持多线程工作,这使得它在处理速度方面十倍于传统的mysqldump。其特征之一是在处理过程中需要对列表加以锁定,因此如果我们需要在工作时段执行备份工作,那么会引起DML阻塞。但一般现在的MySQL都有主从,备份也大部分在从上进行,所以锁的问题可以不用考虑。这样mydumper能更好的完成备份任务。

mydumper非常适合备份恢复单库,单表,相较于mysqldumper,多线程有利于更加快速的备份和恢复,也支持远程备份

Mydumper是一个针对MySQL和Drizzle的高性能多线程备份和恢复工具,开发人员主要来自MySQL,Facebook,SkySQL公司。

Mydumper特性

轻量级C语言写的
执行速度比mysqldump快10倍
事务性和非事务性表一致的快照(适用于0.2.2以上版本)
快速的文件压缩
支持导出binlog(新版本里已经不能备份binlog)
支持将备份文件切块
多线程备份(因为是多线程逻辑备份,备份后会生成多个备份文件)
多线程恢复(适用于0.2.1以上版本)
备份时对MyISAM表施加FTWRL(FLUSH TABLES WITH READ LOCK),会阻塞DML语句
以守护进程的工作方式,定时快照和连续二进制日志(适用于0.5.0以上版本)
开源 (GNU GPLv3)

Mydumper项目地址: https://launchpad.net/mydumper/

Mydumper备份机制

Mydumper工作流程图

主要步骤概括

  • 主线程 FLUSH TABLES WITH READ LOCK, 施加全局只读锁,以阻止DML语句写入,保证数据的一致性
  • 读取当前时间点的二进制日志文件名和日志写入的位置并记录在metadata文件中,以供即使点恢复使用
  • N个(线程数可以指定,默认是4)dump线程 START TRANSACTION WITH CONSISTENT SNAPSHOT; 开启读一致的事物
  • dump non-InnoDB tables, 首先导出非事物引擎的表
  • 主线程 UNLOCK TABLES 非事物引擎备份完后,释放全局只读锁
  • dump InnoDB tables, 基于事物导出InnoDB表
  • 事物结束

Mydumper的less locking模式

Mydumper使用--less-locking可以减少锁等待时间,此时mydumper的执行机制大致为

  • 主线程 FLUSH TABLES WITH READ LOCK(全局锁)
  • Dump线程 START TRANSACTION WITH CONSISTENT SNAPSHOT;
  • LL Dump线程 LOCK TABLES non-InnoDB(线程内部锁)
  • 主线程UNLOCK TABLES
  • LL Dump线程 dump non-InnoDB tables
  • LL DUmp线程 UNLOCK non-InnoDB
  • Dump线程 dump InnoDB tables

Mydumper备份所生成的文件

所有的备份文件在一个目录中,目录可以自己指定。

目录中包含一个metadata文件

  • 记录了备份数据库在备份时间点的二进制日志文件名,日志的写入位置,如果是在从库进行备份,还会记录备份时同步至主库的二进制日志文件及写入位置

每个表有两个备份文件

  • database.table-schema.sql 表结构文件
  • database.table.sql 表数据文件
  • 如果对表文件分片,将生成多个备份数据文件,可以指定行数或指定大小分片

binary logs(新版已废弃)

启用--binlogs选项后,二进制文件存放在binlog_snapshot目录下

daemon mode

  • 在这个模式下,有五个目录0、1、binlogs、binlog_snapshot、last_dump。
  • 备份目录是0和1,间隔备份,如果mydumper因某种原因失败而仍然有一个好的快照,当快照完成后,last_dump指向该备份
Mydumper主要有以下两个命令:mydumper用于备份,myloader用于恢复
mydumper --help
Usage: mydumper [OPTION...] multi
-threaded MySQL dumping Help Options: -?, --help Show help options Application Options: -B, --database 要备份的库,不指定则全备 -T, --tables-list 要备份的表,可用逗号分隔多个 -O, --omit-from-file 需要跳过的database.table文件,一行一个,在正则之前指定 -o, --outputdir 备份文件存储的目录 -s, --statement-size INSERT 时 一条语句的bytes数, default 1000000 -r, --rows 备份表时,指定多少行表数据一个文件,此选项会关闭 --chunk-filesize -F, --chunk-filesize 备份表时,指定多大数据一个文件 默认单位为MB -c, --compress 压缩输出文件 -e, --build-empty-files 即使表中无数据,也备份出一个文件(默认无数据则只备份一个表文件)-x, --regex 正则匹配'db.table',eg:--regex '^(?!(mysql|test))'-i, --ignore-engines 忽略的存储引擎 -N, --insert-ignore 使用INSERT IGNORE -m, --no-schemas 不备份表结构 -d, --no-data 不备份表数据 -G, --triggers 备份 triggers -E, --events 备份 events -R, --routines 备份 stored procedures and functions -W, --no-views 不备份 VIEWs -k, --no-locks 不使用临时共享只读锁 WARNING: 会造成数据不一致 --no-backup-locks Do not use Percona backup locks --less-locking Minimize locking time on InnoDB tables. -l, --long-query-guard Set long query timer in seconds, default 60(超时后mydumper会退出) -K, --kill-long-queries Kill掉长查询(mydumper不会退出)-D, --daemon 启用守护进程模式,此模式守护进程将会以某个时间间隔,不间断的对数据库进行备份 -I, --snapshot-interval 守护进程模式的间隔时间 (in minutes), requires --daemon, default 60 -L, --logfile Log file name to use, by default stdout is used --tz-utc SET TIME_ZONE='+00:00' at top of dump to allow dumping of TIMESTAMP data when a server has data in different time zones or data is being moved between servers with different time zones, defaults to on use --skip-tz-utc to disable. --skip-tz-utc --use-savepoints 使用保存点减少锁, needs SUPER privilege --success-on-1146 Not increment error count and Warning instead of Critical in case of table doesn't exist --lock-all-tables Use LOCK TABLE for all, instead of FTWRL -U, --updated-since Use Update_time to dump only tables updated in the last U days --trx-consistency-only Transactional consistency only --complete-insert Use complete INSERT statements that include column names -h, --host The host to connect to -u, --user Username with the necessary privileges -p, --password User password -a, --ask-password Prompt For User password -P, --port TCP/IP port to connect to -S, --socket UNIX domain socket file to use for connection -t, --threads Number of threads to use, default 4 -C, --compress-protocol Use compression on the MySQL connection -V, --version Show the program version and exit -v, --verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2 --defaults-file Use a specific defaults file --ssl Connect using SSL --key The path name to the key file --cert The path name to the certificate file --ca The path name to the certificate authority file --capath The path name to a directory that contains trusted SSL CA certificates in PEM format --cipher A list of permissible ciphers to use for SSL encryption
myloader --help
Usage:
  myloader [OPTION...] multi-threaded MySQL loader

Help Options:
  -?, --help                        Show help options

Application Options:
  -d, --directory                   Directory of the dump to import
  -q, --queries-per-transaction     Number of queries per transaction, default 1000
  -o, --overwrite-tables            Drop tables if they already exist
  -B, --database                    需要还原的数据库
  -s, --source-db                   还原的数据库(即备份出来的数据库里面选)
-e, --enable-binlog Enable binary logging of the restore data -h, --host The host to connect to -u, --user Username with the necessary privileges -p, --password User password -a, --ask-password Prompt For User password -P, --port TCP/IP port to connect to -S, --socket UNIX domain socket file to use for connection -t, --threads Number of threads to use, default 4 -C, --compress-protocol Use compression on the MySQL connection -V, --version Show the program version and exit -v, --verbose Verbosity of output, 0 = silent, 1 = errors, 2 = warnings, 3 = info, default 2  --defaults-file Use a specific defaults file

使用的话根据需要指定选项即可

posted @ 2020-11-25 15:26  菠菜不要香菜  阅读(447)  评论(0编辑  收藏  举报