MySQL 데이터베이스 백업 및 복구 (5)-간단한 mysqldump 하위 데이터베이스 백업 스크립트 작성

MySQL 데이터베이스 백업 및 복구 (5)-간단한 mysqldump 하위 데이터베이스 백업 스크립트 작성

information_schema 및 performance_schema 데이터베이스를 제외한 모든 데이터베이스를 백업하는 스크립트를 작성하고 각 데이터베이스에 대해 SQL 파일을 생성합니다.

1 단계, MySQL에 포함 된 모든 데이터베이스 이름 제거

[root@Mysql11 tmp]# mysql -uroot -p123456 -e "show databases;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hist               |
| mysql              |
| performance_schema |
| sys                |
| wanggx             |
+--------------------+

2 단계, 시스템 데이터베이스 및 제목 줄 필터링

[root@Mysql11 tmp]# mysql -uroot -p123456 -e "show databases;"|grep -Evi "database|information|performance"
mysql: [Warning] Using a password on the command line interface can be insecure.
hist
mysql
sys
wanggx

step3, 스크립트 작성

vim backup.sh

스크립트의 내용은 다음과 같습니다.

### 利用for循环取出所有的数据库名称,具体的原理参见step1和step2
for dbname in `mysql -uroot -p123456 -e "show databases;"|grep -Evi "database|infor|perfor"`
do
    ### 针对每个数据库名称生成相应的mysqldump命令
    mysqldump -uroot -p123456 --events -B $dbname|gzip > /tmp/${dbname}_bak.sql.gz
done

4 단계, 스크립트에 실행 권한 추가

[root@Mysql11 tmp]# pwd
/tmp
[root@Mysql11 tmp]# vim backup.sh
[root@Mysql11 tmp]# chmod +x backup.sh 
[root@Mysql11 tmp]# ll
总用量 4
-rwxr-xr-x. 1 root root 184 7月   2 15:09 backup.sh

step5, 스크립트 실행, 실행 결과보기

[root@Mysql11 tmp]# ./backup.sh
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.
mysqldump: [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.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 tmp]# ls
backup.sh  hist_bak.sql.gz  mysql_bak.sql.gz  sys_bak.sql.gz  wanggx_bak.sql.gz

추천

출처blog.csdn.net/weixin_44377973/article/details/107084847