在“腳本學習之一---菜鳥級別入門---mysql簡單分庫備份【一】”的是有一個弊端的,就是數據庫的庫名都是我們手動寫的。那麼我們有沒有辦法從數據庫裏面把庫名取出來呢?答案是肯定的如下
因爲performance_schema和information_schema數據庫5.5.32安裝默認就有的,我們備份的時候不要備份,因此我們喲啊過濾掉。
[root@demo scripts]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e"show databases;"
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| liu |
| mysql |
| oldboy |
| performance_schema |
| qq |
| riziwenjian |
| shaopeng |
| shujuku |
| test |
| uu |
| wodeshujk |
| xindata |
| zonglizhu |
+--------------------+
[root@demo scripts]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e"show databases"
+--------------------+
| Database |
+--------------------+
| information_schema |
| binlog |
| liu |
| mysql |
| oldboy |
| performance_schema |
| qq |
| riziwenjian |
| shaopeng |
| shujuku |
| test |
| uu |
| wodeshujk |
| xindata |
| zonglizhu |
+--------------------+
[root@demo scripts]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e"show databases"|sed '1,2d'
binlog
liu
mysql
oldboy
performance_schema
riziwenjian
shaopeng
shujuku
test
uu
wodeshujk
xindata
zonglizhu
[root@demo scripts]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e"show databases"|sed '1,2d'|egrep -v "schema"
binlog
liu
mysql
oldboy
riziwenjian
shaopeng
shujuku
test
uu
wodeshujk
xindata
zonglizhu
[root@demo scripts]# vim fenku_bak_auto.sh
#!/bin/sh
for dbname in `mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e"show databases"|sed '1,2d'|egrep -v "schema"`
do
mysqldump -uroot -poldboy123 --skip-lock-tables -S /data/3306/mysql.sock -F -B ${dbname}|gzip >/server/backup/${dbname}_$(da
te +%F).sql.gz
done
[root@demo scripts]# sh fenku_bak_auto.sh
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.
[root@demo scripts]# ll /server/backup/
總用量 192
-rw-r--r-- 1 root root 511 1月 17 20:00 binlog_2014-01-17.sql.gz
-rw-r--r-- 1 root root 508 1月 17 20:00 liu_2014-01-17.sql.gz
-rw-r--r-- 1 root root 144320 1月 17 20:00 mysql_2014-01-17.sql.gz
-rw-r--r-- 1 root root 938 1月 17 20:00 oldboy_2014-01-17.sql.gz
-rw-r--r-- 1 root root 509 1月 17 20:00 qq_2014-01-17.sql.gz
-rw-r--r-- 1 root root 517 1月 17 20:00 riziwenjian_2014-01-17.sql.gz
-rw-r--r-- 1 root root 513 1月 17 20:00 shaopeng_2014-01-17.sql.gz
-rw-r--r-- 1 root root 513 1月 17 20:00 shujuku_2014-01-17.sql.gz
-rw-r--r-- 1 root root 511 1月 17 20:00 test_2014-01-17.sql.gz
-rw-r--r-- 1 root root 509 1月 17 20:00 uu_2014-01-17.sql.gz
-rw-r--r-- 1 root root 515 1月 17 20:00 wodeshujk_2014-01-17.sql.gz
-rw-r--r-- 1 root root 511 1月 17 20:00 xindata_2014-01-17.sql.gz
-rw-r--r-- 1 root root 515 1月 17 20:00 zonglizhu_2014-01-17.sql.gz
-- Warning: Skipping the data of table mysql.event. Specify the --events option explicitly.備份mysql庫,就會有這個。解決方法:
[root@demo scripts]# mysql -uroot -poldboy123 -S /data/3306/mysql.sock -e"show databases"|sed '1,2d'|egrep -v "mysql|schema"
binlog
liu
oldboy
riziwenjian
shaopeng
shujuku
test
uu
wodeshujk
xindata
zonglizhu