实现需求
在Centos系统,使用 Cron 对 MySQL 数据库执行自动备份,通过 Mutt 自动将备份发送到指定邮箱,并使用Gmail的垃圾邮件策略进行常规删除。备份脚本如下:
#// Path /root/eeBakcupShell/backup-email-mysql-data.sh #!/bin/sh dbpwd='***************' d=`date "+%Y-%m-%d"` #current data:Year-Month-day FName=example_$d #backup file name:yiilib.com_currentData FP=/root/dbBackup/ #tmp save path, will do auto delete at the end. mysql -e "show databases;" -uroot -p$dbpwd | grep -Ev "Database|information_schema|mysql|test" | xargs mysqldump -uroot -p$dbpwd --skip-lock-tables --databases | gzip > $FP$FName.sql.gz #here has a little trick to find need backup database name: list all database names and return no need backup name(Database|information_schema|mysql|test),rest will be backup, also add gzip support echo "backup files"|mutt -s "backup tmp $t" Your_account_name@gmail.com -a $FP$FName.sql.gz #use Mutt send mail to Your_account_name@gmail.com # Using Template Mail # mutt -s "title" Your_account_name@gmail.com -e "set content_type=text/html" < /root/mailBody.html -a attachment.zip rm -rf $FP$FName.sql.gz #delete backup gz file exit #end
定期执行如上脚本
# crontab -e # //save result to log will help more 15 03 * * * /root/eeBakcupShell/backup-email-mysql-data.sh > /log/eelog.log
安装 mutt
当前为 Centos 8.2 系统。安装完 mutt 后,一定要检查配置参数里要开启这些组件:./configure –prefix=/usr/local/mutt –enable-pop –enable-smtp –with-ssl –with-sasl
–enable-pop 启用pop
–enable-smtp 启用smtp
–with-ssl –with-sasl 在启用上述协议的情况下,必须使用更安全的加密
不然发送邮件的时候会出现“SMTP authentication requires SASL”或者另外一个跟ssl有关的错误 See
~] dnf install mutt ~] mutt -v Configured with: ../configure --enable-bootstrap --enable-languages=c,c++,fortran,lto --prefix=/usr --mandir=/usr/share/man --infodir=/usr/share/info --with-bugurl=http://bugzilla.redhat.com/bugzilla --enable-shared --enable-threads=posix --enable-checking=release --enable-multilib --with-system-zlib --enable-__cxa_atexit --disable-libunwind-exceptions --enable-gnu-unique-object --enable-linker-build-id --with-gcc-major-version-only --with-linker-hash-style=gnu --enable-plugin --enable-initfini-array --with-isl --disable-libmpx --enable-offload-targets=nvptx-none --without-cuda-driver --enable-gnu-indirect-function --enable-cet --with-tune=generic --with-arch_32=x86-64 --build=x86_64-redhat-linux Thread model: posix gcc version 8.3.1 20190507 (Red Hat 8.3.1-4) (GCC)
基本配置
mutt 安装完毕后,其全局配置文件在:/etc/Muttrc,在这个文件的最后一行,发现:source /etc/Muttrc.local,默认,这个文件是个空文件,那么,接下来,所有的配置直接在这个文件里写入即可
PS:本计划是用 Gmail 邮箱来实施发送接收备份的,无奈国内的网络环境,时常连接不上,最后,改为了使用腾讯邮箱。
set ssl_force_tls = yes set abort_nosubject = no set mail_check = 60 set timeout = 10 set sort = "reverse-date-received" set signature = "~/.mutt/signature" set copy = no
配置Mutt以使用Qmail
vim /etc/Muttrc.local
set from = "Your_account_name@QQ.Com" set realname = "iSuSee" # Imap settings (Receiving server) set imap_user = "Your_account_name@QQ.Com" set imap_pass = "<mutt-app-specific-password>" set folder = "imaps://imap.exmail.qq.com:993" # Smtp settings (Sending server) set smtp_url = "smtps://Your_account_name@QQ.Com@smtp.exmail.qq.com:465/" set smtp_pass = "<mutt-app-specific-password>" # Remote gmail folders set spoolfile = "+INBOX" set move = no set editor = "vim" set sort = "reverse-date-received" set charset = "utf-8"
字段解释
- from 和 realname:分别指发送方的邮箱及发件人的昵称,对应电子邮件中使用的“ From:”标头参数的值和“真实名称”(您的名字和姓氏)。
- imap_user 和 imap_pass:前者,指的是发件方的邮箱,它与 from 字段相同,后者 imap_pass 可以是邮箱登陆密码,也可以是 专用密码(Gmai 称此为:应用专用密码;腾讯称之为:客户端专用密码、授权码等),推荐使用专用密码作为身份鉴别的依据在,而不要填入邮箱登陆密码。注意:腾讯邮箱,如果改了QQ密码,那么,客户端专用密码也要重新生成使用。
- folder:接收邮件服务器(Gmail Tencent)
- smtp_url:发送邮件服务器
- smtp_pass:与 imap_pass 的意思一样(授权码)
- spoolfile:邮箱内部文件夹
应用专用密码
为Mutt生成应用专用密码:如果登陆 Google 开启了双重验证,那么为了能够从 Mutt 访问 Gmail 帐户,需要生成一个 app-specific-password,因为 google 对于不使用 Oauth2 作为身份验证的使用场景都需要使用app-specific-password来作为登陆身份识别。要生成应用专用密码代替普通密码登陆邮箱, Gmail 只需导航至 该地址,腾讯邮箱导致至 该地址 ,然后按照说明生成密码,即用生成的专用码替换如上字段 imap_pass 和 smtp_pass 中的 <mutt-app-specific-password>。gmail 建议 imap_pass 和 smtp_pass 分别使用不同的专用码,腾讯邮箱统一使用一个专用码即可。注意:腾讯邮箱生成安全码需要绑定微信,而绑定微信后,默认此微信也就关注了:腾讯企业邮箱公众号,同时,每收到一封邮件,手机微信端都会收到提醒,若要不提醒,就登陆网页端邮箱:设置==>邮箱绑定==>使用微信接收新邮件提醒,把这个勾取消。假如直接取消关注此公众号,那么,相应的,<mutt-app-specific-password>也失效了。另外,此<mutt-app-specific-password>同时也是使用QQ普通邮箱添加其他邮箱代收发的密码(在QQ普通邮箱添加后,手机QQ邮箱就可以同时收发以及管理Email了)添加路径为:设置==>其它邮箱==>添加其它邮箱账户。服务器(POP):pop.exmail.qq.com,端口:995,具体也可以参照 这里 。
配置Mutt以使用Gmail
为了将Mutt与gmail结合使用,我们要设置的选项如下(我们使用虚拟地址):
set from = "Your_account_name@gmail.com" set realname = "iSuSee" # Imap settings set imap_user = "Your_account_name@gmail.com" set imap_pass = "<mutt-app-specific-password>" set folder = "imaps://imap.gmail.com:993" # Smtp settings set smtp_url = "smtp://Your_account_name@smtp.gmail.com:587" set smtp_pass = "<mutt-app-specific-password>" # Remote gmail folders set spoolfile = "+INBOX" set postponed = "+[Gmail]/Drafts" set record = "+[Gmail]/Sent Mail" set trash = "+[Gmail]/Trash" set move = no # Others set editor = "vim" set charset = "utf-8" # unset ssl_verify_host set timeout = 300 set imap_keepalive = 300 set header_cache =~/.mutt/cache/headers set message_cachedir =~/.mutt/cache/bodies set sort = "reverse-date-received" set envelope_from=yes
保护敏感的帐户信息
此时,应该提出一个问题:将敏感信息(例如为 Mutt 生成的专用密码)放入简单的配置文件中是否危险?当然是这样!这是绝对不应该做的事情!另一方面,每次我们要连接到邮箱时都必须提供密码,这确实非常繁琐。解决此问题的方法是将配置信息存储在一个单独的文件中,将使用gpg对其进行加密;然后,该文件将通过mutt主配置文件从内存中获取并解密。
例如:将刚刚配置在 /etc/Muttrc.local 所有信息拷贝到 ~/.mutt/qmail,并使用 gpg 对其进行加密,并运行以下命令:
gpg --recipient Your_account_name@QQ.Com --encrypt ~/.mutt/qmail
运行后,提示:No public key,那是因为还没创建公共密钥。GPG依靠每人两个加密密钥,每个人都有一个私有密钥和公共密钥。公钥可以解密、使用私钥加密的内容。为了安全地发送文件,需使用私钥和收件人的公钥对其进行加密。要解密文件,同样也需要其私钥和你作为收件人的公钥。因此,必须共享公用密钥。作为发件人,你需要具有收件人的公共密钥才能加密文件,而作为收件人同样需要你的公共密钥才能解密文件。共享公共密钥没有风险,而私钥必须保持私密,所以,私钥必须自己保存好。设置GPG涉及的步骤比使用GPG涉及的步骤更多,但通常只需要设置一次即可。See
生成密钥
不必通过电子邮件使用GPG。加密后的文件收件人使用。但是,需要将电子邮件地址与生成的密钥相关联,因此,在生成密钥过程中须填入自己的电子邮件地址。
如下为生成密钥的命令。该–full-generate-key选项在终端窗口内的交互式会话中生成密钥。还将提示您输入密码。确保记住密码。
~] gpg --full-generate-key gpg (GnuPG) 2.2.20; Copyright (C) 2020 Free Software Foundation, Inc. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Please select what kind of key you want: (1) RSA and RSA (default) (2) DSA and Elgamal (3) DSA (sign only) (4) RSA (sign only) (14) Existing key from card Your selection? 1 # 选择加密类型。除非特殊需求,否则键入1 并按 Enter RSA keys may be between 1024 and 4096 bits long. What keysize do you want? (2048) # 选择加密密钥位长,按 Enter接受默认值 Requested keysize is 2048 bits Please specify how long the key should be valid. 0 = key does not expire <n> = key expires in n days <n>w = key expires in n weeks <n>m = key expires in n months <n>y = key expires in n years Key is valid for? (0) 2y # 指定密钥应持续多长时间,例如2y 两年。密钥将持续24个月,因此需要在两年后进行更新。 Key expires at Thu 23 Jan 2023 08:23:16 PM CST Is this correct? (y/N) y # 用确认选择 Y GnuPG needs to construct a user ID to identify your key. Real name: iSuSee # 你的姓 Email address: Your_account_name@QQ.Com # 你的邮箱 Comment: Sample description You selected this USER-ID: "iSuSee (Sample description) <Your_account_name@QQ.Com>" Change (N)ame, (C)omment, (E)mail or (O)kay/(Q)uit? O # 输入 O 确认后,会弹出一个密码框,输入密码并谨记此密码
生成吊销证书
如果他人知道了你的私钥,则需要将旧密钥与你的身份解除关联,以便可以生成新的私钥。为此,需要吊销证书。我们现在将执行此操作并将其存储在安全的地方。
该–output选项后必须跟要创建的证书的文件名。该–gen-revoke选项导致gpg生成吊销证书。您必须提供生成密钥时使用的电子邮件地址。See
至此,密钥创建完成,查看密钥:gpg –list-keys,再次使用对qmail加密
~] gpg --recipient Your_account_name@QQ.Com --encrypt ~/.mutt/qmail gpg: checking the trustdb gpg: marginals needed: 3 completes needed: 1 trust model: pgp gpg: depth: 0 valid: 1 signed: 0 trust: 0-, 0q, 0n, 0m, 0f, 1u gpg: next trustdb check due at 2023-01-23
上面的命令将生成一个新的加密文件~/.mutt/qmail.gpg。现在,可以清空:/etc/Muttrc.local 刚刚配置的内容。要从Mutt主配置中获取加密文件,接下来要要做的就是在:/etc/Muttrc 末尾,注释掉:source /etc/Muttrc.local,改为:source “gpg2 -dq ~/.mutt/qmail.gpg |”,即如:vim /etc/Muttrc
# source /etc/Muttrc.local source "gpg2 -dq ~/.mutt/qmail.gpg |"
测试发邮件
整个过程终于配置完毕,现在输入:mutt 会进入收件箱,发个测试邮箱试试
~] gpg2 -dq ~/.mutt/qmail.gpg # 首先,执行一次解密 echo "This is the content of the email"|mutt -s "The mail title" Your_account_name@QQ.Com # 纯文本邮件 echo "This is the content of the email"|mutt -s "The mail title" Your_account_name@QQ.Com -a bak.sql.gz # 带附件的邮件
See:https://linuxconfig.org/how-to-install-configure-and-use-mutt-with-a-gmail-account-on-linux
https://www.garron.me/en/go2linux/send-mail-gmail-mutt.html
https://nixvsevil.com/posts/mutt-gmail-oauth-gnupg/
http://yiilib.com/en/topic/679/use+MUTT+send+email+from+Centos
https://qiita.com/cd01/items/c32f0fbe90639276517a
https://forums.linuxmint.com/viewtopic.php?t=202471
https://medium.com/@itsjefftong/mutt-gmail-59447a4bffef
https://forums.centos.org/viewtopic.php?t=16724
https://www.thegeekdiary.com/how-to-install-and-configure-mutt-in-centos-rhel/
https://www.fosstechnix.com/install-and-configure-mutt-on-ubuntu-centos/
https://kompjuteras.com/en/how-to-install-mutt-from-source-in-centos-7-and-configure-smtp-options-for-gmail-yandex-or-custom-smtp-server/
https://blog.spokaneits.com/2015/12/05/mutt/
https://www.itread01.com/content/1545905371.html
bak
http://yiilib.com/en/topic/681/Centos+Regular+Auto-backup+MySQL+Database+and+send+to+email+address
https://blog.51cto.com/59090939/2342669
key
https://www.howtogeek.com/427982/how-to-encrypt-and-decrypt-files-with-gpg-on-linux/
https://linux.101hacks.com/unix/gpg-command-examples/
mailBody
https://www.codexworld.com/send-beautiful-html-email-using-php/
https://webdesign.tutsplus.com/articles/build-an-html-email-template-from-scratch–webdesign-12770