本文针对MySQL的mysqldump工具在使用时,对于某些场景需要备份多个数据库或者排除部分数据库的场景进行举例。主要针对于备份的库是否符合预期,其余一些备份参数可根据使用场景增加调整。
我的环境的mysql的版本为
mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.27 |
+-----------+
1 row in set (0.00 sec)
原本MySQL数据库包含7个数据库。
mysql> show databases;
+--------------------+
| Database |
+--------------------+
| database1 |
| database2 |
| information_schema |
| mysql |
| performance_schema |
| sys |
| xmaster |
+--------------------+
7 rows in set (0.00 sec)
( --database database1 )
备份database1单库
正在上传…重新上传取消
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 0
[mysql@mogdb-kernel-0005 back_demo]$ mysqldump -u root -p'mysql' --databases database1 > backup_01.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 4
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
[mysql@mogdb-kernel-0005 back_demo]$ cat backup_01.sql | grep -i 'create database'
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database1` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
[mysql@mogdb-kernel-0005 back_demo]$
其中mysqldump: [Warning] Using a password on the command line interface can be insecure警告是因为我使用了明文密码的原因。
( --database database1 database2)
备份database1和database2两个库
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 4
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
[mysql@mogdb-kernel-0005 back_demo]$ mysqldump -u root -p'mysql' --databases database1 database2> backup_02.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 8
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
-rw-rw-r-- 1 mysql mysql 1687 Aug 16 12:29 backup_02.sql
[mysql@mogdb-kernel-0005 back_demo]$ cat backup_02.sql | grep -i 'create database'
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database1` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database2` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
( --all-databases ,或者 -A)
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 8
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
-rw-rw-r-- 1 mysql mysql 1687 Aug 16 12:29 backup_02.sql
[mysql@mogdb-kernel-0005 back_demo]$ mysqldump -u root -p'mysql' --all-databases > backup_03.sql
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 1200
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
-rw-rw-r-- 1 mysql mysql 1687 Aug 16 12:29 backup_02.sql
-rw-rw-r-- 1 mysql mysql 1219017 Aug 16 12:44 backup_03.sql
[mysql@mogdb-kernel-0005 back_demo]$ cat backup_03.sql | grep 'CREATE DATABASE \/\*\!32312 IF NOT EXISTS\*\/ '
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `mysql` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database1` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database2` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `xmaster` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
(利用xargs和grep筛选)
?
排除掉information_schema、performance_schema、sys、mysql、database1几个库,只备份其余的库。
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 1200
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
-rw-rw-r-- 1 mysql mysql 1687 Aug 16 12:29 backup_02.sql
-rw-rw-r-- 1 mysql mysql 1219017 Aug 16 12:44 backup_03.sql
[mysql@mogdb-kernel-0005 back_demo]$ mysql -uroot -p'mysql' -N -e "show databases;"|grep -Ev "information_schema|performance_schema|sys|mysql|database1"|xargs mysqldump -uroot -p'mysql' --databases > backup_04.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[mysql@mogdb-kernel-0005 back_demo]$ ll
total 1204
-rw-rw-r-- 1 mysql mysql 1470 Aug 16 12:13 backup_01.sql
-rw-rw-r-- 1 mysql mysql 1687 Aug 16 12:29 backup_02.sql
-rw-rw-r-- 1 mysql mysql 1219017 Aug 16 12:44 backup_03.sql
-rw-rw-r-- 1 mysql mysql 2271 Aug 16 12:50 backup_04.sql
[mysql@mogdb-kernel-0005 back_demo]$ cat backup_04.sql | grep 'CREATE DATABASE \/\*\!32312 IF NOT EXISTS\*\/ '
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `database2` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */;
CREATE DATABASE /*!32312 IF NOT EXISTS*/ `xmaster` /*!40100 DEFAULT CHARACTER SET utf8mb4 C