MySQL 데이터베이스 백업 및 복구 (6) -mysqldump 하위 테이블 백업 스크립트 작성
데이터베이스의 모든 데이터 테이블을 백업하는 스크립트를 작성하면 각 데이터 테이블이 SQL 파일을 생성합니다.
1 단계, hist 데이터베이스에 포함 된 모든 테이블의 이름을 제거합니다.
[root@Mysql11 tmp]# mysql -uroot -p123456 hist -e "show tables;"
mysql: [Warning] Using a password on the command line interface can be insecure.
+----------------+
| Tables_in_hist |
+----------------+
| course |
| dept |
| score |
| stu |
| t1 |
+----------------+
step2, 헤더 행 필터링
[root@Mysql11 tmp]# mysql -uroot -p123456 hist -e "show tables;"|grep -Evi "table"
mysql: [Warning] Using a password on the command line interface can be insecure.
course
dept
score
stu
t1
step3, 스크립트 작성
vim backup_tables.sh
스크립트의 내용은 다음과 같습니다.
### 利用for循环取出数据库中所有的表名称,具体的原理参见step1和step2
for tablename in `mysql -uroot -p123456 hist -e "show tables;"|grep -Evi "table"`
do
### 针对每个表名称生成相应的mysqldump命令
mysqldump -uroot -p123456 --events hist $tablename|gzip > /tmp/${tablename}_bak.sql.gz
done
4 단계, 스크립트에 실행 권한 추가
[root@Mysql11 tmp]# pwd
/tmp
[root@Mysql11 tmp]# chmod +x backup_tables.sh
[root@Mysql11 tmp]# ll
总用量 4
-rwxr-xr-x. 1 root root 184 7月 2 15:37 backup_tables.sh
step5, 스크립트 실행, 실행 결과보기
[root@Mysql11 tmp]# ./backup_tables.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.
mysqldump: [Warning] Using a password on the command line interface can be insecure.
[root@Mysql11 tmp]# ls
backup_tables.sh course_bak.sql.gz dept_bak.sql.gz score_bak.sql.gz stu_bak.sql.gz t1_bak.sql.gz