#前言:咱們知道對數據進行備份很重要,出現非正常操做能夠進行對數據進行恢復,下面咱們就來使用一下mysql數據庫自帶的一個邏輯備份工具mysqldumpmysql
#mysqldump:mysql數據自帶的一個備份命令sql
#語法格式:數據庫
mysqldump -u用戶名 -p 數據庫名 > 備份的文件名
#mysqldump邏輯備份的工做原理bash
1、mysqldump命令備份數據的過程,實際是把數據從mysql庫裏以邏輯的sql語句形式直接輸出或生成備份文件的過程 2、恢復的時候就是把sql語句在數據庫裏面執行一遍的過程
#mysqldump幫助文檔session
[root@ctos3 ~]# mysqldump --help > 1.txt [root@ctos3 ~]# cat 1.txt
mysqldump Ver 10.13 Distrib 5.7.29, for Linux (x86_64) Copyright (c) 2000, 2020, 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/my.cnf /etc/mysql/my.cnf /application/mysql/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, except for login file. --defaults-file=# Only read default options from the given file #. --defaults-extra-file=# Read this file after the global files are read. --defaults-group-suffix=# Also read groups with concat(group, suffix) --login-path=# Read this path from the login file. -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. (Defaults to on; use --skip-add-drop-table to disable.) --add-drop-trigger Add a DROP TRIGGER before each create. --add-locks Add locks around INSERT statements. (Defaults to on; use --skip-add-locks to disable.) --allow-keywords Allow creation of column names that are keywords. --apply-slave-statements Adds 'STOP SLAVE' prior to 'CHANGE MASTER' and 'START SLAVE' to bottom of dump. --bind-address=name IP address to bind to. --character-sets-dir=name Directory for character set files. -i, --comments Write additional information. (Defaults to on; use --skip-comments to disable.) --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. (Defaults to on; use --skip-create-options to disable.) -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 This is a non-debug version. Catch this and exit. --debug-info This is a non-debug version. Catch this and exit. --default-character-set=name Set the default character set. --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. (Defaults to on; use --skip-disable-keys to disable.) --dump-slave[=#] This causes the binary log position and filename of the master to be appended to the dumped data output. Setting the value to 1, will printit as a CHANGE MASTER command in the dumped data output; 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. -E, --events Dump events. -e, --extended-insert Use multiple-row INSERT syntax that include several VALUES lists. (Defaults to on; use --skip-extended-insert to disable.) --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. -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-error=name A comma-separated list of error numbers to be ignored if encountered during dump. --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. --include-master-host-port Adds 'MASTER_HOST=<host>, MASTER_PORT=<port>' to 'CHANGE MASTER TO..' in dump produced with --dump-slave. --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. (Defaults to on; use --skip-lock-tables to disable.) --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 Same as --skip-set-charset. --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. (Defaults to on; use --skip-quick to disable.) -Q, --quote-names Quote table and column names with backticks (`). (Defaults to on; use --skip-quote-names to disable.) --replace Use REPLACE INTO instead of INSERT INTO. -r, --result-file=name Direct output to a given file. This option should be used in systems (e.g., DOS, Windows) that use carriage-return linefeed pairs (\r\n) to separate text lines. This option ensures that only a single newline is used. -R, --routines Dump stored routines (functions and procedures). --set-charset Add 'SET NAMES default_character_set' to the output. (Defaults to on; use --skip-set-charset to disable.) --set-gtid-purged[=name] Add 'SET @@GLOBAL.GTID_PURGED' to the output. Possible values for this option are ON, OFF and AUTO. If ON is used and GTIDs are not enabled on the server, an error is generated. If OFF is used, this option does nothing. If AUTO is used and GTIDs are enabled on the server, 'SET @@GLOBAL.GTID_PURGED' is added to the output. If GTIDs are disabled, AUTO does nothing. If no value is supplied then the default (AUTO) value will be considered. --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. (Defaults to on; use --skip-dump-date to disable.) --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. --secure-auth Refuse client connecting to server if it uses old (pre-4.1.1) protocol. Deprecated. Always TRUE --ssl-mode=name SSL connection mode. --ssl Deprecated. Use --ssl-mode instead. (Defaults to on; use --skip-ssl to disable.) --ssl-verify-server-cert Deprecated. Use --ssl-mode=VERIFY_IDENTITY instead. --ssl-ca=name CA file in PEM format. --ssl-capath=name CA directory. --ssl-cert=name X509 cert in PEM format. --ssl-cipher=name SSL cipher to use. --ssl-key=name X509 key in PEM format. --ssl-crl=name Certificate revocation list. --ssl-crlpath=name Certificate revocation list path. --tls-version=name TLS version to use, permitted values are: TLSv1, TLSv1.1, TLSv1.2 --server-public-key-path=name File path to the server public RSA key in PEM format. --get-server-public-key Get server public key -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. (Defaults to on; use --skip-triggers to disable.) --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.) -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. --plugin-dir=name Directory for client-side plugins. --default-auth=name Default authentication client-side plugin to use. --enable-cleartext-plugin Enable/disable the clear text authentication plugin. Variables (--variable-name=value) and boolean options {FALSE|TRUE} Value (after reading options) --------------------------------- ---------------------------------------- all-databases FALSE all-tablespaces FALSE no-tablespaces FALSE add-drop-database FALSE add-drop-table TRUE add-drop-trigger FALSE add-locks TRUE allow-keywords FALSE apply-slave-statements FALSE bind-address (No default value) character-sets-dir (No default value) comments TRUE compatible (No default value) compact FALSE complete-insert FALSE compress FALSE create-options TRUE databases FALSE default-character-set utf8 delete-master-logs FALSE disable-keys TRUE dump-slave 0 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) flush-logs FALSE flush-privileges FALSE force FALSE hex-blob FALSE host (No default value) ignore-error (No default value) include-master-host-port FALSE 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 3306 quick TRUE quote-names TRUE replace FALSE routines FALSE set-charset TRUE single-transaction FALSE dump-date TRUE socket /application/mysql/tmp/mysql.sock secure-auth TRUE ssl TRUE ssl-verify-server-cert 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-crl (No default value) ssl-crlpath (No default value) tls-version (No default value) server-public-key-path (No default value) get-server-public-key FALSE tab (No default value) triggers TRUE tz-utc TRUE user (No default value) verbose FALSE where (No default value) plugin-dir (No default value) default-auth (No default value) enable-cleartext-plugin FALSE
#mysqldump備份使用場景oracle
1、遷移或者升級數據庫時 2、增長從庫時候 3、由於硬件或特殊異常狀況,主庫或從庫宕機 4、人爲的DDL,DML語句,主從庫沒辦法了,全部庫都會執行 5、跨機房災備,須要備份
1、 -B --databases 指定多個庫。增長建庫語句和use語句 2、 --compact 去掉註釋,適合調試輸出 3、 -A --all-databases 備份全部庫 4、 -F 刷新binlog 日誌 5、--master-data 增長binlog日誌文件名及對應的位置點 6、-x --lock-all-tables 全局的讀鎖,會阻止對全部表的寫入操做 7、 -l 該鎖不會阻止讀,也不會阻止新的數據插入 8、 -d 只備份表結構 9、 -t 只備份數據 10、 --signle-transaction 適合innodb事務數據庫備份
#在介紹相關參數的時候,咱們先來了解一下-e參數app
#做用:加上-e參數能夠直接在外面交互式查看數據庫裏面的東西,而不用登陸到數據庫裏面less
#例1:查看數據庫socket
[root@cots3 ~]# mysql -uroot -p -e "show databases;" Enter password: +--------------------+ | Database | +--------------------+ | information_schema | | data | | db | | mysql | | performance_schema | | sys | +--------------------+
#例二:能夠執行多個操做,如進入某個數據庫而後查看錶tcp
[root@cots3 ~]# mysql -uroot -p -e "use data;show tables;" Enter password: +----------------+ | Tables_in_data | +----------------+ | Student | +----------------+
#例三:查看數據庫鏈接狀況,也能夠show full processlist查看完整的語句
[root@cots3 ~]# mysql -uroot -p -e "show processlist;" Enter password: +----+------+-----------+------+---------+------+----------+------------------+ | Id | User | Host | db | Command | Time | State | Info | +----+------+-----------+------+---------+------+----------+------------------+ | 31 | root | localhost | NULL | Query | 0 | starting | show processlist | +----+------+-----------+------+---------+------+----------+------------------+
#參數介紹:id:進程,user:用戶,host:鏈接的主機,db:使用的哪一個庫,command:執行了什麼命令,time:多長時間,state:狀態,info:信息
#1.-B參數
#做用:若是刪除了數據庫,不加-B參數,備份回來是會報錯的,除非將數據庫建立回來,備份時候加了-B參數會有自動建庫語句
#例子:咱們來演示如下加-B和不加-B的效果
#1.先演示不加-B參數的效果 #提示:我這裏建立的表的時候,庫和表的字符集都是使用UTF8,以避免導出中文的時候出現亂碼 mysql> create database data character set=utf8; #表會隨着庫的字符集 Query OK, 1 row affected (0.00 sec) 使用root用戶登陸將data數據庫備份到/opt目錄下 [root@ctos3 ~]# mysqldump -uroot -p data > /opt/mysql.bak.sql Enter password: #查看備份的東西 [root@ctos3 ~]# grep -Ev "#|\*|--|^$" /opt/mysql.bak.sql DROP TABLE IF EXISTS `Student`; CREATE TABLE `Student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; LOCK TABLES `Student` WRITE; INSERT INTO `Student` VALUES (1,'aa','19'),(2,'bb','20'),(3,'張衝','21'); UNLOCK TABLES; #恢復 #將表刪除再恢復 [root@ctos3 ~]# mysql -uroot -p -e "use data;drop table Student;" #刪除表 Enter password: [root@cots3 ~]# mysql -uroot -p -e "select * from data.Student;" #確認表有沒有刪除 Enter password: ERROR 1146 (42S02) at line 1: Table 'data.Student' doesn't exist [root@ctos3 ~]# mysql -uroot -p data < /opt/mysql.bak.sql #進行恢復,沒問題 Enter password: [root@ctos3 ~]# mysql -uroot -p -e "select * from data.Student;" #查看 Enter password: +----+--------+-----+ | id | name | age | +----+--------+-----+ | 1 | aa | 19 | | 2 | bb | 20 | | 3 | 張衝 | 21 | +----+--------+-----+ #問題:若是刪除了數據庫,若是備份的時候不加-B參數,備份回來是會報錯的,除非將數據庫建立回來 mysql> drop database data; Query OK, 1 row affected (0.02 sec) [root@ctos3 ~]# mysql -uroot -p data < /opt/mysql.bak.sql #恢復的時候就會報錯,備份的時候沒有加-B參數,沒有建庫語句 Enter password: ERROR 1049 (42000): Unknown database 'data' [root@ctos3 ~]# mysql -uroot -p -e "create database data;" #將數據庫建立回來 Enter password: [root@ctos3 ~]# mysql -uroot -p -e "show databases;" | grep -E "data" Enter password: data [root@ctos3 ~]# mysql -uroot -p data < /opt/mysql.bak.sql #恢復數據 Enter password: [root@ctos3 ~]# mysql -uroot -p -e "use data;select * from Student;" #查看 Enter password: +----+--------+-----+ | id | name | age | +----+--------+-----+ | 1 | aa | 19 | | 2 | bb | 20 | | 3 | 張衝 | 21 | +----+--------+-----+ #2.演示加-B參數備份 [root@cots3 ~]# mysqldump -uroot -p -B data > /opt/mysql.bak_B.sql #首先使用-B進行備份 Enter password: [root@cots3 ~]# cd /opt/ [root@cots3 opt]# ls mysql.bak_B.sql mysql.bak.sql [root@cots3 opt]# diff mysql.bak.sql mysql.bak_B.sql #對比一下加-B和不加的區別,加了-B會有建立數據庫和鏈接數據庫的命令 18a19,26 > -- Current Database: `data` > -- > > CREATE DATABASE /*!32312 IF NOT EXISTS*/ `data` /*!40100 DEFAULT CHARACTER SET latin1 */; > > USE `data`; > > -- 52c60 < -- Dump completed on 2020-03-12 5:46:28 --- > -- Dump completed on 2020-03-12 5:53:11 [root@cots3 ~]# mysql -uroot -p -e "drop database data;" #刪除數據庫 Enter password: [root@cots3 ~]# mysql -uroot -p < /opt/mysql.bak_B.sql #而後恢復 Enter password: [root@cots3 ~]# mysql -uroot -p -e "select * from data.Student;" Enter password: +----+--------+-----+ | id | name | age | +----+--------+-----+ | 1 | aa | 19 | | 2 | bb | 20 | | 3 | 張衝 | 21 | +----+--------+-----+ #提示:備份的時候加入-B參數好點,還原的時候不用加建立庫
#2.-d參數:只備份表結構
#例子:只備份data庫的全部表的結構
[root@cots3 ~]# mysqldump -uroot -p --compact -d data Enter password: /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `Student` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, `age` varchar(16) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8; /*!40101 SET character_set_client = @saved_cs_client */; /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
#查看test表結構
[root@cots3 ~]# mysqldump -uroot -p --compact -d data test Enter password: /*!40101 SET @saved_cs_client = @@character_set_client */; /*!40101 SET character_set_client = utf8 */; CREATE TABLE `test` ( `id` int(4) NOT NULL AUTO_INCREMENT, `name` char(20) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; /*!40101 SET character_set_client = @saved_cs_client */;
#提示:--compact參數會提供較少的輸出(對調試頗有用)
#3.-t參數:只輸出數據
[root@cots3 ~]# mysqldump -uroot -p --compact -t data Enter password: INSERT INTO `Student` VALUES (1,'aa','19'),(2,'bb','20'),(3,'張衝','21');
#4.可使用gzip對備份的數據進行壓縮
#好處:使用壓縮備份的話將會縮小文件大小,減小佔用空間
[root@cots3 ~]# mysqldump -uroot -p -B data | gzip > /opt/mysql.bak_sql.gz Enter password: [root@cots3 ~]# ls -lh /opt/ total 8.0K -rw-r--r-- 1 root root 2.6K Mar 12 06:32 mysql.bak_B.sql -rw-r--r-- 1 root root 851 Mar 12 07:56 mysql.bak_sql.gz
#3.1.備份單個庫
[root@cots3 ~]# mysqldump -uroot -p -B data > /opt/mysql.bak_data.sql
Enter password:
#3.2.備份多個庫
[root@cots3 ~]# mysqldump -uroot -p -B data db | gzip > /opt/mysql.bak.sql.gz
Enter password:
#提示:-B參數是關鍵,在-B參數後指定庫名,空格隔開備份多個庫
#注意點:在備份多個數據庫的時候最好使用root去備份,要否則若是使用別的用戶取備份,在建立用戶的時候若是沒有指定那個庫,那麼就會相對應的備份不了
#演示效果
[root@cots3 ~]# mysql -uroot -p -e "grant all privileges on db.* to test@localhost identified by 'guoke123';" #建立一個test用戶,指定管理db庫 Enter password: [root@cots3 ~]# mysqldump -utest -p -B data db | gzip > /opt/test_bak1.sql.gz #備份到其餘庫就會報錯,沒有權限 Enter password: mysqldump: Got error: 1044: Access denied for user 'test'@'localhost' to database 'data' when selecting the database [root@cots3 ~]# mysqldump -utest -p -B db | gzip > /opt/test_bak1.sql.gz #備份本身管理的就能夠 Enter password:
#3.3.分庫備份
#簡單理解:分庫就是將數據庫裏面的庫分開來進行備份
#注意點:每一個庫均可以用對應備份的庫做爲庫名結尾加.sql
#分庫備份的意義:有時一個企業的數據庫裏會有多個庫,例如(www,guoke,blog),可是有時候出問題的可能只是某一個庫,若是在備份時把全部的庫都備份成一個數據文件的話,恢復某一個庫的數據時就比較麻煩了
#備份方法
#方法1:使用一條命令完成
[root@cots3 ~]# mkdir /opt/back [root@cots3 ~]# mysql -uroot -p -e "show databases;" | grep -Evi "database|infor|per|sys" | sed -r 's#^([a-z].*$)#mysqldump -uroot -p -B \1|gzip >/opt/back/\1.sql.gz#g' | bash Enter password: Enter password: Enter password: Enter password: [root@cots3 ~]# ls -l /opt/back/ total 232 -rw-r--r-- 1 root root 851 Mar 12 08:14 data.sql.gz -rw-r--r-- 1 root root 1212 Mar 12 08:14 db.sql.gz -rw-r--r-- 1 root root 229070 Mar 12 08:14 mysql.sql.gz #提示:爲何是輸入四次密碼呢,由於第一次是登陸的密碼,其他3次是備份3個庫的時候使用
#命令解釋:首先使用grep過濾出想要備份的庫,而後使用sed來進行替換,-r是支持擴展表達式
#方法二:若是庫少的話就一個庫對一個應該文件,一個一個來,若是庫多久寫腳本,用for循環
[root@cots3 ~]# mkdir /opt/bak [root@cots3 ~]# cat fenku.sh #!/bin/bash for dbname in `mysql -uroot -p"guoke123" -e "show databases;" | grep -Evi "database|infor|per|sys"` do mysqldump -uroot -p"guoke123" --events -B $dbname |gzip > /opt/bak/${dbname}.sql.gz done [root@cots3 ~]# ls -l /opt/bak/ #查看效果 total 232 -rw-r--r-- 1 root root 868 Mar 12 08:23 data.sql.gz -rw-r--r-- 1 root root 1230 Mar 12 08:23 db.sql.gz -rw-r--r-- 1 root root 229088 Mar 12 08:23 mysql.sql.gz
4.1.備份單個表
#語法格式:mysqldump -u 用戶名 -p 數據庫名 表名 > 備份的文件名
#例子:
[root@cots3 ~]# mysqldump -uroot -p db test > /opt/mysql_test.sql Enter password: [root@cots3 ~]# ls -l /opt/ | grep mysql_test -rw-r--r-- 1 root root 1934 Mar 12 08:26 mysql_test.sql
#提示:備份一個表的時候不要加-B,要否則全部表都會出來,就會報錯
4.2.備份多個表
#語法格式:mysqldump -u 用於名 -p 數據庫名 表名1 表名2 > 備份的文件名
#例子:
[root@cots3 ~]# mysqldump -uroot -p db guoke test > /opt/mysql_table.sql
#4.3使用腳本進行分庫分表備份
[root@ctos3 ~]# cat bak.sh #!/bin/bash MYUSER="root" MYPASS="guoke123" MYLOG="mysql -u$MYUSER -p$MYPASS -e" MYDUMP="mysqldump -u$MYUSER -p$MYPASS -x -F" DBLIST=$($MYLOG "show databases;" | sed 1d | grep -Ev 'info|mysq|per|sys') DIR=/backup [ ! -d $DIR ] && mkdir $DIR cd $DIR for dbname in $DBLIST do TABLIST=$($MYLOG "show tables from $dbname;" | sed 1d) for tabname in $TABLIST do mkdir -p $DIR/$dbname $MYDUMP $dbname $tabname --events |gzip > $DIR/${dbname}/${tabname}_$(date +%F_%T).sql.gz done done
#備份的效果
[root@ctos3 ~]# ls -l /backup/ total 0 drwxr-xr-x. 2 root root 45 Mar 13 09:14 data drwxr-xr-x. 2 root root 48 Mar 13 09:14 db [root@ctos3 ~]# ls -l /backup/data/ total 4 -rw-r--r--. 1 root root 742 Mar 13 09:14 test_2020-03-13_09:14:26.sql.gz [root@ctos3 ~]# ls -l /backup/db/ total 4 -rw-r--r--. 1 root root 699 Mar 13 09:14 student_2020-03-13_09:14:27.sql.gz
#1.通常的恢復數據庫命令
[root@cots3 ~]# mysql -uroot -p < /opt/mysql_test1.sql #備份時加-B參數恢復方法
[root@cots3 ~]# mysql -uroot -p test < /opt/mysql_test1.sql #備份時不加-B參數方法
#2.恢復分庫分表備份的數據
#思路:先將全部庫找出來,而後使用for循環將其恢復。
#提示:若是備份的時候是壓縮了,能夠先解壓,或者可使用sed過濾數據庫名字出來,使用for循環恢復
#例子:
#1.首先備份全部數據庫 [root@cots3 ~]# mysql -uroot -p -e "show databases;" | grep -Evi "database|infor|per|sys|mysql" | sed -r 's#^([a-z].*$)#mysqldump -uroot -p -B \1|gzip >/opt/back/\1.sql.gz#g' | bash Enter password: Enter password: Enter password: [root@cots3 ~]# ls -l /opt/back/ total 8 -rw-r--r-- 1 root root 851 Mar 12 08:48 data.sql.gz -rw-r--r-- 1 root root 1212 Mar 12 08:48 db.sql.gz #2.將數據庫刪除,進行解壓縮再進行恢復 mysql> drop database data; Query OK, 2 rows affected (0.02 sec) mysql> drop database db; Query OK, 3 rows affected (0.02 sec) #3.將備份的文件解壓縮 [root@cots3 ~]# cd /opt/back/ [root@cots3 back]# ls data.sql.gz db.sql.gz [root@cots3 back]# gunzip * [root@cots3 back]# ls data.sql db.sql #4.使用for循環恢復 [root@cots3 ~]# for dbname in `ls /opt/back/*.sql`;do mysql -uroot -p < ${dbname};done Enter password: Enter password:
#3.使用source恢復數據庫
#方法:登陸數據庫mysql>source 備份文件
#例子:
[root@cots3 bak]# mysql -uroot -p -e "drop database data;" #將data庫刪除 mysql> system ls /opt/bak data.sql db.sql mysql.sql mysql> source /opt/bak/data.sql #進行恢復
#全量備份
全量備份就是把數據庫中全部的數據進進行備份
#例子:
[root@cots3 ~]# mysqldump -uroot -p --events -F -A -B |gzip > /opt/back/mysqlback_$(date +%F).sql.gz #備份全部庫 Enter password: [root@cots3 ~]# ls -l /opt/back/mysqlback_2020-03-12.sql.gz -rw-r--r-- 1 root root 186470 Mar 12 10:01 /opt/back/mysqlback_2020-03-12.sql.gz [root@cots3 ~]# mysqldump -uroot -p --events -F -B data | gzip > /opt/back/mysqlbak_$(date +%F).sql.gz #備份單個庫 Enter password:
#增量備份
增量備份就是上一次全量備份以後,到下一次全備的中間的備份就是增量備份
#企業場景全量備份和增量備份部署
1.中小公司,全量通常是天天一次,業務流量低估執行全備,備份時會鎖表 #按天備份的優缺點 優勢:恢復時間短,維護成本低 缺點:佔用空間多,佔用系統資源多,常常鎖表影響用戶體驗 2.大公司周備,每週六00點一次全量,下週日-下週六00點前都是增量 #按周備份的優缺點 優勢:佔用空間小,佔用系統資源少,無需鎖表,用戶體驗好點 缺點:維護成本高,恢復麻煩,時間長
#增量恢復
利用二進制日誌binlog和全備進行的恢復過程,稱爲增量恢復
#增量恢復的應用場景
1.人爲操做數據庫SQL語句破壞主庫 2.只有一個主庫
#增量恢復的必要條件
#開啓MySQL的log-bin日誌功能
/etc/my.cnf [mysqld] log_bin=mysql_bin
server-id=1
#增量恢復主要是靠binlog日誌來進行恢復
#mysqlbinlog是用來解析mysql的binlog日誌的
#mysqlbinlog的日誌做用:用來記錄MySQL內部增刪改查等對MySQL數據庫有更新的內容的記錄
#提示:查看binlog日誌須要使用mysqlbinlog打開
[root@ctos3 data]# mysqlbinlog mysql_bin.000001 /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=1*/; /*!50003 SET @OLD_COMPLETION_TYPE=@@COMPLETION_TYPE,COMPLETION_TYPE=0*/; DELIMITER /*!*/; # at 4 #200313 8:31:31 server id 1 end_log_pos 123 CRC32 0x9ffc5808 Start: binlog v 4, server v 5.7.29-log created 200313 8:31:31 at startup # Warning: this binlog is either in use or was not closed properly. ROLLBACK/*!*/; BINLOG ' I31rXg8BAAAAdwAAAHsAAAABAAQANS43LjI5LWxvZwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAjfWteEzgNAAgAEgAEBAQEEgAAXwAEGggAAAAICAgCAAAACgoKKioAEjQA AQhY/J8= '/*!*/; # at 123 #200313 8:31:31 server id 1 end_log_pos 154 CRC32 0x2f9f0a63 Previous-GTIDs # [empty] # at 154 #200313 8:40:27 server id 1 end_log_pos 219 CRC32 0x49d233b5 Anonymous_GTID last_committed=0 sequence_number=1 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 219 #200313 8:40:27 server id 1 end_log_pos 313 CRC32 0x163c2e24 Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1584103227/*!*/; SET @@session.pseudo_thread_id=3/*!*/; SET @@session.foreign_key_checks=1, @@session.sql_auto_is_null=0, @@session.unique_checks=1, @@session.autocommit=1/*!*/; SET @@session.sql_mode=32/*!*/; SET @@session.auto_increment_increment=1, @@session.auto_increment_offset=1/*!*/; /*!\C utf8 *//*!*/; SET @@session.character_set_client=33,@@session.collation_connection=33,@@session.collation_server=33/*!*/; SET @@session.lc_time_names=0/*!*/; SET @@session.collation_database=DEFAULT/*!*/; create database data /*!*/; # at 313 #200313 8:40:32 server id 1 end_log_pos 378 CRC32 0x404f0c1d Anonymous_GTID last_committed=1 sequence_number=2 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 378 #200313 8:40:32 server id 1 end_log_pos 466 CRC32 0xfcb07e1f Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1584103232/*!*/; create database db /*!*/; # at 466 #200313 8:41:11 server id 1 end_log_pos 531 CRC32 0xb1d1de3b Anonymous_GTID last_committed=2 sequence_number=3 rbr_only=no SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 531 #200313 8:41:11 server id 1 end_log_pos 726 CRC32 0x9dbf88b2 Query thread_id=3 exec_time=0 error_code=0 use `data`/*!*/; SET TIMESTAMP=1584103271/*!*/; create table test ( id int(4) not null auto_increment, name char(20) not null, age varchar(16) not null, primary key(id)) /*!*/; # at 726 #200313 8:41:32 server id 1 end_log_pos 791 CRC32 0x44340287 Anonymous_GTID last_committed=3 sequence_number=4 rbr_only=yes /*!50718 SET TRANSACTION ISOLATION LEVEL READ COMMITTED*//*!*/; SET @@SESSION.GTID_NEXT= 'ANONYMOUS'/*!*/; # at 791 #200313 8:41:32 server id 1 end_log_pos 863 CRC32 0xea8b7d1b Query thread_id=3 exec_time=0 error_code=0 SET TIMESTAMP=1584103292/*!*/; BEGIN /*!*/; # at 863 #200313 8:41:32 server id 1 end_log_pos 916 CRC32 0x8bc89c48 Table_map: `data`.`test` mapped to number 108 # at 916 #200313 8:41:32 server id 1 end_log_pos 981 CRC32 0xbccabc0a Write_rows: table id 108 flags: STMT_END_F BINLOG ' fH9rXhMBAAAANQAAAJQDAAAAAGwAAAAAAAEABGRhdGEABHRlc3QAAwP+DwT+PDAAAEicyIs= fH9rXh4BAAAAQQAAANUDAAAAAGwAAAAAAAEAAgAD//gBAAAACHpoYW5nc2FuAjE5+AIAAAAEbGlz aQIyMAq8yrw= '/*!*/; # at 981 #200313 8:41:32 server id 1 end_log_pos 1012 CRC32 0x24abc81e Xid = 13 COMMIT/*!*/; SET @@SESSION.GTID_NEXT= 'AUTOMATIC' /* added by mysqlbinlog */ /*!*/; DELIMITER ; # End of log file /*!50003 SET COMPLETION_TYPE=@OLD_COMPLETION_TYPE*/; /*!50530 SET @@SESSION.PSEUDO_SLAVE_MODE=0*/;
#恢復方法
#1.基於位置點的增量恢復
[root@ctos3 data]# pwd /application/mysql/data [root@ctos3 data]# ls -l | grep bin -rw-r-----. 1 mysql mysql 1035 Mar 13 08:44 mysql_bin.000001 -rw-r-----. 1 mysql mysql 154 Mar 13 08:44 mysql_bin.000002 -rw-r-----. 1 mysql mysql 38 Mar 13 08:44 mysql_bin.index #指定開始位置到結束位置,從pos466開始到531結束 [root@ctos3 data]# mysqlbinlog --no-defaults mysql_bin.000001 --start-position=466 --stop-position=531 -r pos.sql [root@ctos3 data]# ls -l | grep pos.sql -rw-r-----. 1 root root 936 Mar 13 08:47 pos.sql #指定開始位置到文件結束 [root@ctos3 data]# mysqlbinlog --no-defaults mysql_bin.000001 --start-position=531 -r pos531-end.sql [root@ctos3 data]# ls -l | grep 531 -rw-r-----. 1 root root 2450 Mar 13 08:49 pos531-end.sql #從文件開頭到指定位置結束 #提示:關鍵參數爲at,指定開始和結束的位置
#2.指定時間來恢復
[root@ctos3 data]# mysqlbinlog --no-defaults mysql-bin.000001 --start-datetime='2020-1-22 17:30:40' --stop-datetime='2020-3-22 17:30:58' -r time.sql
#企業生產場景不一樣引擎備份命令
#myisam
[root@ctos3 ~]# mysqldump -uroot -p -A -B --master-data=2 -x --events | gzip > /opt/all.sql.gz
#innodb
[root@ctos3 ~]# mysqldump -uroot -p -A -B --master-data=2 --events --single-transaction | gzip > /opt/all_1.sql.gz