首页 > 未分类 > mysql自动备份(sh脚本)

mysql自动备份(sh脚本)

Aug 4th,2009 发表评论

唉~~服务器不稳定,改个sh脚本备份data保险下
自动备份上传到指定FTP
可以将下面这个脚本放进crontab,每天凌晨执行一次,自动上传数据和日志到FTP备份,我这边是每天3点
0 3 * * * /opt/mysql/backup.sh >> /tmp/backup.txt
备份路径可自己创建,可以不管它。
在下面这条语句中
/opt/mysql/bin/mysqldump -u$DBUser -p$DBPasswd –opt $DBName > $DumpFile
mysqldump要写它的绝对路径,crontab不认相对路径。

#!/bin/sh
#Powered by tiham.com
#2004-09

#Setting
DBName=tiham
DBUser=tiham.com


DBPasswd=tiham.com
BackupPath=/root/db/
LogFile=/root/db.log
DBPath=/opt/mysql/data
#BackupMethod=mysqldump
BackupMethod=mysqlhotcopy
Ftpserver=xx.xx.xx.xx
Ftpuser=tiham
Ftppass=tiham.com

#BackupMethod=tar
#Setting End
BackupMethod=mysqldump
#BackupMethod=mysqlhotcopy

NewFile=”$BackupPath”db$(date +%y%m%d).tgz
DumpFile=”$BackupPath”db$(date +%y%m%d)
OldFile=”$BackuipPath”db$(date +%y%m%d –date=’5 days ago’).tgz
Putfile=db$(date +%y%m%d).tgz

echo “——————————————-” >> $LogFile
echo $(date +”%y-%m-%d %H:%M:%S”) >> $LogFile
echo “————————–” >> $LogFile
#create invironment
if [ ! -d $BackupPath ]
then
mkdir $BackupPath
echo “create file…”
fi

#Delete Old File
if [ -f $OldFile ]
then
rm -f $OldFile >> $LogFile
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
/opt/mysql/bin/mysqldump -u$DBUser -p$DBPasswd –opt $DBName > $DumpFile
fi
tar czvfP $NewFile $DumpFile
echo “[$NewFile]Backup Success!” >> $LogFile
rm -rf $DumpFile
;;

esac
fi

cp $LogFile $BackupPath
cd $BackupPath
F=”my.ftp”
echo “open $Ftpserver” >> $F
echo “user $Ftpuser $Ftppass” >> $F
echo “bin” >> $F
echo “cd others” >> $F
echo “mput $Putfile” >> $F
echo “mput db.log” >> $F
echo “bye” >> $F
ftp -i -in < $F
rm -rf $F
echo “Upfile $Putfile success!” >> $LogFile

声明: 本文采用 BY-NC-SA 协议进行授权. 转载请注明转自: mysql自动备份(sh脚本)
  1. 本文目前尚无任何评论.
  1. 本文目前尚无任何 trackbacks 和 pingbacks.