MySQL database backup and recovery (5)-write a simple mysqldump sub-database backup script

MySQL database backup and recovery (5)-write a simple mysqldump sub-database backup script

Write a script to back up all databases except the information_schema and performance_schema databases, and each database generates a sql file.

step1, remove all database names included in 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             |
+--------------------+

step2, filter out the system database and title line

[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, write script

vim backup.sh

The content of the script is as follows:

### 利用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

step4, add executable permissions to the script

[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, execute the script, view the execution result

[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

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/107084847