#vi backup.sh
#!/bin/bash
##################################################
# 紀錄檔所在
LogFile="/backups/log/`date +"%Y-%m"`.log"
# 壓縮檔案名稱
file="backupfilename"
7zpassword="mypassword"
# 備份來源目錄
SourceDir="/etc/SourceDir"
# 備份目的目錄
BakDir="/backups"
# FTP IP位址
host="123.456.789.123"
# FTP 帳號和密碼
user="ftpusername"
pass="ftpuserpassword"
# FTP 備份目錄
dir="./"
# 備份保留天數
RetainDay=20
# Email To ?
email="xxxxx@xxxxx.edu.tw"
##################################################
DATE=`date +"%Y-%m-%d"`
echo "backup start at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
echo "--------------------------------------------------" >>$LogFile
cd $BakDir
PackFile=$file.$DATE.tar.gz
tar zcvf /backups/$PackFile $SourceDir
# 備份來源目錄
SourceDir="/etc/SourceDir"
# 備份目的目錄
BakDir="/backups"
# FTP IP位址
host="123.456.789.123"
# FTP 帳號和密碼
user="ftpusername"
pass="ftpuserpassword"
# FTP 備份目錄
dir="./"
# 備份保留天數
RetainDay=20
# Email To ?
email="xxxxx@xxxxx.edu.tw"
##################################################
DATE=`date +"%Y-%m-%d"`
echo "backup start at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
echo "--------------------------------------------------" >>$LogFile
cd $BakDir
PackFile=$file.$DATE.tar.gz
tar zcvf /backups/$PackFile $SourceDir
#7zip 壓縮,需另外安裝。
echo "Backup files have been uploaded" >>$LogFile
#Centos
find -type f -mtime +$RetainDay -name "*.tar.gz" -exec rm {} \; >/dev/null
#freebsd
#find $BakDir -mtime +$RetainDay -name "*.tar.gz" -exec rm {} \; >/dev/null
echo "Delete expired backup files" >>$LogFile
# email subject
SUBJECT="$PackFile Backup is done."
# Email text/message
EMAILMESSAGE=$BakDir"/emailmessage.txt"
echo "$PackFile Backup is done."> $EMAILMESSAGE
echo "you can check FTP server $hotst.">>$EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
echo "Send e-mail notification" >>$LogFile
echo "--------------------------------------------------" >>$LogFile
echo "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
echo " " >> $LogFile
#chmod a+x backup.sh
#crontab -e
7za a -t7z -mhe=on -P$7zpassword /backups/$PackFile $SourceDir
echo "backup $SourceDir done into $PackFile" >>$LogFile
echo "backup $SourceDir done into $PackFile" >>$LogFile
#FTP 檔案傳輸
/usr/bin/ftp -in $host << EOF
/usr/bin/ftp -in $host << EOF
user $user $pass
cd $dir
bin
put $PackFile
quit
EOF
cd $dir
bin
put $PackFile
quit
EOF
#FTPS 加密檔案傳輸。
#/usr/bin/lftp -u $user,$pass ftps://$host << EOF
#cd $dir
#put $PackFile
#quit
#EOF
#Centos
find -type f -mtime +$RetainDay -name "*.tar.gz" -exec rm {} \; >/dev/null
#freebsd
#find $BakDir -mtime +$RetainDay -name "*.tar.gz" -exec rm {} \; >/dev/null
echo "Delete expired backup files" >>$LogFile
# email subject
SUBJECT="$PackFile Backup is done."
# Email text/message
EMAILMESSAGE=$BakDir"/emailmessage.txt"
echo "$PackFile Backup is done."> $EMAILMESSAGE
echo "you can check FTP server $hotst.">>$EMAILMESSAGE
mail -s "$SUBJECT" "$EMAIL" < $EMAILMESSAGE
echo "Send e-mail notification" >>$LogFile
echo "--------------------------------------------------" >>$LogFile
echo "backup end at $(date +"%Y-%m-%d %H:%M:%S")" >>$LogFile
echo " " >> $LogFile
#chmod a+x backup.sh
#crontab -e
05 1 * * * ./backups/backup.sh
沒有留言:
張貼留言