====== 使用 rclone 同步 Google Drive 檔案 ======
因為愈來愈多檔案存放在 Google Drive, 所以要考慮一個自動的機制將檔案同步回自己的電腦主機內.
**如果需要 Docker Compose 版本可以參考** - [[tech/rclone_docker]]
===== 安裝方式 =====
==== Alpine 3 ====
* 安裝環境 : Alpine Linux 3.18
* 設定 cifs 掛上 NAS 目錄 : [[alpine_cifs]]
*
apk add rclone
rclone version
* ++看結果|
pve-alpine3:~# rclone version
rclone v1.62.2-DEV
- os/version: alpine 3.18.6 (64 bit)
- os/kernel: 6.1.84-0-lts (x86_64)
- os/type: linux
- os/arch: amd64
- go/version: go1.20.11
- go/linking: static
- go/tags: none
++
==== CentOS 7 ====
* 安裝環境 : CentOS Linux release 7.7.1908 (Core)
* 安裝相關套件
yum install curl unzip man-db
* 開始安裝 rclone
curl https://rclone.org/install.sh | bash
* 安裝成功後可以下以下指令確認版本
rclone version
* ++看結果|
[root@ct-cloudsync ~]# rclone version
rclone v1.50.1
- os/arch: linux/amd64
- go version: go1.13.4
++
===== 設定 rclone 對 Google Drive / Photos Library API 的 config =====
* 依據 rclone 的說明, 如果使用量大, 最好申請自己 Google 帳號的 oAuth 方式來使用 rclone
* 申請 Google API oAuth 的方式 :
- 連上 Google Developer Console - https://console.developers.google.com/
- 選擇一個專案 (第一次使用請先建立專案) ++看畫面|{{:tech:2019022801.png}}++
- 啟用 Google Drive API++看畫面|{{:tech:2019022802.png}} {{:tech:2019022803.png}}++
- 啟用 Photos Library API++看畫面|{{:tech:2019110403.png}} {{:tech:2019110404.png}}++
- 建立 oAuth 憑證++看畫面|{{:tech:2019022804.png}} {{:tech:2019022805.png}} {{:tech:2019022806.png}}++
* 進行 rclone config 的建立 rclone config
- n) New Remote (建立一個新的遠端連結定義)
- name> (設定這個遠端連結定義名稱) Exp. JonathanGooglePhotos
- Storage> (選擇 Cloud Storage 類型)
- 如果是 Google Drive 就要選 -> **15 Google Drive**
- 如果是要同步 Google 相簿 就要選 -> **16 Google Photos**
- client_id> (就直接輸入 oAuth 申請的 **用戶端 ID**)
- client_secret> (就直接輸入 oAuth 申請的 **用戶端密碼**)
- scope> (如果想雙向同步就選可讀寫 1, 若指向備份回本地端就選唯讀 2)
- root_folder_id> (如果整個 Drive 都要讀取就留空白, 因我定義這遠端連結是 Google 相簿, 所以先在瀏覽器上 Drive 點入 Google 相簿, 看到網址的 folder id) Exp. 我的雲端硬碟 -> Google 相簿 網址 - https://drive.google.com/drive/u/0/folders/1RxkXxxxxxxxxxxxxxxxxxxxxxxxq7F_45Fxxxxxgs , 所以 Folder id 就是 **1RxkXxxxxxxxxxxxxxxxxxxxxxxxq7F_45Fxxxxxgs**
- service_account_file> (空白就好)
- Edit advanced config? (y/n) n
- Remote config Use auto config? (y/n) n <- 因為直接在 Linux Console 無法本機端有瀏覽器, 所以要選 n 產生認證網址
- 將出現的網址 https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=20xxxxxxxxxx.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.readonly&state=a9exxxxxxxxxxxxxxxxxxxxxxxxxa823 貼上工作電腦的瀏覽器上
- 登入 Google 帳號
- 允許 rclone 的授權
- 將產生的授權碼複製下來 Exp. 4/_wxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxp9bRJw
- Enter verification code> (輸入瀏覽器產生的授權碼)
- Configure this as a team drive? (y/n) (個人的 Drive 就選 n)
- y/e/d> (如果呈現的設定都沒問題就選 y)
- q (離開 config 選單)
===== 使用 rclone 讀取 Google Drive 的語法 =====
* 列出所有檔案 rclone ls JonathanGooglePhotos:/
* 只列出目錄 rclone lsd JonathanGooglePhotos:/
* 將所有檔案複製回本機 /nas_Photos/ 目錄內 rclone copy -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/
===== 設定每天 2:00 執行一次 =====
* 設定到 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 內容, 可先參考 [[tech:mailx]]
* 將 /etc/crontab 改成類似以下的語法, 將原本產生的 /tmp/syncGooglePhotos.log 內容寄給 log@ichiayi.com
:
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" log@ichiayi.com
* 如果要改用 webhook 方式進行通知(Exp. Discord, Teams...)
- 先安裝 bash, jq 與 curl Exp.
apk add bash jq curl
- 下載 [[https://raw.githubusercontent.com/tryweb/docker-compose/refs/heads/main/scripts/send_logs_to_discord.sh|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
- 將 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 方式 =====
* 其實和開始安裝 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.
===== 啟用 rclone WebUI =====
* 只要加上 rcd --rc-web-gui 就可以啟動 Web UI 的服務
* 如果要外部電腦也可以使用 Web UI 就要加上 --rc-addr 0.0.0.0:5572
* 再加上登入帳號密碼 Exp. --rc-user admin --rc-pass "MyPass_111"
* Exp.
rclone rcd --rc-web-gui --rc-addr 0.0.0.0:5572 --rc-user admin --rc-pass "MyPass_111" &
\\ {{:tech:螢幕擷取畫面_2025-04-21_135724.png|}}
===== 參考網址 =====
* https://rclone.org/drive/
* https://blog.csie.io/rclone-loves-google-drive.html
* https://forum.rclone.org/t/log-file-and-stats-simultaneously/4230/14
{{tag>CentOS7 rclone Google Drive Cloud}}