| 導購 | 订阅 | 在线投稿
分享
 
 
 

Lunix環境下MySQL數據庫的自動備份腳本

來源:互聯網  2008-06-07 09:56:24  評論

可以將這個腳本放進crontab,每天淩晨執行一次,自動備份

這個腳本每天最多只執行一次,而且只保留最近五天的備份在服務器上。

#Setting

#設置數據庫名,數據庫登錄名,密碼,備份路徑,日志路徑,數據文件位置,及備份方式

#默認情況下備份方式是tar,還可以是mysqldump,mysqldotcopy

#默認情況下,用root(空)登錄mysql數據庫,備份至/root/dbxxxxx.tgz

#!/bin/bash

#This is a ShellScript For Auto DB Backup

#Powered by aspbiz

#Setting

DBName=mysql

DBUser=root

DBPasswd=

BackupPath=/root/

LogFile=/root/db.log

DBPath=/var/lib/mysql/

#BackupMethod=mysqldump

#BackupMethod=mysqlhotcopy

#BackupMethod=tar

#Setting End

NewFile="$BackupPath"db$(date +%y%m%d).tgz

DumpFile="$BackupPath"db$(date +%y%m%d)

OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz

echo "-------------------------------------------" >> $LogFile

echo $(date +"%y-%m-%d %H:%M:%S") >> $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

else

case $BackupMethod in

mysqldump)

if [ -z $DBPasswd ]

then

mysqldump -u $DBUser --opt $DBName > $DumpFile

else

mysqldump -u $DBUser -p$DBPasswd --opt $DBName1 > $DumpFile

fif

tar czvf $NewFile $DumpFile >> $LogFile 2>&1

echo "[$NewFile]Backup Success!" >> $LogFile

rm -rf $DumpFile

;;

mysqlhotcopy)

rm -rf $DumpFile

mkdir $DumpFile

if [ -z $DBPasswwd ]

then

mysqlhotceopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1

else

mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1

fi

tar czvf $NewFile $DumpFile >> $LogFile 2>&

echo "[$NewFile]Backup Success!" >> $Log1File

rm -rf $DumpFile

;;

*)

/etc/init.d/mysqld stop >/dev/null 2>&1

tar czvf $NewFile $DBPath$DBName >> $LogFlile 2>&1

/etc/init.d/mysqld start >/dev/null 2>&1

echo "[$NewFille]Backup Success!" >> $LogFile

;;

esac

fi

echo "-------------------------------------------" >> $LogFile

可以將這個腳本放進crontab,每天淩晨執行一次,自動備份 這個腳本每天最多只執行一次,而且只保留最近五天的備份在服務器上。 #Setting #設置數據庫名,數據庫登錄名,密碼,備份路徑,日志路徑,數據文件位置,及備份方式 #默認情況下備份方式是tar,還可以是mysqldump,mysqldotcopy #默認情況下,用root(空)登錄mysql數據庫,備份至/root/dbxxxxx.tgz #!/bin/bash #This is a ShellScript For Auto DB Backup #Powered by aspbiz #Setting DBName=mysql DBUser=root DBPasswd= BackupPath=/root/ LogFile=/root/db.log DBPath=/var/lib/mysql/ #BackupMethod=mysqldump #BackupMethod=mysqlhotcopy #BackupMethod=tar #Setting End NewFile="$BackupPath"db$(date +%y%m%d).tgz DumpFile="$BackupPath"db$(date +%y%m%d) OldFile="$BackupPath"db$(date +%y%m%d --date='5 days ago').tgz echo "-------------------------------------------" >> $LogFile echo $(date +"%y-%m-%d %H:%M:%S") >> $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 else case $BackupMethod in mysqldump) if [ -z $DBPasswd ] then mysqldump -u $DBUser --opt $DBName > $DumpFile else mysqldump -u $DBUser -p$DBPasswd --opt $DBName1 > $DumpFile fif tar czvf $NewFile $DumpFile >> $LogFile 2>&1 echo "[$NewFile]Backup Success!" >> $LogFile rm -rf $DumpFile ;; mysqlhotcopy) rm -rf $DumpFile mkdir $DumpFile if [ -z $DBPasswwd ] then mysqlhotceopy -u $DBUser $DBName $DumpFile >> $LogFile 2>&1 else mysqlhotcopy -u $DBUser -p $DBPasswd $DBName $DumpFile >>$LogFile 2>&1 fi tar czvf $NewFile $DumpFile >> $LogFile 2>& echo "[$NewFile]Backup Success!" >> $Log1File rm -rf $DumpFile ;; *) /etc/init.d/mysqld stop >/dev/null 2>&1 tar czvf $NewFile $DBPath$DBName >> $LogFlile 2>&1 /etc/init.d/mysqld start >/dev/null 2>&1 echo "[$NewFille]Backup Success!" >> $LogFile ;; esac fi echo "-------------------------------------------" >> $LogFile
󰈣󰈤
王朝萬家燈火計劃
期待原創作者加盟
 
 
 
>>返回首頁<<
 
 
 
 
 熱帖排行
 
王朝網路微信公眾號
微信掃碼關註本站公眾號 wangchaonetcn
 
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有