备份方式
物理备份:直接拷贝备份库和表对应的文件
cp -r /var/lib/mysql/mysql /mysql.bak
tar -zcvf /mysql.tar.gz /varlib/mysql/mysql/*
逻辑备份:执行备份时,根据已有的库和表生成对应的sql命令,把生成的sql命令存储到指定的备份文件里
备份策略
完全备份: 备份所有数据(表,库,服务器)
差异备份:备份自完全备份后所新产生(备份新产生的数据)
增量备份:备份自上一次备份后,所有新产生(备份新产生的数据)
备份数据时要考虑的问题:备份频率 备份时间 备份策略 存储空间 备份文件的命名 xx.sql
备份策略使用方式
完全 + 增量
完全 + 差异
执行数据备份的方式:周期性计划任务crond 执行备份脚本
完全备份数据mysqldump
mysqldump --flush-logs -uroot -ptest 库名 > 目录/名.sql
mysqldump --flush-logs -uroot -A --events --ignore-table=mysql.events > all.sql
库名的表示方式:
-A --all-database #备份数据库服务器的所有数据
库名 #备份一个库里的所有数据
库 表 #备份一个表里的所有数据( 库和表用空格间隔 )
-B 库名1 库名2 #一起备份多各库里的所有数据
# 提示:
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
处理:
--events --ignore-table=mysql.event.
增量备份与增量恢复
一,启用binlog日志 实现实时增量备份
binlog日志介绍:又被称为二进制日志 是mysql数据库服务日志文件的一种,记录连接服务器后,执行的除查询之外的sql命令
查看的:show desc select
写的:insert update delete
启用binlog日志
vi /etc/my.cnf
[mysqld]
log-bin
server_id=1 #5.7之前的版本不需要使用 值1-255
log-bin=/logdir/test #自定义binlog日志
max_binlog_size=数字m超过 数子m 大小时自动生成下一各日志文件
binlog_format="mixed"
# 启用前
-rw-rw---- 1 mysql mysql 10485760 8月 9 08:10 ibdata1
-rw-rw---- 1 mysql mysql 5242880 8月 9 08:10 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 8月 9 08:08 ib_logfile1
drwx------ 2 mysql mysql 4096 8月 9 08:08 mysql
srwxrwxrwx 1 mysql mysql 0 8月 9 08:08 mysql.sock
# 启用后
-rw-rw---- 1 mysql mysql 10485760 8月 9 09:16 ibdata1
-rw-rw---- 1 mysql mysql 5242880 8月 9 09:16 ib_logfile0
-rw-rw---- 1 mysql mysql 5242880 8月 9 08:08 ib_logfile1
drwx------ 2 mysql mysql 4096 8月 9 08:08 mysql
-rw-rw---- 1 mysql mysql 106 8月 9 09:16 mysqld-bin.000001
-rw-rw---- 1 mysql mysql 20 8月 9 09:16 mysqld-bin.index
srwxrwxrwx 1 mysql mysql 0 8月 9 09:16 mysql.sock
默认文件名 主机名-bin.000001 > 500M 时会自动在生成 主机名-bin.000002
日志索引文件 主机名-bin.index 记录当前已有的binlog日志文件名
# 查看日志当前记录格式
mysql> show variables like '%binlog_format%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| binlog_format | MIXED |
+---------------+-------+
1 row in set (0.01 sec)
有三种记录格式:
statement: 每条修改数据的sql命令都会记录在binlog日志中
row:不记录sql语句上下文相关信息,仅保存那条记录被修改
mixed:是以上两种格式混合使用
# 查看binlog日志内容
mysqlbinlog binlog日志文件名
内容中/* */ 为注释内容
5.7 之前直接sql写直接记sql
# 执行 mysql
mysql> insert into crm_user(name,password,sex) values(11,12,13);
Query OK, 1 row affected, 17 warnings (0.00 sec)
mysql> insert into crm_user(name,password,sex) values(21,22,23);
Query OK, 1 row affected, 17 warnings (0.00 sec)
mysql> insert into crm_user(name,password,sex) values(31,32,33);
Query OK, 1 row affected, 17 warnings (0.00 sec)
# at 106
#190809 9:50:21 server id 1 end_log_pos 134 Intvar
SET INSERT_ID=8/*!*/;
# at 134
#190809 9:50:21 server id 1 end_log_pos 252 Query thread_id=7 exec_time=0 error_code=0
use `crm`/*!*/;
SET TIMESTAMP=1565315421/*!*/;
SET @@session.pseudo_thread_id=7/*!*/;
SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=1, @@session.unique_checks=1, @@session.autocommit=1/*!*/;
SET @@session.sql_mode=0/*!*/;
SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
SET @@session.lc_time_names=0/*!*/;
SET @@session.collation_database=DEFAULT/*!*/;
insert into crm_user(name,password,sex) values(11,12,13)
/*!*/;
# at 252
#190809 9:52:26 server id 1 end_log_pos 373 Query thread_id=6 exec_time=0 error_code=0
SET TIMESTAMP=1565315546/*!*/;
/*!\C utf8 *//*!*/;
SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=8/*!*/;
UPDATE `crm`.`crm_user` SET `sex` = 133 WHERE `user_id` = 8
/*!*/;
# at 373
#190809 9:55:47 server id 1 end_log_pos 401 Intvar
SET INSERT_ID=9/*!*/;
# at 401
#190809 9:55:47 server id 1 end_log_pos 519 Query thread_id=7 exec_time=0 error_code=0
SET TIMESTAMP=1565315747/*!*/;
/*!\C latin1 *//*!*/;
SET @@session.character_set_client=8,@@session.collation_connection=8,@@session.collation_server=8/*!*/;
insert into crm_user(name,password,sex) values(21,22,23)
/*!*/;
# at 519
#190809 10:25:27 server id 1 end_log_pos 547 Intvar
SET INSERT_ID=10/*!*/;
# at 547
#190809 10:25:27 server id 1 end_log_pos 665 Query thread_id=7 exec_time=0 error_code=0
SET TIMESTAMP=1565317527/*!*/;
insert into crm_user(name,password,sex) values(31,32,33)
/*!*/;
# binlog日志文件记录sql命令的方式:
时间点
pos点(偏移量)
# at 547
#190809 10:25:27 server id 1 end_log_pos 665
从偏移量量547 到 665 时间 2019-08-09 10:25:27
# 执行binlog里记录的sql命令恢复数据
mysqlbinlog 选项 binlog日志文件名 | mysql -uroot -ptest
选项
时间点 --start-datetime="yyyy/mm/dd hh:mm:ss"
--stop-datetime="yyyy/mm/dd hh:mm:ss"#不声明结束时间 默认读到日志结束
偏移量pos点
--start-positon=数字
--stop-positon=数字
查找其偏移量在 6742-7180 之间
mysqlbinlog --start-position=6742 --stop-position=7180 /opt/binlog/test.000001 |mysql -uroot -p
mysqlbinlog --start-datetime='2019-08-09 11:05:36' --stop-datetime='2019-08-09 11:06:22' /opt/binlog/test.000001 |mysql -uroot -p
binlog 如何产生
/etc/init.d/mysqld restart
mysql> flush logs;
mysql> reset master; #删除当前所有日志文件 重新生成一各日志文件
mysql> purge master logs to 'test.000003'; #删除从最开始到当前日志文件名的文件
mysqldump 指令
[root@localhost ~]# mysqldump -h
mysqldump Ver 10.13 Distrib 5.1.73, for redhat-linux-gnu (x86_64)
Copyright (c) 2000, 2013, 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.
Dumping structure and contents of MySQL databases and tables.
Usage: mysqldump [OPTIONS] database [tables]
OR mysqldump [OPTIONS] --databases [OPTIONS] DB1 [DB2 DB3...]
OR mysqldump [OPTIONS] --all-databases [OPTIONS]
Default options are read from the following files in the given order:
/etc/mysql/my.cnf /etc/my.cnf ~/.my.cnf
The following groups are read: mysqldump client
The following options may be given as the first argument:
--print-defaults Print the program argument list and exit.
--no-defaults Don't read default options from any option file.
--defaults-file=# Only read default options from the given file #.
--defaults-extra-file=# Read this file after the global files are read.
--all Deprecated. Use --create-options instead.
-A, --all-databases Dump all the databases. This will be same as --databases
with all databases selected.
-Y, --all-tablespaces
Dump all the tablespaces.
-y, --no-tablespaces
Do not dump any tablespace information.
--add-drop-database Add a DROP DATABASE before each create.
--add-drop-table Add a DROP TABLE before each create.
--add-locks Add locks around INSERT statements.
--allow-keywords Allow creation of column names that are keywords.
--character-sets-dir=name
Directory for character set files.
-i, --comments Write additional information.
--compatible=name Change the dump to be compatible with a given mode. By
default tables are dumped in a format optimized for
MySQL. Legal modes are: ansi, mysql323, mysql40,
postgresql, oracle, mssql, db2, maxdb, no_key_options,
no_table_options, no_field_options. One can use several
modes separated by commas. Note: Requires MySQL server
version 4.1.0 or higher. This option is ignored with
earlier server versions.
--compact Give less verbose output (useful for debugging). Disables
structure comments and header/footer constructs. Enables
options --skip-add-drop-table --skip-add-locks
--skip-comments --skip-disable-keys --skip-set-charset.
-c, --complete-insert
Use complete insert statements.
-C, --compress Use compression in server/client protocol.
-a, --create-options
Include all MySQL specific create options.
-B, --databases Dump several databases. Note the difference in usage; in
this case no tables are given. All name arguments are
regarded as database names. 'USE db_name;' will be
included in the output.
-#, --debug[=#] This is a non-debug version. Catch this and exit.
--debug-check Check memory and open file usage at exit.
--debug-info Print some debug info at exit.
--default-character-set=name
Set the default character set.
--delayed-insert Insert rows with INSERT DELAYED.
--delete-master-logs
Delete logs on master after backup. This automatically
enables --master-data.
-K, --disable-keys '/*!40000 ALTER TABLE tb_name DISABLE KEYS */; and
'/*!40000 ALTER TABLE tb_name ENABLE KEYS */; will be put
in the output.
-E, --events Dump events.
-e, --extended-insert
Use multiple-row INSERT syntax that include several
VALUES lists.
--fields-terminated-by=name
Fields in the output file are terminated by the given
string.
--fields-enclosed-by=name
Fields in the output file are enclosed by the given
character.
--fields-optionally-enclosed-by=name
Fields in the output file are optionally enclosed by the
given character.
--fields-escaped-by=name
Fields in the output file are escaped by the given
character.
--first-slave Deprecated, renamed to --lock-all-tables.
-F, --flush-logs Flush logs file in server before starting dump. Note that
if you dump many databases at once (using the option
--databases= or --all-databases), the logs will be
flushed for each database dumped. The exception is when
using --lock-all-tables or --master-data: in this case
the logs will be flushed only once, corresponding to the
moment all tables are locked. So if you want your dump
and the log flush to happen at the same exact moment you
should use --lock-all-tables or --master-data with
--flush-logs.
--flush-privileges Emit a FLUSH PRIVILEGES statement after dumping the mysql
database. This option should be used any time the dump
contains the mysql database and any other database that
depends on the data in the mysql database for proper
restore.
-f, --force Continue even if we get an SQL error.
-?, --help Display this help message and exit.
--hex-blob Dump binary strings (BINARY, VARBINARY, BLOB) in
hexadecimal format.
-h, --host=name Connect to host.
--ignore-table=name Do not dump the specified table. To specify more than one
table to ignore, use the directive multiple times, once
for each table. Each table must be specified with both
database and table names, e.g.,
--ignore-table=database.table.
--insert-ignore Insert rows with INSERT IGNORE.
--lines-terminated-by=name
Lines in the output file are terminated by the given
string.
-x, --lock-all-tables
Locks all tables across all databases. This is achieved
by taking a global read lock for the duration of the
whole dump. Automatically turns --single-transaction and
--lock-tables off.
-l, --lock-tables Lock all tables for read.
--log-error=name Append warnings and errors to given file.
--master-data[=#] This causes the binary log position and filename to be
appended to the output. If equal to 1, will print it as a
CHANGE MASTER command; if equal to 2, that command will
be prefixed with a comment symbol. This option will turn
--lock-all-tables on, unless --single-transaction is
specified too (in which case a global read lock is only
taken a short time at the beginning of the dump; don't
forget to read about --single-transaction below). In all
cases, any action on logs will happen at the exact moment
of the dump. Option automatically turns --lock-tables
off.
--max_allowed_packet=#
The maximum packet length to send to or receive from
server.
--net_buffer_length=#
The buffer size for TCP/IP and socket communication.
--no-autocommit Wrap tables with autocommit/commit statements.
-n, --no-create-db Suppress the CREATE DATABASE ... IF EXISTS statement that
normally is output for each dumped database if
--all-databases or --databases is given.
-t, --no-create-info
Don't write table creation info.
-d, --no-data No row information.
-N, --no-set-names Suppress the SET NAMES statement
--opt Same as --add-drop-table, --add-locks, --create-options,
--quick, --extended-insert, --lock-tables, --set-charset,
and --disable-keys. Enabled by default, disable with
--skip-opt.
--order-by-primary Sorts each table's rows by primary key, or first unique
key, if such a key exists. Useful when dumping a MyISAM
table to be loaded into an InnoDB table, but will make
the dump itself take considerably longer.
-p, --password[=name]
Password to use when connecting to server. If password is
not given it's solicited on the tty.
-P, --port=# Port number to use for connection.
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-q, --quick Don't buffer query, dump directly to stdout.
-Q, --quote-names Quote table and column names with backticks (`).
--replace Use REPLACE INTO instead of INSERT INTO.
-r, --result-file=name
Direct output to a given file. This option should be used
in MSDOS, because it prevents new line '\n' from being
converted to '\r\n' (carriage return + line feed).
-R, --routines Dump stored routines (functions and procedures).
--set-charset Add 'SET NAMES default_character_set' to the output.
Enabled by default; suppress with --skip-set-charset.
-O, --set-variable=name
Change the value of a variable. Please note that this
option is deprecated; you can set variables directly with
--variable-name=value.
--single-transaction
Creates a consistent snapshot by dumping all tables in a
single transaction. Works ONLY for tables stored in
storage engines which support multiversioning (currently
only InnoDB does); the dump is NOT guaranteed to be
consistent for other storage engines. While a
--single-transaction dump is in process, to ensure a
valid dump file (correct table contents and binary log
position), no other connection should use the following
statements: ALTER TABLE, DROP TABLE, RENAME TABLE,
TRUNCATE TABLE, as consistent snapshot is not isolated
from them. Option automatically turns off --lock-tables.
--dump-date Put a dump date to the end of the output.
--skip-opt Disable --opt. Disables --add-drop-table, --add-locks,
--create-options, --quick, --extended-insert,
--lock-tables, --set-charset, and --disable-keys.
-S, --socket=name The socket file to use for connection.
--ssl Enable SSL for connection (automatically enabled with
other flags).Disable with --skip-ssl.
--ssl-ca=name CA file in PEM format (check OpenSSL docs, implies
--ssl).
--ssl-capath=name CA directory (check OpenSSL docs, implies --ssl).
--ssl-cert=name X509 cert in PEM format (implies --ssl).
--ssl-cipher=name SSL cipher to use (implies --ssl).
--ssl-key=name X509 key in PEM format (implies --ssl).
--ssl-verify-server-cert
Verify server's "Common Name" in its cert against
hostname used when connecting. This option is disabled by
default.
-T, --tab=name Create tab-separated textfile for each table to given
path. (Create .sql and .txt files.) NOTE: This only works
if mysqldump is run on the same machine as the mysqld
server.
--tables Overrides option --databases (-B).
--triggers Dump triggers for each dumped table.
--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.
-u, --user=name User for login if not current user.
-v, --verbose Print info about the various stages.
-V, --version Output version information and exit.
-w, --where=name Dump only selected records. Quotes are mandatory.
-X, --xml Dump a database as well formed XML.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
--------------------------------- -----------------------------
all TRUE
all-databases FALSE
all-tablespaces FALSE
no-tablespaces FALSE
add-drop-database FALSE
add-drop-table TRUE
add-locks TRUE
allow-keywords FALSE
character-sets-dir (No default value)
comments TRUE
compatible (No default value)
compact FALSE
complete-insert FALSE
compress FALSE
create-options TRUE
databases FALSE
debug-check FALSE
debug-info FALSE
default-character-set utf8
delayed-insert FALSE
delete-master-logs FALSE
disable-keys TRUE
events FALSE
extended-insert TRUE
fields-terminated-by (No default value)
fields-enclosed-by (No default value)
fields-optionally-enclosed-by (No default value)
fields-escaped-by (No default value)
first-slave FALSE
flush-logs FALSE
flush-privileges FALSE
force FALSE
hex-blob FALSE
host (No default value)
insert-ignore FALSE
lines-terminated-by (No default value)
lock-all-tables FALSE
lock-tables TRUE
log-error (No default value)
master-data 0
max_allowed_packet 25165824
net_buffer_length 1046528
no-autocommit FALSE
no-create-db FALSE
no-create-info FALSE
no-data FALSE
order-by-primary FALSE
port 0
quick TRUE
quote-names TRUE
replace FALSE
routines FALSE
set-charset TRUE
single-transaction FALSE
dump-date TRUE
socket (No default value)
ssl FALSE
ssl-ca (No default value)
ssl-capath (No default value)
ssl-cert (No default value)
ssl-cipher (No default value)
ssl-key (No default value)
ssl-verify-server-cert FALSE
tab (No default value)
triggers TRUE
tz-utc TRUE
user (No default value)
verbose FALSE
where (No default value)
mysqlbinlog 指令
[root@localhost ~]# mysqlbinlog -h
mysqlbinlog Ver 3.3 for redhat-linux-gnu at x86_64
Copyright (c) 2000, 2013, 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 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); 'always' prints
base64 whenever possible. 'always' is for debugging only
and should not be used in a production system. If this
argument is not given, the default is 'auto'; if it is
given with no argument, 'always' is used.
--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.
-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.
-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.
-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).
--position=# Deprecated. Use --start-position instead.
--protocol=name The protocol to use for connection (tcp, socket, pipe,
memory).
-R, --read-from-remote-server
Read binary logs from a MySQL server.
-r, --result-file=name
Direct output to a given file.
--server-id=# Extract only binlog entries created by the server having
the given 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-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 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.
Variables (--variable-name=value)
and boolean options {FALSE|TRUE} Value (after reading options)
--------------------------------- -----------------------------
base64-output (No default value)
character-sets-dir (No default value)
database (No default value)
debug-check FALSE
debug-info FALSE
disable-log-bin FALSE
force-if-open TRUE
force-read FALSE
hexdump FALSE
host (No default value)
local-load (No default value)
offset 0
port 0
position 4
read-from-remote-server FALSE
server-id 0
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-position 18446744073709551615
to-last-log FALSE
user (No default value)
open_files_limit 64