使用 rclone 同步 Google Drive 檔案

因為愈來愈多檔案存放在 Google Drive, 所以要考慮一個自動的機制將檔案同步回自己的電腦主機內.

  • 安裝環境 : Alpine Linux 3.18
  • 設定 cifs 掛上 NAS 目錄 : Alpine 設定 CIFS(SAMBA) 方式
  • apk add rclone
    rclone version

    • 看結果
  • 安裝環境 : CentOS Linux release 7.7.1908 (Core)
  • 安裝相關套件
    yum install curl unzip man-db
  • 開始安裝 rclone
    curl https://rclone.org/install.sh |  bash
  • 安裝成功後可以下以下指令確認版本
    rclone version
    • 看結果
  • 列出所有檔案
    rclone ls JonathanGooglePhotos:/ 
  • 只列出目錄
    rclone lsd JonathanGooglePhotos:/ 
  • 將所有檔案複製回本機 /nas_Photos/ 目錄內
    rclone copy -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/
  • 設定到 crontab
     vi /etc/crontab 
    :
    0 2 * * * root rclone copy --max-age 48h --no-traverse -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/ 2>&1 | tee /tmp/syncGooglePhotos.log
  • 重新啟動 crond
    systemctl restart crond
  • 如果要設定可以自動寄出 log 內容, 可先參考 CentOS7 設定 mailx 寄出 log 方式
  • 將 /etc/crontab 改成類似以下的語法, 將原本產生的 /tmp/syncGooglePhotos.log 內容寄給 [email protected]
    :
    0 2 * * * root rclone copy --max-age 48h --no-traverse -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/ 2>&1 | tee /tmp/syncGooglePhotos.log ; cat /tmp/syncGooglePhotos.log | mail -s "RClone Sync Log" [email protected]
  • 如果要改用 webhook 方式進行通知(Exp. Discord, Teams…)
    1. 先安裝 bash, jq 與 curl Exp.

      apk add bash jq curl

    2. 下載 send_logs_to_discord.sh

      curl -o /usr/local/bin/send_logs_to_discord.sh https://raw.githubusercontent.com/tryweb/docker-compose/refs/heads/main/scripts/send_logs_to_discord.sh
      chmod a+x /usr/local/bin/send_logs_to_discord.sh

    3. 將 crontab 內容改成
      :
      0 2 * * * root rclone copy --max-age 48h --no-traverse -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/ 2>&1 | tee /tmp/syncGooglePhotos.log && send_logs_to_discord.sh TITLE="RClone sync Google Photos..." LOG_FILE="/tmp/syncGooglePhotos.log" WEBHOOK_URL="https://discord.com/api/webhooks/13xxxxxxxxxxxxxxx28/Wxxxxxy-hkxxxxxxxxxxxxxxxxxxxxIOsyuGjyaqxxxxxxxxxxxxxxxxxxxxNJ0kYFnC"
      
  • 其實和開始安裝 rclone 的語法相同
    curl https://rclone.org/install.sh |  bash
  • 更新後的結果
    Processing manual pages under /usr/share/man/sk...
    Purging old database entries in /usr/share/man/tr...
    Processing manual pages under /usr/share/man/tr...
    Purging old database entries in /usr/share/man/da...
    Processing manual pages under /usr/share/man/da...
    Purging old database entries in /usr/local/share/man...
    Processing manual pages under /usr/local/share/man...
    0 man subdirectories contained newer manual pages.
    0 manual pages were added.
    0 stray cats were added.
    0 old database entries were purged.
    
    rclone v1.54.0 has successfully installed.
    Now run "rclone config" for setup. Check https://rclone.org/docs/ for more details.
  • tech/rclone.txt
  • 上一次變更: 2025/03/17 13:12
  • jonathan