注:圖片如果損壞,點擊文章鏈接:https://www.toutiao.com/i6812982512256549387/
承接上一篇文檔《Java實現163郵箱發送郵件到QQ郵箱》
主方法修改獲取數據的方式
打包
可以看到打包的位置
選擇jar
上傳到linux執行
執行語句
java -jar javamail-1.0-jar-with-dependencies.jar /data/qxclxx/shell/test.sql mail.sql 你的郵箱@qq.com
查看郵箱,可以看到是有的
編寫一個腳本完成測試數據庫的備份
創建腳本
編寫腳本
運行腳本
查看郵件成功了
將腳本目錄加入搭配環境變量中
查看下
在任意目錄直接執行腳本
出現問題
Error: Unable to access jarfile javamail-1.0-jar-with-dependencies.jar
改下權限(省事直接全給了)
chmod -R 777 javamail-1.0-jar-with-dependencies.jar
仍然出現這個錯誤
在腳本中使用全路徑
這次成功了,同時也收到郵件了
寫一個定時器,看是否能夠定時發送郵件
查看日誌
tail -f /var/log/cron
出現錯誤
mailed 42 bytes of output but got status 0x004b#012
查看日誌
cat /var/log/maillog
發現錯誤
fatal: parameter inet_interfaces: no local interface found for ::1
解決方法:
vim /etc/postfix/main.cf
將:
inet_interfaces = localhost
inet_protocols = all
改成:
inet_interfaces = all
inet_protocols = all
inet_interfaces 參數指定postfix系統監聽的網絡接口。缺省地,postfix監聽所有的網絡接口。如果你的postfix運行在一個虛擬的ip地址上,則必須指定其監聽的地址。如:
inet_interfaces = all
inet_interface = 192.168.1.1
重新啓動
service postfix start
可以查看到下面的內容
裏面有錯誤信息
You have new mail in /var/spool/mail/root
打開可以看到下面的錯誤內容
sh: mailexe.sh: No such file or directory
crontab 有一個壞毛病, 就是它總是不會缺省的從用戶 profile 文件中讀取環境變量參數,經常導致在手工執行某個腳本時是成功的,但是到 crontab 中試圖讓它定期執行時就是會出錯。
修復:在腳本文件的開頭,強制要求導入環境變量,可保萬無一失。
這樣的話,腳本的頭部一律以下列格式開頭:
#!/bin/sh
. /etc/profile
. ~/.bash_profile
可以將備份的sql發到郵箱了,這樣就可以備份很多東西了