兩個備份資料庫的shell指令碼_linux shell

來源:互聯網
上載者:User

備份資料庫的shell指令碼一

複製代碼 代碼如下:

#!/bin/bash
#指定啟動並執行指令碼shell
#運行指令碼要給使用者執行許可權
bakdir=/backup
month=`date +%m`
day=`date +%d`
year=`date +%Y`
hour=`date +%k`
min=`date +%M`
dirname=$year-$month-$day-$hour-$min
mkdir $bakdir/$dirname
mkdir $bakdir/$dirname/conf
mkdir $bakdir/$dirname/web
mkdir $bakdir/$dirname/db
#熱備份資料庫
cp /opt/mysql/my.cnf $bakdir/$dirname/db/my.cnf
cd /opt/mysql
mysqldump --opt -u zhy -p --password=1986 test>$bakdir/$dirname/db/test.sql
mysqldump --opt -u zhy -p --password=1986 phpwind>$bakdir/$dirname/db/phpwind.sql
#遠程拷貝的目錄要有可寫入權限
scp -r /backup/$dirname root@10.1.1.178:/backup

Database Backup指令碼shell二
複製代碼 代碼如下:

[plain]
#!/bin/bash   
#---------------------------- 
#Target machine configuration 
#---------------------------- 
#DBUser->Database user name 
#DBPasswd->Database password ,not null 
#DBName->Database name 
#SERVER->IP address 
#---------------------------- 
DBUser=**** 
DBPasswd=**** 
DBName=***** 
SERVER=**** 

#-------------------------------- 
#The local machine configuration 
#-------------------------------- 
#LocalDBUser->Database user name 
#LocalDBPasswd->Database password,not null 
#LocalDBName->Database name 
#BackupPath->Backup path,end with '/' 
#LogFile->Log file 
#num->backup numbers 
#-------------------------------- 
LocalDBUser=** 
LocalDBPasswd=*** 
LocalDBName=**** 
BackupPath=**** 
LogFile=**** 
#num=2 -----> Has not been completed 
#------------------------------- 

#------------------------------- 
#Other configuration 
#------------------------------- 
#IgnoreTables->ignore tables,like this: 
#IgnoreTables= '--ignore-table=DBName.TableName1 --ignore-table=DBName.TableName2 .....' 
#NeedTables->need backup tables,if you want to backup all tables,please let it empty 
#like this: NeedTables='table1 table2 table3' 
#------------------------------- 
IgnoreTables='****‘  
NeedTables= 
#begin 

NewFile="$BackupPath"dbBackup$(date +%y%m%d).sql   
OldFile="$BackupPath"dbBackup$(date +%y%m%d --date='5 days ago').sql  

echo "-------------------------------------------" >> $LogFile   
echo $(date +"%y-%m-%d %H:%M:%S") >> $LogFile   
echo $(date +%y%m%d --date='5 days ago') >>$LogFile 
echo "--------------------------" >> $LogFile   
#Delete Old File 
if [ -f $OldFile ]   
then   
   rm -f $OldFile >> $LogFile 2>&1   
   echo "[$OldFile]Delete Old File Success!" >> $LogFile   
else   
   echo "[$OldFile]No Old Backup File!" >> $LogFile   
fi   
if [ -f $NewFile ]   
then   
   echo "[$NewFile]The Backup File is exists,Can't Backup!" >> $LogFile  
   echo "Begin ro recover the data...."   
   mysql -u$LocalDBUser -p$LocalDBPasswd --default-character-set='utf8' $LocalDBName <$NewFile 
   echo "Data has been restored!" >>$LogFile 
else   
    mysqldump -h$SERVER -u$DBUser -p$DBPasswd --default-character-set='utf8' --quick --single-transaction  $DBName  $NeedTables    $IgnoreTables  >$NewFile 
    echo "[$NewFile]Backup Success!" >> $LogFile   
fi 
echo "-------------------------------------------" 
echo "作者 陳建虹"

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.