MySQLデータベースのバックアップとリカバリ(6)-mysqldumpサブテーブルバックアップスクリプトを作成する

MySQLデータベースのバックアップとリカバリ(6)-mysqldumpサブテーブルバックアップスクリプトを作成する

データベース内のすべてのデータテーブルをバックアップするスクリプトを記述します。各データテーブルはsqlファイルを生成します。

step1、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             |
+----------------+

ステップ2、ヘッダー行をフィルターで除外する

[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

ステップ3、スクリプトを書く

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

おすすめ

転載: blog.csdn.net/weixin_44377973/article/details/107085696