linux使用Msmtp mutt shell自动发邮件(备份mysql)
linux使用Msmtp mutt shell发邮件(备份mysql)
因为数据库有插件自动备份到邮箱,本例示范web数据备份。
需要用到mutt 和 msmtp 这两款软件
1.编译安装软件
- tar jxvf msmtp-1.4.16.tar.bz2
- ./configure --prefix=/usr/local/msmtp &&make &&make install
- tar -xzvf mutt-1.5.11.tar.gz
- ./configure --prefix=/usr/local/mutt &&make &&make install
2.查看msmtp配置文件路径
- [root@vi bin]# ./msmtp --ver
- msmtp version 1.4.16
- TLS/SSL library: OpenSSL
- Authentication library: GNU SASL
- Supported authentication methods:
- plain cram-md5 digest-md5 gssapi external login
- IDN support: enabled
- NLS: enabled, LOCALEDIR is /usr/local/msmtp/share/locale
- System configuration file name: /usr/local/msmtp/etc/msmtprc
- User configuration file name: /root/.msmtprc
- Copyright (C) 2008 Martin Lambers and others.
- This is free software. You may redistribute copies of it under the terms of
- the GNU General Public License .
- There is NO WARRANTY, to the extent permitted by law.
3.配置msmtp的配置文件
- [root@vi ~]# vi .msmtprc
- defaults
- #-------该文件要存在,不然没有日志,需要手工建立
- logfile /usr/local/msmtp/msmtp.log
- account vi
- host smtp.yahoo.cn
- from viyin@yahoo.cn
- auth plain
- user viyin@yahoo.cn
- password viyin.net
- account default : viyin
- [root@vi ~]#
测试一下
- /usr/local/msmtp/bin/msmtp viyin@yahoo.cn
- test,viyin
- crtl +d 退出
tail -f /usr/local/msmtp/msmtp.log 查看日志
然后再进入到上面的邮件中,看看信收到没有
4.配置mutt
- [root@vi viyin]# cat ~/.muttrc
- set sendmail="/usr/local/msmtp/bin/msmtp"
- set use_from=yes
- set realname="vi"
- set from=viyin@yahoo.cn
- set envelope_from=yes
5.测试发信
- [root@vi viyin]# echo "viyin.net test" | mutt -s "messages" 7676002@qq.com
6.备份脚本
- #!/bin/bash
- cd /www/web/
- tar -zvcf backup/web_viyinnet_`date +"%y-%m-%d-%H"`.tar.gz viyinnet
- echo "web backup" | mutt -s "web_viyinnet_`date +"%y-%m-%d"`" -a /www/web/backup
- /web_viyinnet_`date +"%y-%m-%d-%H"`.tar.gz vi@viyin.net
7. 加入crontab自动执行
- 30 6 * * * /root/webbackup.sh
错误提示:
msmtp: /root/.msmtprc: must have no more than user read/write permissions
解决办法:更改权限 chmod 600 .msmtprc
一定只能是600权限