您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“l(fā)inux下怎么自動(dòng)備份MySQL數(shù)據(jù)并上傳到FTP上”,感興趣的朋友不妨來(lái)看看。本文介紹的方法操作簡(jiǎn)單快捷,實(shí)用性強(qiáng)。下面就讓小編來(lái)帶大家學(xué)習(xí)“l(fā)inux下怎么自動(dòng)備份MySQL數(shù)據(jù)并上傳到FTP上”吧!
代碼如下:
#!/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 script
到此,相信大家對(duì)“l(fā)inux下怎么自動(dòng)備份MySQL數(shù)據(jù)并上傳到FTP上”有了更深的了解,不妨來(lái)實(shí)際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢(xún),關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。