自動備份MySQL數(shù)據(jù)并上傳到FTP上shell腳本
Linux自動備份MySQL數(shù)據(jù)庫并上傳到FTP服務(wù)器上shell腳本
#!/bin/bash
#Mysql autobackup shell
#
#
#----------------set the mysql login parameters
dbuser=root
dbpasswd=
dbserver=localhost
dbname=fwserver2008
dbopt=--opt
backupdir=/dbbackup/
#-----------------set the FTP paramters , 0 no send to a ftp server,1 send to a ftp server
copytoftp=1
ftpserver=172.16.25.2
ftpuser=linux
ftppasswd=123456
#----------------set the backpfile paramters
fileprefix=fwserver
dumpfilename=$backupdir$fileprefix`date +%F_%H%M%S`.sql
newfile=$fileprefix-`date +%F_%H%M%S`.tar.gz
keepdays=10
#------------------write the operater command to log file
logfile=/var/log/_mysqlbackup.log
logtmp=/var/log/_mybackup.tmp
#===============================================
if [ ! -d $backupdir ]
then
echo "$backupdir is not exist, then make ..." >> $logfile
mkdir -p $backupdir
fi
echo "start====================================>">>$logfile
echo "Beginning backup `date '+%F %T'`" >>$logfile
echo "Delete $keepdays days ago files ..." >>$logfile
find $backupdir -name $fileprefix* -mtime +$keepdays -fls $logtmp -exec rm {} \;
echo "Deleted Backup file is :">>$logfile
cat $logtmp >>$logfile
echo "Delete old file Success!" >>$logfile
if [ -f $backupdir$newfile ]
then
echo "$newfile backup exist, backup stop ..." >>$logfile
else
if [ -z $dbpasswd ]
then
mysqldump -u$dbuser -h$dbserver $dbopt $dbname >$dumpfilename
else
mysqldump -u$dbuser -p$dbpasswd -h$dbserver $dbopt $dbname >$dumpfilename
fi
tar czvf $backupdir$newfile $dumpfilename >>$logfile 2>&1
echo "$backupdir$newfile Backup Success!" >>$logfile
rm -fr $dumpfilename
if [ $copytoftp = 1 ]; then
if [ -z $ftpserver ];then
echo "Ftp Server not set,Copy to Ftp Failed ..." >>$logfile
exit 1
elif [ -z $ftpuser ];then
echo "Ftp user not set, Copy to Ftp Failed ..." >>$logfile
exit 2
elif [ -z $ftppasswd ]; then
echo "Ftp password not set, Copy to Ftp Failed ..." >>$logfile
exit 3
else
echo "Start copy to Ftp server ...." >> $logfile
ftp -i -n <<end_ftp
open $ftpserver
user $ftpuser $ftppasswd
lcd $backupdir
hash
prompt
put $newfile
close
bye
end_ftp
fi
fi
echo "End=======================================">>$logfile
fi
# the end of the auto backup
關(guān)鍵字:MySQL、數(shù)據(jù)、服務(wù)器
新文章:
- CentOS7下圖形配置網(wǎng)絡(luò)的方法
- CentOS 7如何添加刪除用戶
- 如何解決centos7雙系統(tǒng)后丟失windows啟動項
- CentOS單網(wǎng)卡如何批量添加不同IP段
- CentOS下iconv命令的介紹
- Centos7 SSH密鑰登陸及密碼密鑰雙重驗證詳解
- CentOS 7.1添加刪除用戶的方法
- CentOS查找/掃描局域網(wǎng)打印機IP講解
- CentOS7使用hostapd實現(xiàn)無AP模式的詳解
- su命令不能切換root的解決方法
- 解決VMware下CentOS7網(wǎng)絡(luò)重啟出錯
- 解決Centos7雙系統(tǒng)后丟失windows啟動項
- CentOS下如何避免文件覆蓋
- CentOS7和CentOS6系統(tǒng)有什么不同呢
- Centos 6.6默認(rèn)iptable規(guī)則詳解