#!/bin/bash
#author: 505056357@qq.com
echo "backup job start"
#設(shè)置要備份的網(wǎng)站目錄,即是網(wǎng)站的根目錄
file=/home/wwwroot/default
#設(shè)置本地備份目錄,用于存放打包后的文件
backpath=/home/wwwroot/bkdata/
dbuser=root
#數(shù)據(jù)密碼,要記得用單引號將把整個密碼引起來,如果密碼中有單引號就用雙引號,嘿嘿
dbpwd='123456'
dbname=car
domain=192.168.0.187
user=ftpuser
userpwd='123456'
bkdate=$(date +%Y%m%d)
sqlfile_sql=${dbname}_${bkdate}.sql
sqlfile_gz=${dbname}_${bkdate}.tar.gz
sqlfp=${backpath}${bkdate}/$sqlfile_sql
if [ -e $backpath${bkdate} ]
then
echo $backpath is exists.
else
mkdir -p $backpath${bkdate}
fi
#執(zhí)行數(shù)據(jù)庫備份,/apps/mysql/bin/mysqldump修改為你實際環(huán)境的路徑
/apps/mysql/bin/mysqldump -u $dbuser -p$dbpwd $dbname > ${sqlfp}
#對備份之后的數(shù)據(jù)庫文件壓縮,或者在數(shù)據(jù)庫備份時通過加入?yún)?shù)直接進(jìn)行壓縮
tar zcf $backpath${bkdate}/$sqlfile_gz -C ${backpath}${bkdate} ${sqlfile_sql}
#刪除未壓縮SQL的文件
rm -f ${sqlfp}
#生成的文件名格式:web_網(wǎng)站目錄名_20130419.tar.gz
fn=web_${file##*/}_${bkdate}.tar.gz
tar zcf $backpath${bkdate}/$fn -C ${file%/*} ${file##*/}
#以下操作為FTP自動連接并把本地文件上傳到異地服務(wù)器上
/usr/bin/lftp ${domain} END
user ${user} ${userpwd}
lcd ${backpath}
mirror -R ${bkdate}
exit
END
echo "backup job done"