在網(wǎng)上看了幾個(gè)腳本根據(jù)網(wǎng)上的稍微修改了下,稍微更準(zhǔn)確了點(diǎn),就是缺少記錄日志的功能,還沒想好怎么寫,后續(xù)添加
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:申請(qǐng)域名、虛擬主機(jī)、營(yíng)銷軟件、網(wǎng)站建設(shè)、廣平網(wǎng)站維護(hù)、網(wǎng)站推廣。
#!/bin/bash
#做大備份數(shù)量
Backup_Max_Files=20
#數(shù)據(jù)庫(kù)備份目錄
WORKING_DIR=$(dirname "$0")
export WORKING_DIR=$(cd -P "$WORKING_DIR"/ > /dev/null; pwd)
#數(shù)據(jù)庫(kù)IP地址、帳號(hào)、密碼
DB_Host="XXXXXXX"
DB_User="XXXXXX"
DB_Password="XXXXXXX"
#需要備份的數(shù)據(jù)庫(kù)
Databases=(
"MySQL"
"zabbix"
)
#==========
echo "###########Bash Shell MySql Databases Backup Tools!"
#定義變量=當(dāng)前日期
Date_Time=$(date +%F)
#定義備份文件前綴
BackDataFile=zhang"${Date_Time}"
cd $WORKING_DIR
mkdir $BackData_File
#統(tǒng)計(jì)需要備份數(shù)據(jù)庫(kù)的個(gè)數(shù)
count=0
while [ "x${Databases[count]}" != "x" ]
do
count=$((count + 1))
echo "Databases=$count"
done
echo "[+] ${count} The database is about to be backed up"
for Databases in ${Databases[@]}
do
echo "[+] MySql-backing up: ${Databases}"
echo -n " MySql Backup Start: "
echo $(date +"%Y-%m-%d %H:%M:%S")
#使用Mysqdump工具進(jìn)行邏輯備份(全備)并使用郵件腳本發(fā)送成功失敗提醒
if $(mysqldump -h${DB_Host} -u${DB_User} -p${DB_Password} --single-transaction --flush-logs mysql > ${BackData_File}/${Databases}.sql )
then
echo " Success OK!: Mysql database backup success!"
python /root/jiaoben/mail.py "y@163.com" "${Databases}數(shù)據(jù)庫(kù)備份成功" "${Databases}>數(shù)據(jù)庫(kù)備份成功"
else
echo " Error NO!: Failure of MySQL database backup!"
#python /root/jiaoben/mail.py "y@163.com" "${Databases}數(shù)據(jù)庫(kù)備份失敗" "${Databases}>數(shù)據(jù)庫(kù)備份失敗請(qǐng)查看"
fi
echo -n "Backup success: "
echo $(date +"%Y-%m-%d %H:%M:%S")
done
echo
echo "[+] Packaging and compressing folders with Tar ...."
#使用tar命令打包并壓縮生成的數(shù)據(jù)庫(kù)文件
cd $WORKING_DIR
tar -cv ${BackData_File} | bzip2 > ${BackData_File}.tar.bz2 && rm -rf ${BackData_File}
#刪除n天以前備份的數(shù)據(jù)文件
echo "[+] Delete over time database backup files ...."
find $WORKINGDIR -name "zhang*.tar.bz2" -mtime +${Backup_Max_Files} -exec rm -rf {} \;
#find $WORKINGDIR -name "zhang*.tar.bz2" -mtime +${Backup_Max_Files} -exec ls {} \;
后補(bǔ):郵件報(bào)警腳本(網(wǎng)絡(luò)版,已測(cè)試可以使用沒有問題)
#!/usr/bin/python
coding=utf-8
#測(cè)試方法:python 5.py XXXXX@163.com test test
#能正常發(fā)送郵件說明成功
import smtplib
import sys
from email.mime.text import MIMEText
class send_mail():mail_host="smtp.163.com"
mail_user="XXXXXX@163.com"
br/>mail_host="smtp.163.com"
mail_user="XXXXXX@163.com"
mail_pass="XXXXXx"
mail_postfix="163.com"
def init(self,sub,content):self.me="Zabbix-Alter"+"<"+self.mail_user+"@"+self.mail_postfix+">"
br/>self.me="Zabbix-Alter"+"<"+self.mail_user+"@"+self.mail_postfix+">"
self.msg['From'] = self.me
self.msg['Subject'] = sub
def sendMessage(self,user):
self.msg['To'] = ";".join(user)
try:
s = smtplib.SMTP_SSL(host=self.mail_host,port=465)
s.login(self.mail_user,self.mail_pass)
s.sendmail(self.me, user, self.msg.as_string())
#發(fā)送郵件
s.close()
except Exception, e:
print str(e)
if name== 'main':
mailto_list=[]
mailto_list.append(sys.argv[1])
s = send_mail(sys.argv[2],sys.argv[3])
s.sendMessage(mailto_list)