標籤:localhost database website 資料庫 檔案夾
#!/bin/bash
#mysql全備指令碼,此指令碼可以備份多個資料庫,
#為每個資料建立單獨的檔案夾進行備份
#author:yxy
#website:www.sql8.net
#mail:[email protected]
#created:2014-8-14
#Mysql dabase information
#主機
db_host="localhost"
#資料庫帳號
db_user="root"
#資料庫密碼
db_passwd="12356789"
#mysql安裝目錄
MYSQL="/opt/lampp/bin/mysql"
#mysql命令目錄
MYSQLDUMP="/opt/lampp/bin/mysqldump"
#Path information
#存放備份檔案的路徑
BACKUP_DB="/opt/backup/database"
LogFile=$BACKUP_DB"/bak.log"
#Time information
#時間
time=`date +"%Y-%m-%d-%H-%M-%S"`
day=`date +"%d"`
month=`date +"%Y-%m"`
weekday=`date +"%u"`
#Path enable write
if [ ! -w "$BACKUP_DB" ]; then
chmod -R 700 $BACKUP_DB
fi
#echo "**********************************host info****************************************"
#echo "db_host:"$db_host
#echo "db_user:"$db_user
#echo "db_passwd:"$db_passwd
#echo "MYSQL:"$MYSQL
#echo "MYSQLDUMP:"$MYSQLDUMP
#echo "databaseName:"$databaseName
#echo "BACKUP_DB:"$BACKUP_DB
#echo "**********************************************************************************"
#Mysql Backup
Date=`date +%Y%m%d`
Begin=`date +"%Y-%m-%d %H:%M:%S"`
#echo "start backup database:"$databaseName" "$Begin
#databases
#需要備份的資料庫,如果有多個資料庫,請用,分隔
databaseList="dd1,dd2"
for databaseName in `echo "$databaseList" | sed ‘s/,/\n/g‘`
do
echo $databaseName
#fileName
#產生備份檔案名
fileName=$databaseName"-"$time".sql"
#產生備份檔案完整路徑
BACKUP_DBPATH=$BACKUP_DB"/"$databaseName
#echo "***********************************database Info***********************************************"
#echo "BACKUP_DBPATH:"$BACKUP_DBPATH
#echo "fileName:"$fileName
#echo "**********************************************************************************"
if [ ! -d "$BACKUP_DBPATH" ]; then
mkdir "$BACKUP_DBPATH"
fi
#備份mysql
$MYSQLDUMP -u $db_user -p$db_passwd -h $db_host $databaseName > $BACKUP_DBPATH/$fileName
#打包.sql檔案
cd $BACKUP_DBPATH && tar -czf $fileName.tar.gz $fileName && rm -rf $fileName && chmod go-rwx $fileName.tar.gz
Last=`date +"%Y-%m-%d %H:%M:%S"`
#echo "end backup database:"$databaseName" "$Last
#輸出日誌
echo start:$Begin end:$Last $fileName succ >> $LogFile
#關於備份檔案,本指令碼不作處理,如果有需要可以聯絡樓主
done