一、先創建數據庫,下面是遠程創建數據庫的腳本
#!/bin/bash
MYSQL_DIR=/usr/local/mariadb/bin
for databases in $*
do
${MYSQL_DIR}/mysql -h 192.168.1.21 -u root -p123456 -e "create database ${databases};"
done
echo "查看192.168.1.21機器所有數據庫"
${MYSQL_DIR}/mysql -h 192.168.1.21 -u root -p123456 -e "show databases;"
執行後結果
[root@localhost src]# ./auto_createDB.sh test1 test2 test3 test4 test5
查看192.168.1.21機器所有數據庫
+--------------------+
| Database |
+--------------------+
| information_schema |
| mysql |
| performance_schema |
| test |
| test1 |
| test2 |
| test3 |
| test4 |
| test5 |
+--------------------+
二、備份遠程數據庫,單個或多個數據庫
#!/bin/bash
MYSQL_DIR=/usr/local/mariadb/bin
DATE=`date +%Y%m%d%H%M`
if (( $# < 1 ));then
echo "請輸入要備份的數據庫名"
exit 1
fi
for database in $*
do
${MYSQL_DIR}/mysqldump -h 192.168.1.21 -ubackup -p123456 -B ${database}>${database}${DATE}.sql
if [ $? -ne 0 ];then
echo "數據庫${database}備份失敗,請檢查"
exit 1
else
echo "數據庫${database}備份成功!"
fi
done
執行結果
[root@localhost src]# ./auto_bakDB.sh test1 test2 test3 test4
數據庫test1備份成功!
數據庫test2備份成功!
數據庫test3備份成功!
數據庫test4備份成功!
[root@localhost src]# ls test*
test1202007041953.sql test2202007041953.sql test3202007041953.sql test4202007041953.sql
注:備份數據時需要添加備份權限,見下
創建備份用戶及授權
>grant select,lock tables on *.* to "backup"@"192.168.1.%" identified by "123456";
>flush privileges;
>select Host,User,Password from mysql.user;
------------------------end