這個(gè)腳本是07年在網(wǎng)上查找并經(jīng)過修改用到正式系統(tǒng)下的腳本,數(shù)據(jù)庫名字讓我改為了LinuxIDC.com
1.首先建立sh文件,在/usr/bin/先建立一個(gè)名叫mysqlscript.sh的腳本文件,內(nèi)容參見mysqlscript.sh
2.其次在根目錄下打開 #crontab -e 輸入以下內(nèi)容 12 12 * * * /usr/bin/mysqlscript.sh
-
3.最后,如果發(fā)現(xiàn)不能定時(shí)執(zhí)行腳本,可能的原因是mysqlscript.sh腳本文件的權(quán)限問題,然后更改為可執(zhí)行文件#chmod 777 mysqlscript.sh;
4.上面的工作做完以后就應(yīng)該沒什么問題.
mysqlscript.sh的腳本內(nèi)容如下:
#!/bin/bash
#This is a ShellScript For Auto DB Backup
#Powered by aspbiz
#2007-11-25
#Setting
#設(shè)置數(shù)據(jù)庫名,數(shù)據(jù)庫登錄名,密碼,備份路徑,日志路徑,數(shù)據(jù)文件位置,以及備份方式
#默認(rèn)情況下備份方式是tar,還可以是mysqldump,mysqldotcopy
#默認(rèn)情況下,用root(空)登錄mysql數(shù)據(jù)庫,備份至/root/dbxxxxx.tgz
#數(shù)據(jù)庫名
DBName=LinuxIDC.com
#數(shù)據(jù)庫用戶名
DBUser=root
#密碼
DBPasswd=!qazdr%
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 $DBName > $DumpFile
fi
tar czvf $NewFile $DumpFile >> $LogFile 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
mysqlhotcopy)
rm -rf $DumpFile
mkdir $DumpFile
if [ -z $DBPasswd ]
then
mysqlhotcopy -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>&1
echo "[$NewFile]Backup Success!" >> $LogFile
rm -rf $DumpFile
;;
*)
/etc/init.d/mysqld stop >/dev/null 2>&1
tar czvf $NewFile $DBPath$DBName >> $LogFile 2>&1
/etc/init.d/mysqld start >/dev/null 2>&1
echo "[$NewFile]Backup Success!" >> $LogFile
;;
esac
fi
echo "-------------------------------------------" >> $LogFile 本文出自:億恩科技【mszdt.com】
服務(wù)器租用/服務(wù)器托管中國五強(qiáng)!虛擬主機(jī)域名注冊(cè)頂級(jí)提供商!15年品質(zhì)保障!--億恩科技[ENKJ.COM]
|