[mysqldump备份单库、部分库、全库、及排除部分库]

发布时间:2024年01月17日

本文针对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)

1.mysqldump备份单库

( --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警告是因为我使用了明文密码的原因。

2.mysqldump备份部分库

( --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' */;

3.mysqldump备份全库

( --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' */;

4.mysqldump排除部分库备份

(利用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
文章来源:https://blog.csdn.net/weixin_47308871/article/details/126651932
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。