本篇内容介绍了“Shell脚本实现备份系统文件并邮件发送功能”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
安装MUTT邮件客户端
# yum install mutt -y
创建脚本
# 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"
执行脚本
# sh /etc/rc.d/mail_to_bak_daily.sh
注:可利用 tail -f /var/log/maillog 查看发送日志
4.测试结果如图:
注:附件较大请耐心等待
“Shell脚本实现备份系统文件并邮件发送功能”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!