差異處

這裏顯示兩個版本的差異處。

連向這個比對檢視

下次修改
前次修改
tech:rclone [2019/02/28 23:36] – 建立 jonathan_tsaitech:rclone [2025/03/17 13:12] (目前版本) – [設定每天 2:00 執行一次] jonathan
行 1: 行 1:
 +====== 使用 rclone 同步 Google Drive 檔案 ======
 +
 +因為愈來愈多檔案存放在 Google Drive, 所以要考慮一個自動的機制將檔案同步回自己的電腦主機內.
 +
 +===== 安裝方式 =====
 +==== Alpine 3 ====
 +  * 安裝環境 : Alpine Linux 3.18
 +  * 設定 cifs 掛上 NAS 目錄 : [[alpine_cifs]] 
 +  * <cli>
 +apk add rclone
 +rclone version
 +</cli>
 +    * ++看結果|<cli>
 +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
 +</cli>++
 +
 +==== CentOS 7 ==== 
 +  * 安裝環境 : CentOS Linux release 7.7.1908 (Core)
 +  * 安裝相關套件 <code sh>
 +yum install curl unzip man-db
 +</code>
 +  * 開始安裝 rclone <code sh>
 +curl https://rclone.org/install.sh |  bash
 +</code>
 +  * 安裝成功後可以下以下指令確認版本 <code sh>
 +rclone version
 +</code>
 +    * ++看結果|<cli>
 +[root@ct-cloudsync ~]# rclone version
 +rclone v1.50.1
 +- os/arch: linux/amd64
 +- go version: go1.13.4
 +</cli>++
 +
 +===== 設定 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 的建立 <code sh>rclone config</code>
 +    - 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 的語法 =====
 +  * 列出所有檔案 <code sh>rclone ls JonathanGooglePhotos:/ </code>
 +  * 只列出目錄 <code sh>rclone lsd JonathanGooglePhotos:/ </code>
 +  * 將所有檔案複製回本機 /nas_Photos/ 目錄內 <code sh>rclone copy -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/</code>
 +
 +
 +===== 設定每天 2:00 執行一次 =====
 +  * 設定到 crontab <code sh> vi /etc/crontab </code><file>
 +:
 +0 2 * * * root rclone copy --max-age 48h --no-traverse -v JonathanGooglePhotos:/ /nas_Photos/GooglePhotos/ 2>&1 | tee /tmp/syncGooglePhotos.log
 +</file>
 +  * 重新啟動 crond <code sh>systemctl restart crond</code>
 +
 +
 +<note>
 +  * 如果要設定可以自動寄出 log 內容, 可先參考 [[tech:mailx]] 
 +  * 將 /etc/crontab 改成類似以下的語法, 將原本產生的 /tmp/syncGooglePhotos.log 內容寄給 [email protected] <file>
 +:
 +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]
 +</file>
 +</note>
 +
 +<note tip>
 +  * 如果要改用 webhook 方式進行通知(Exp. Discord, Teams...)
 +    - 先安裝 bash, jq 與 curl Exp. <cli>
 +apk add bash jq curl
 +</cli>
 +    - 下載 [[https://raw.githubusercontent.com/tryweb/docker-compose/refs/heads/main/scripts/send_logs_to_discord.sh|send_logs_to_discord.sh]]<cli>
 +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
 +</cli>
 +    - 將 crontab 內容改成<file>
 +:
 +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"
 +
 +</file>  
 +</note>
 +===== 更新 rclone 方式 =====
 +  * 其實和開始安裝 rclone 的語法相同<code sh>
 +curl https://rclone.org/install.sh |  bash
 +</code>
 +  * 更新後的結果<file>
 +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.
 +</file>
 +
 +
 +===== 參考網址 =====
 +  * 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}}