歡迎拍磚,詳細內容如下:
# MYSQL 數據文件路徑
MYSQL_DATA_PATH="/usr/local/mysql/data/"
# 備份目錄
MYSQL_DUMP_PATH="***/DB/"
# 備份用戶名
MYSQL_ACCOUNT="mysql_backup"
# 備份密碼
MYSQL_PASSWORD="mysql_backup"
MYSQLDUMP_OPTS="mysqldump -u$MYSQL_ACCOUNT -p$MYSQL_PASSWORD --single-transaction"
# 壓縮命令 bzip2 -k 保留源文件
TAR_OPTS="bzip2 "
# 單個數據庫進行備份
function BackDB(){
FULLPATH="$1"
#遍歷目錄
for dbName in $(/bin/ls $FULLPATH)
do
# check is database
fileType=`/usr/bin/file $FULLPATH$dbName | /bin/awk '{FS=":"} {printf $2}'`
# 文件夾的類型為directory
if [ "directory" == $fileType ]; then
# start dump
if [ "$dbName" == "mysql" ] \
|| [ "$dbName" == "test" ] \
|| [ "$dbName" == "information_schema" ] \
|| [ "$dbName" == "performance_schema" ]; then
echo "------------->$dbName not need dump!!"
else
echo $dbName www.2cto.com
dumpName=$MYSQL_DUMP_PATH/"$dbName"_"$DATESTR"
$MYSQLDUMP_OPTS $dbName > "$dumpName".sql
# 壓縮備份檔
$TAR_OPTS "$BACKUP_NAME".sql
fi
else
printf '%s is not database ######## \n' $dbName
fi
done
}
# 每個數據表單獨備份
function BackDBTB(){
FULLPATH="$1"
#遍歷目錄
for fName in $(/usr/bin/find $FULLPATH -name "*.ibd")
do
dbPath=`echo "$fName" | /bin/awk 'BEGIN {FS="'$FULLPATH'"} {printf $2}'`
# get database name
dbName=`echo "$dbPath" | /bin/awk 'BEGIN {FS="/"} {printf $1}'`
# get table name
tbName=`echo "$dbPath" | /bin/awk 'BEGIN {FS="/"} {printf $2}'`
# start dump
if [ "$dbName" == "mysql" ]\
|| [ "$dbName" == "test" ] \
|| [ "$dbName" == "information_schema" ] \
|| [ "$dbName" == "performance_schema" ]; then
echo "------------->$dbName not need dump!!"
else
echo $dbName $tbName
dumpName=$MYSQL_DUMP_PATH/"$dbName"_"$tbName"_"$DATESTR"
$MYSQLDUMP_OPTS $dbName $tbName > "$dumpName".sql
# 壓縮備份檔
$TAR_OPTS "$dumpName".sql
fi
done
}
作者 babaoqi