1.安裝MUTT郵件用戶端
2.建立指令碼
# vi /etc/rc.d/mail_to_bak_daily.sh
版本一:
#!/bin/bash# =========================================================# 請輸入,你想讓備份資料放置到哪個獨立的目錄區basedir=/backup/daily# =========================================================PATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin;export PATHexport LANG=C# 定義資料庫資訊user=rootpasswd=123456# 定義mysql備份路徑mysql_db_bak=$basedir/mysql_db_bak/$(date +%F_%H%M)[ ! -d "$mysql_db_bak" ] && mkdir -p $mysql_db_bak# 定義www備份路徑www_src_bak=$basedir/www_src_bak/$(date +%F_%H%M)[ ! -d "$www_src_bak" ] && mkdir -p $www_src_bak# 定義郵件附件路徑mail_attach_mysql=$mysql_db_bak/wordpress.sqlmail_attach_www=$www_src_bak/www.tar.bz2# 定義郵件標題mail_subject="www.example.com_daily_backup"# 註:這裡的標題之間的字元不能有空格,如有空格會按照郵件名發出,例如daily@www.example.com,backup@www.example.com 本例以底線代替# 定義郵件內容mail_content=/tmp/mail_content.txtmail_code="send MySQL_BAK and WWW_SRC to you at $(date +%F_%H%M)"# 定義郵件地址mail_address="test@example.com"# 1. 備份MySQL (資料庫目錄在/usr/local/mysql)mysqldump -u $user -p$passwd --opt --skip-lock-tables --flush-logs --database wordpress > $mysql_db_bak/wordpress.sql# 2.備份www原始碼:cd /usr/localtar -jpcf $www_src_bak/www.tar.bz2 www# 3.郵件內文cd /tmpecho $mail_code > $mail_content# 4.發送附件yum install mutt -ycat $mail_content |mutt -s $mail_subject -a $mail_attach_mysql -a $mail_attach_www -- $mail_address# 5.清除臨時檔案cd /tmp rm -rf $mail_content
版本二:推薦
註:此版本將附件分開發送,對郵箱同時發送多個大附件有收不到的幾率的情況,可按照如下版本重新設定
#!/bin/bashPATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin;export PATHexport LANG=C# 系統檔案備份路徑basedir=/backup/daily# 資料庫資訊user=rootpasswd=123456# 定義mysql備份路徑mysql_db_bak=$basedir/mysql_db_bak/$(date +%F_%H%M)if [ ! -d "$mysql_db_bak" ]; then mkdir -p $mysql_db_bakfi# 定義www備份路徑www_src_bak=$basedir/www_src_bak/$(date +%F_%H%M)if [ ! -d "$www_src_bak" ]; then mkdir -p $www_src_bakfi# 備份檔案mysql_bak=$mysql_db_bak/wordpress.sql.gzwww_bak=$www_src_bak/www.tar.bz2# 定義備份檔案作為附件的數組all_bak=($mysql_bak $www_bak)# 郵件標題mail_subject="www.showerlee.com_daily_backup"# 郵件內容mail_content_mysql="send MySQL_BAK at $(date +%F_%H%M)"mail_content_www="send WWW_SRC at $(date +%F_%H%M)"# 定義內容數組mail_content=("$mail_content_mysql" "$mail_content_www")# 郵件地址mail_address="test@example.com"# 1. 備份MySQL (資料庫目錄在/usr/local/mysql)mysqldump -u $user -p$passwd --opt --skip-lock-tables --flush-logs --database wordpress | gzip > $mysql_bak# 2.備份www原始碼:cd /usr/local tar -jpcf $www_bak www# 3.發送附件yum install mutt -yfor((i=0; i!=${#all_bak[@]}; ++i)){ echo ${mail_content[i]} | mutt -s $mail_subject $mail_address -a ${all_bak[i]}}# 4.清理mutt臨時檔案cat /dev/null > ~/sent
版本三:
實現rar分割並附件發送
推薦附件超過50M的使用者使用
#/bin/bashPATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin;export PATHexport LANG=C# 系統檔案備份路徑basedir=/data/backup/daily# 資料庫資訊user=rootpasswd=123456# 安裝rarrar=$(rar | grep RAR | awk '{print $1}'| sed -n '1p')if [ $? != 0 ];then rar=nullfisys_version=$(uname -m)rar_path="/usr/local/rar"if [ $rar != 'RAR' ];then if [ $sys_version == 'x86_64' ];then wget http://www.rarlab.com/rar/rarlinux-x64-4.2.0.tar.gz tar zxvf rarlinux-x64-4.2.0.tar.gz mv rar $rar_path cd $rar_path make else wget http://www.rarlab.com/rar/rarlinux-4.2.0.tar.gz tar zxvf rarlinux-4.2.0.tar.gz mv rar $rar_path cd $rar_path make fifi# 定義mysql備份路徑mysql_db_bak=$basedir/mysql_db_bak/$(date +%F_%H%M)if [ ! -d "$mysql_db_bak" ];then mkdir -p $mysql_db_bakfi# 定義www備份路徑www_src_bak=$basedir/www_src_bak/$(date +%F_%H%M)if [ ! -d "$www_src_bak" ];then mkdir -p $www_src_bakfi# 備份檔案mysql_bak=$mysql_db_bak/test.sql.gzwww_bak=$www_src_bak/www.rarwww_bak_part1=$www_src_bak/www.part1.rarwww_bak_part2=$www_src_bak/www.part2.rarwww_bak_part3=$www_src_bak/www.part3.rar# 定義附件數組all_bak=($mysql_bak $www_bak_part1 $www_bak_part2 $www_bak_part3)# 郵件標題mail_subject="showerlee.com_and_yanwenbo.cn_daily_backup"# 郵件內容mail_content_mysql="send MySQL_BAK at $(date +%F_%H%M)"mail_content_www_p1="send WWW_SRC rarball part1 at $(date +%F_%H%M)"mail_content_www_p2="send WWW_SRC rarball part2 at $(date +%F_%H%M)"mail_content_www_p3="send WWW_SRC rarball part3 at $(date +%F_%H%M)"# 定義內容數組mail_content=("$mail_content_mysql" "$mail_content_www_p1" "$mail_content_www_p2" "$mail_content_www_p3")# 郵件地址mail_address1="showerlee@vip.qq.com"mail_address2="kevinywb@vip.qq.com"# 1. 備份MySQLmysqldump -u $user -p$passwd --opt --skip-lock-tables --flush-logs --database test | gzip > $mysql_bak# 2.備份www原始碼(分割若干個45m的tar包)if [ -z $rar ];then exit 0ficd /datarar a -v45000k $www_bak www# 3.發送附件for((i=0; i!=${#all_bak[@]}; ++i)){if [ -f ${all_bak[i]} ]; then echo ${mail_content[i]} | mutt -s $mail_subject $mail_address1 $mail_address2 -a ${all_bak[i]}else echo ${mail_content[i]} | mutt -s $mail_subject $mail_address1 $mail_address2 -a $www_bak breakfi}# 4.清理mutt臨時檔案cat /dev/null > ~/sent# 5.清理1個月前的備份檔案dir=/data/backup/daily/find $dir -mtime +30 -type d -name "201*" -print |xargs -i rm -rf {} 2>/dev/null# 6.釋放系統緩衝echo 1 > /proc/sys/vm/drop_caches
注:如果報如下錯誤
複製代碼 代碼如下:
postdrop: warning: uid=0: File too largesendmail: fatal: root(0): message file too bigError sending message, child exited 75 (Deferred.).Could not send the message.
是由於超過系統預設發送附件大小,修改成如下大小即可(2G)
# postconf -e "message_size_limit = 2048000000"# postconf -e "mailbox_size_limit = 2048000000"
3.執行指令碼
# sh /etc/rc.d/mail_to_bak_daily.sh
註:可利用 tail -f /var/log/maillog 查看發送日誌
4.測試結果如圖:
註:附件較大請耐心等待