系统环境: CentOS6.3

MUTT:     YUM默认源安装


1.安装MUTT邮件客户端

# yum install mutt -y


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 PATH

export LANG=C


# 定义数据库信息

user=root

passwd=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.sql

mail_attach_www=$www_src_bak/www.tar.bz2


# 定义邮件标题

mail_subject="www.example.com_daily_backup"

# 注:这里的标题之间的字符不能有空格,如有空格会按照邮件名发出,例如[email protected],[email protected] 本例以下划线代替


# 定义邮件内容

mail_content=/tmp/mail_content.txt

mail_code="send MySQL_BAK and WWW_SRC to you at $(date +%F_%H%M)"


# 定义邮件地址

mail_address="[email protected]"


# 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/local

tar -jpcf $www_src_bak/www.tar.bz2 www


# 3.邮件正文

cd /tmp

echo $mail_code > $mail_content



# 4.发送附件

yum install mutt -y

cat $mail_content |mutt -s $mail_subject -a $mail_attach_mysql -a $mail_attach_www -- $mail_address


# 5.清除临时文件

cd /tmp

 rm -rf $mail_content

---------------------------------------------------------


(版本二) 推荐

注:此版本将附件分开发送,对邮箱同时发送多个大附件有收不到的几率的情况,可按照如下版本重新配置

--------------------------------------------------------

#!/bin/bash


PATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin;

export PATH

export LANG=C


# 系统文件备份路径

basedir=/backup/daily


# 数据库信息

user=root

passwd=123456


# 定义mysql备份路径

mysql_db_bak=$basedir/mysql_db_bak/$(date +%F_%H%M)

if [ ! -d "$mysql_db_bak" ];

 then

    mkdir -p $mysql_db_bak

fi


# 定义www备份路径

www_src_bak=$basedir/www_src_bak/$(date +%F_%H%M)

if [ ! -d "$www_src_bak" ];

 then

    mkdir -p $www_src_bak

fi


# 备份文件

mysql_bak=$mysql_db_bak/wordpress.sql.gz

www_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="[email protected]"


# 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 -y

for((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/bash


PATH=/bin:/usr/bin:/sbin:/usr/sbin::/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin;

export PATH

export LANG=C


# 系统文件备份路径

basedir=/data/backup/daily


# 数据库信息

user=root

passwd=123456


# 安装rar

rar=$(rar | grep RAR | awk '{print $1}'| sed -n '1p')

if [ $? != 0 ];then

  rar=null

fi

sys_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

    fi

fi


# 定义mysql备份路径

mysql_db_bak=$basedir/mysql_db_bak/$(date +%F_%H%M)


if [ ! -d "$mysql_db_bak" ];

then

   mkdir -p $mysql_db_bak

fi



# 定义www备份路径

www_src_bak=$basedir/www_src_bak/$(date +%F_%H%M)


if [ ! -d "$www_src_bak" ];

then

   mkdir -p $www_src_bak

fi


# 备份文件

mysql_bak=$mysql_db_bak/test.sql.gz

www_bak=$www_src_bak/www.rar

www_bak_part1=$www_src_bak/www.part1.rar

www_bak_part2=$www_src_bak/www.part2.rar

www_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="[email protected]"

mail_address2="[email protected]"


# 1. 备份MySQL

mysqldump -u $user -p$passwd --opt --skip-lock-tables  --flush-logs  --database test | gzip > $mysql_bak


# 2.备份www源代码(分割若干个45m的tar包)

if [ -z $rar ];then

      exit 0

fi

cd /data

rar 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

   break

fi

}


# 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 large

sendmail: fatal: root(0): message file too big

Error 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.测试结果如图:

注:附件较大请耐心等待

[SHELL]备份系统文件并邮件发送_第1张图片