linux中对mysql操作的shell
#! /bin/bash
#数据库使用的一些方法
#2012年10月17日 19:31:49
#made by qiunet
#
MYSQLHOST=127.0.0.1
MYSQLUSER=root
MYSQLPASS=*******
function EXEC {
if [ $# -ne 1 ];then
echo "EXEC must have one param ,example : EXEC 'show databases'"
return 1
fi
mysql -u$MYSQLUSER -p$MYSQLPASS --default-character-set=utf8 -h$MYSQLHOST -e "$1"
if [ $? -ne 0 ];then
echo "please check sql ,example : EXEC 'show databases'"
return 1
fi
return 0
}
function BACKUPDB {
if [ $# -ne 3 ];then
echo "BACKUPDB must have two param ,example BACKUPDB '--no-data' databaseName databaseName.sql"
return 1
fi
mysqldump --databases $1 -u$MYSQLUSER -p$MYSQLPASS -h$MYSQLHOST $2 > $3
return 0
}
function BACKUPTB {
if [ $# -ne 4 ];then
echo "BACKUPTB must have 4 param ,example BACKUPDB -d databaseName 'table1 table2' databaseName.sql"
return 1
fi
mysqldump -u$MYSQLUSER -p$MYSQLPASS -h$MYSQLHOST $1 $2 $3 > $4
return 0
}
function RESQL {
if [ $# -ne 2 ];then
echo "RESQL must have two param ,example RESQL databaseName databaseName.sql"
return 1
fi
mysql -u$MYSQLUSER -p$MYSQLPASS -h$MYSQLHOST --default-character-set=utf8 $1 < $2
}
# "" 可以禁用shell的再次解析
case "$1" in
# ./edb exec "use databaseName;select * from tableName"
exec)
EXEC "$2"
;;
# ./edb backupdb "--no-data" "databaseName databaseName_0 databaseName_1" databaseName.sql
backupdb)
BACKUPDB "$2" "$3" "$4"
;;
# ./edb backuptb "-d" databaseName "table1 table2" databaseName.sql
backuptb)
BACKUPTB "$2" "$3" "$4" "$5"
;;
# ./edb res databaseName databaseName.sql
res)
RESQL "$2" "$3"
;;
*)
echo do not support this comand,just support command like exec backupdb backuptb res. example:
echo ./edb exec 'use databaseName;select * from tableName;'
echo ./edb backupdb '--no-data' 'databaseName databaseName_0 databaseName_1' databaseName.sql
echo ./edb backuptb '-d' databaseName 'table1 table2' databaseName.sql
echo ./edb res databaseName databaseName.sql
exit 1
;;
esac
exit 0
可以任意备份同一服务器的任意多个数据库,同一库任意多个表.
有好的脚本.大家可以分享哦
edb为该文件名,就不提供下载了!