您好,登錄后才能下訂單哦!
在網上看了幾個腳本根據網上的稍微修改了下,稍微更準確了點,就是缺少記錄日志的功能,還沒想好怎么寫,后續添加
#!/bin/bash
#做大備份數量
Backup_Max_Files=20
#數據庫備份目錄
WORKING_DIR=$(dirname "$0")
export WORKING_DIR=$(cd -P "$WORKING_DIR"/ > /dev/null; pwd)
#數據庫IP地址、帳號、密碼
DB_Host="XXXXXXX"
DB_User="XXXXXX"
DB_Password="XXXXXXX"
#需要備份的數據庫
Databases=(
"mysql"
"zabbix"
)
#==========
echo "###########Bash Shell MySql Databases Backup Tools!"
#定義變量=當前日期
Date_Time=$(date +%F)
#定義備份文件前綴
BackDataFile=zhang"${Date_Time}"
cd $WORKING_DIR
mkdir $BackData_File
#統計需要備份數據庫的個數
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工具進行邏輯備份(全備)并使用郵件腳本發送成功失敗提醒
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}數據庫備份成功" "${Databases}>數據庫備份成功"
else
echo " Error NO!: Failure of MySQL database backup!"
#python /root/jiaoben/mail.py "y@163.com" "${Databases}數據庫備份失敗" "${Databases}>數據庫備份失敗請查看"
fi
echo -n "Backup success: "
echo $(date +"%Y-%m-%d %H:%M:%S")
done
echo
echo "[+] Packaging and compressing folders with Tar ...."
#使用tar命令打包并壓縮生成的數據庫文件
cd $WORKING_DIR
tar -cv ${BackData_File} | bzip2 > ${BackData_File}.tar.bz2 && rm -rf ${BackData_File}
#刪除n天以前備份的數據文件
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 {} \;
后補:郵件報警腳本(網絡版,已測試可以使用沒有問題)
#!/usr/bin/python
coding=utf-8
#測試方法:python 5.py XXXXX@163.com test test
#能正常發送郵件說明成功
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())
#發送郵件
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)
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。