自動將 Ubuntu Server 日常排程(crontab)執行訊息(log)傳送至 Gmail 及 Authorication Failed 處理

step1: 下載 ssmtp

$ sudo apt-get update && echo y | sudo apt-get install ssmtp mailutils

step2: 編輯 ssmtp.conf 檔

$ sudo rm /etc/ssmtp/ssmtp.conf && sudo nano /etc/ssmtp/ssmtp.conf

將下方內容貼至 ssmtp.conf 檔案中

# Config file for sSMTP sendmail
# DONT CHANGE BELOW FOR @GMAIL.COM
mailhub=smtp.gmail.com:587
rewriteDomain=gmail.com
hostname=localhost
FromLineOverride=YES
UseTLS=Yes
UseSTARTTLS=Yes

# CHANGE NEXT 3 OPTIONS 將下方三行改為自己的帳號密碼
# Username/Password (NO @Gmail)
root=yourusername@gmail.com
AuthUser=yourusername
AuthPass=yourpassword

step3: 編輯 revaliases 檔

$ sudo nano /etc/ssmtp/revaliases

將下方內容貼至 revaliases 檔案中

# sSMTP aliases
# linux user:mail:stmp port
# 將 ubuntu 中的使用者 (user) 對應其 email
root:yourusername@gmail.com:smtp.gmail.com:587
guest:somebody@gmail.com:smtp.gmail.com:587

step4: 測試是否可寄送 email

$ ssmtp yourusername@gmail.com

輸入上方指令按下 enter,貼上下方段落 email 內容。

note: Subject 之後一定要空行

To: yourusername@gmail.com
From: yourusername@gmail.com
Subject: test

Hello World!

完成後使用 ctrl+d 跳出,至信箱檢查 email。

也可使用指令 echo "Hello world mail content" | mail -s "Hello world Subject" yourusername@gmail.com 測試寄信。

若顯示錯誤訊息 Authorization failed (534 5.7.14 https://support.google.com/mail/answer/78754 tm1sm6036157pac.23 – gsmtp

首先到 https://www.google.com/settings/u/1/security/lesssecureapps安全性較低的應用程式存取權限 設為 開啟

再試試 step4 ,若還是一樣顯示錯誤,則可試第二種方法,

下載文字瀏覽器 w3m,

$ sudo apt-get install w3m w3m-img

連結至 gmail 頁面,並輸入帳號密碼登入,

$ w3m mail.google.com

初次登入 gmail 會要求輸入備援電話號碼之驗證碼或備援電子信箱,

登入成功後再重新試 step4。

step5: 使日常排程將 log 自動發送至 email

前面測試寄送 email 成功後,使用指令編輯 crontab,

$sudo vi /etc/crontab

在 crontab 中 PATH 下一行加入

MAILTO=yourusername@gmail.com

於 Ubuntu Server 使用 Crontab 定時自動備份 Mysql 資料庫

使用指令 sudo vi /etc/crontab 編輯檔案

加入備份指令如下

# 分 時 日 月 週 使用者 指令
# 表示於每日12點整,以 root 身份備份 mysql 所有資料庫並存放到 /home/user/mysqldump/ 資料夾中
0 0 * * * root mysqldump -u root -proot --all-databases > /home/user/mysqldump/database_`date '+\%Y-\%m-\%d'`.sql
# 只備份指定的資料庫 somedb
0 0 * * * root mysqldump -u root -proot somedb > /home/user/mysqldump/somedb_`date '+\%Y-\%m-\%d'`.sql

使用指令 sudo service cron restart 重啟 crontab 服務


相關文章:

自動將 Ubuntu Server 日常排程(crontab)執行訊息(log)傳送至 Gmail 及 Authorication Failed 處理

Grails: 設定 Session 自動登出時間

在 src/templates/war/web.xml 中修改 <session-timeout> 的設定即可,

時間單位為分鐘,預設值為 30。

<session-config>
    <!-- 30 minutes -->
    <session-timeout>30</session-timeout>
</session-config>

Grails: 更改 Cookie 名稱以解決多專案無法同時登入問題

Grails 同時 run 多專案時,若一專案登入帳號,另一專案便會自動登出,

這是由於 cookie 名稱相同所造成,

解決方式:

先安裝 templates $grails install-templates

在 src/templates/war/web.xml 中新增自訂的名稱即可。

<session-config>
    <cookie-config>
        <name>MY_SESSION_NAME</name>
    </cookie-config>
</session-config>

Facebook 按鈕造成 the Domain of This URL Isn’t Included in the App’s Domains… 錯誤 如:讚(like)、分享(share)、登入(login)…等

最近發現網頁出現錯誤訊息:

無法載入網址: The domain of this URL isn’t included in the app’s domains. To be able to load this URL, add all domains and subdomains of your app to the App Domains field in your app settings.

檢查後發現是 facebook 按鈕造成的,

查了相關資料應該是要將網頁與 facebook 的開發管理網站連結,

其實不做的話按鈕還是可以正常使用,只是夏天有潔癖看了不開心 XDD

首先登入 Facebook 開發者網站

接著新增一個應用程式,在左邊選單點選設定選項,

在右邊畫面下方可以找到+新增平台按鈕,點下後選取網頁類型並輸入網站網址,

完成後按下立即開始,將程式碼加入網頁中的 body 區塊即可。