3513 字
18 分鐘
Ubuntu-linux版-drive雲端硬碟大量複製檔案-Auto rclone + gclone

介紹#

google drive 需要大量的複製備份到另一個雲端硬碟,例如有 A:Team Drive(共享雲端硬碟)複製到 B:Team Drive(共享雲端硬碟),使用 Autorclone 及 gclone 工具,以 Ubuntu 18.04 做為測試,如果是中國大陸使用者需要掛 VPN 或是使用 VPS 架設,此教學即使不董程式也可以學會 Auto rclone + gclone 安裝及設定。。

  • 名詞

  • 每日上傳檔案數量上限

    • 個別使用者每天只能在「我的雲端硬碟」和所有共用雲端硬碟上傳 750 GB 的檔案。當使用者達到 750 GB 的上限或上傳大於 750 GB 的單一檔案,當天就無法再上傳其他檔案,不過上傳中的檔案仍可以完成上傳。可以上傳或同步處理的個別檔案大小上限則為 5 TB。
  • 取得無限空間帳號 如果是教育或企業帳號可以選擇共享資料夾或是共享硬碟,如果是一般帳號只有 15GB 容量必須使用共享硬碟(團隊盤) 台灣大學專校都已經使用 G Suite 教育版,想要使用 google 教育帳號可以去自己學校網站看看,只要學校信箱是 google gmail 就可以使用 google 任何服務,共享雲端硬碟要看 G Suite 管理員有沒有開放權限使用,也可以使用教育帳號個人雲端,因為教育帳號已經是無限空間了,所以教育帳號使用共享雲端硬碟就不是很重要了。

  • 其他取得方法可以到蝦皮、露天拍賣網站上看看 搜尋 google 教育(請不要買純新增共享雲端硬碟的這是很浪費錢的(找認識的朋友幫妳新增一個共享雲端硬碟或是往下看取得免費共享雲端硬碟),請購買 google 教育帳號,企業帳號也不要買萬一賣家不在付費 G Suite 也等於無限空間失效),以上個人的分析

取得免費的 Team Drive 共享雲端硬碟(團隊盤)#

不要放重要的資料,建議申請 Team Drive 使用英文名稱

Ubuntu#

前置安裝及設定#

更新及取得 root#

更新及取得 root

Terminal window
apt update -y && apt upgrade -y
apt install wget curl screen git sudo python3-distutils -y
sudo -i

安裝 python 3 及 pip 3#

Terminal window
apt install python3 python3-pip -y
#如果找不到pip請按以下命令操作
curl "https://bootstrap.pypa.io/get-pip.py" -o "get-pip.py"
python3 get-pip.py

安裝 AutoRlone 及設定#

安裝 AutoRclone、pip 相關 python 套件 指令

Terminal window
cd ~
git clone https://github.com/xyou365/AutoRclone && cd AutoRclone && sudo pip3 install -r requirements.txt

安裝 Rclone

Terminal window
curl https://rclone.org/install.sh | sudo bash
啟用 dirve api#

上傳 credentials.json 檔案#

credentials.json非常重要,如果沒有正確放到 AutoRclone 內是無法使用 api 的

複製credentials.json到 ubuntu

VPS 上傳檔案方法

Terminal window
scp 'PATH'/credentials.json 'SSH_USER_NAME'@'REMOTE_IP':'REMOTE_PATH'
'PATH' 本地 credentials.json 文件存放的路徑
'SSH_USER_NAME' SSH 用戶名
'REMOTE_IP' VPS的IP
'REMOTE_PATH' VPS 中 AutoRclone 目錄
以上輸入時不要''引號
  • 使用 windows 10 子系統 ubuntu 18.04 ,windows 10 連接到 ubuntu 系統資料夾 開啟任何的資料夾,輸入 \\wsl$

路徑\\wsl$\Ubuntu-18.04\root\AutoRclone 放入credentials.json

檢查文件

Terminal window
ls ~/AutoRclone | grep 'credentials.json'
自動建立 service 帳號#

建立 100 個 service 帳號,每個 project 能建立 100 個 service 帳號 以指令是建立 6 個 project 有 600 個 service 帳號,計算一下 600 * 750GB = 450,000 GB,建議不要建立太多個 project

建立

Terminal window
python3 gen_sa_accounts.py --quick-setup 5
  • 創建 6 個項目(項目 0 到項目 5)
  • 開啟相關的服務
  • 創建 600 個 service accounts(6 個項目,每個項目 100 個)
  • 將 600 個 service accounts 的授權文件下載到 accounts 資料夾下面

如果你已經有 N 個項目,現需要創建新的項目並在新的項目中創建 service accounts,直接運行

Terminal window
python3 gen_sa_accounts.py --quick-setup 2 --new-only
  • 額外創建 2 個項目(項目 N+1 到項目 N+2)
  • 開啟相關的服務
  • 創建 200 個 service accounts(2 個項目,每個項目 100 個)
  • 將 200 個 service accounts 的授權文件下載到 accounts 資料夾下面

圖有 windows 10 的命令視窗,沒有特別截圖 ubuntu,方法都是一模一樣的

複製產生的網址

一路按下允許

複製授權碼到 CMD 視窗上,按下 ENTER

複製網址,啟用 Service Usage API,在回到 CMD 視窗按下 ENTER,會自動開啟建立 service 帳號

建立完成後,到accounts資料夾看到非常多的 json 檔,每個 json 檔都是一個 service 帳號

檢查 sa 文件

Terminal window
ls ~/AutoRclone/accounts

加入 service 帳號到 team drive#

  • 成員限制說明:https://support.google.com/a/answer/7338880?hl=zh-Hant(在新分頁開啟)

  • 有兩種方法,任選一種

    • 第一種使用 python 腳本自動將所有的 service 帳號加入到 team drive,最多能加 600 個帳號,這是最快的方法,
    • 第二種 使用 groups 論壇,建立群組並手動加入 service 帳號,一天最多加入 100 個,這種方法會比較慢,但是可以加入非常多個,之後只需要將 groups 論壇的管理員 email 加入到 team dirve 就可以了
    • 注意:如果是請朋友幫你加入共享雲端硬碟,必須是管理員權限
  • 使用 python 腳本自動將所有的 service 帳號加入到 team drive 指令, SharedTeamDriveSrcID 改成 team drive 的 ID

python3 add_to_team_drive.py -d SharedTeamDriveSrcID

取得 team drive 的 ID,例如:0AL9kUqtQlodwUk9PVA

注意:一個 team drive 最多只能加入 600 個使用者

如果有 600 個 service 帳號卻沒有加入滿到 600,只需要多執行幾次就可以,直到加入到 600 個

到這裡就快完成啦

  • 使用 groups 論壇,建立群組並手動加入 service 帳號

批量提取 SA email 文件: 提取 accounts 目錄內 sa json 中的 email

Terminal window
cat ~/AutoRclone/accounts/*.json | grep "client_email" | awk '{print $2}'| tr -d ',"' | sed 'N;N;N;N;N;N;N;N;N;/^$/d;G' > ~/email

執行完上步驟後可以把 ’~/email’ 文件下載到本地自己保存

文件會放在 root

Terminal window
cd ~
ls
AutoRclone email

查看 email 內容

Terminal window
cat email

建立 群組論壇

取得群組 email (非常重要)

例如:[email protected]

將群組 EMAIL 加入到 Team drive

GSuite Admin 加入成員方法 step-3-add-service-accounts-to-google-groups-optional-but-recommended-for-hassle-free-long-term-use(在新分頁開啟)

安裝 gclone 及 Rclone#

  • 安裝 Rclone 指令
Terminal window
cd ~
curl https://rclone.org/install.sh | sudo bash
  • 安裝 gclone 指令
Terminal window
cd ~
bash <(wget -qO- https://git.io/gclone.sh)
gclone 建立設定檔#

輸入命令獲取一個 sa json 文件名,隨機取得一個 sa json 檔名,設定時需要使用到一個 json 檔名

Terminal window
ls ~/AutoRclone/accounts | awk 'NR==1'

輸入 gclone config

Terminal window
root@:~# gclone config
2020/08/08 22:58:12 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote < 建立一個新的remote
s) Set configuration password
q) Quit config
n/s/q> n < 輸入n
name> gd_team
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
1 / 1Fichier
\ "fichier"
2 / Alias for an existing remote
\ "alias"
3 / Amazon Drive
\ "amazon cloud drive"
4 / Amazon S3 Compliant Storage Provider (AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, etc)
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Box
\ "box"
7 / Cache a remote
\ "cache"
8 / Citrix Sharefile
\ "sharefile"
9 / Dropbox
\ "dropbox"
10 / Encrypt/Decrypt a remote
\ "crypt"
11 / FTP Connection
\ "ftp"
12 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
13 / Google Drive < 因為要使用dirve 輸入13
\ "drive"
14 / Google Photos
\ "google photos"
15 / Hubic
\ "hubic"
16 / In memory object storage system.
\ "memory"
17 / JottaCloud
\ "jottacloud"
18 / Koofr
\ "koofr"
19 / Local Disk
\ "local"
20 / Mail.ru Cloud
\ "mailru"
21 / Mega
\ "mega"
22 / Microsoft Azure Blob Storage
\ "azureblob"
23 / Microsoft OneDrive
\ "onedrive"
24 / OpenDrive
\ "opendrive"
25 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
26 / Pcloud
\ "pcloud"
27 / Put.io
\ "putio"
28 / QingCloud Object Storage
\ "qingstor"
29 / SSH/SFTP Connection
\ "sftp"
30 / Sugarsync
\ "sugarsync"
31 / Transparently chunk/split large files
\ "chunker"
32 / Union merges the contents of several remotes
\ "union"
33 / Webdav
\ "webdav"
34 / Yandex Disk
\ "yandex"
35 / http Connection
\ "http"
36 / premiumize.me
\ "premiumizeme"
Storage> 13
** See help for drive backend at: https://rclone.org/drive/ **
Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> 留空白
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> 留空白
Scope that rclone should use when requesting access from drive.
Enter a string value. Press Enter for the default ("").
Choose a number from below, or type in your own value
1 / Full access all files, excluding Application Data Folder.
\ "drive"
2 / Read-only access to file metadata and file contents.
\ "drive.readonly"
/ Access to files created by rclone only.
3 | These are visible in the drive website.
| File authorization is revoked when the user deauthorizes the app.
\ "drive.file"
/ Allows read and write access to the Application Data folder.
4 | This is not visible in the drive website.
\ "drive.appfolder"
/ Allows read-only access to file metadata but
5 | does not allow any access to read or download file content.
\ "drive.metadata.readonly"
scope> 1 < 輸入 1
ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders (see docs), or for rclone to use
a non root folder as its starting point.
Note that if this is blank, the first time rclone runs it will fill it
in with the ID of the root folder.
Enter a string value. Press Enter for the default ("").
root_folder_id> 留空白,默認一個資料夾為根目錄,沒有特別需求是不需要的
Service Account Credentials JSON file path
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Enter a string value. Press Enter for the default (""). < json不要與我一樣,從你的accounts隨便取得一個json檔名
service_account_file> /root/AutoRclone/accounts/00af8bc13083978e5ad222c77380c3b6877fdeda.json
Service Account Credentials JSON file path .
Enter a string value. Press Enter for the default (""). < 重要必須輸入,讀取sa的路徑
service_account_file_path> /root/AutoRclone/accounts/
Edit advanced config? (y/n) 高級設定
y) Yes
n) No (default)
y/n> n <輸入 n
Remote config
Configure this as a team drive? 設定一個team drive,使用team drive需要輸入y
y) Yes
n) No (default)
y/n> y < 輸入y
Fetching team drive list...
Choose a number from below, or type in your own value
1 / share_gogogo < 每個人team drive都不一樣,選擇要使用team drive代號
\ "0AL9kUqtQlodwUk9PVA"
Enter a Team Drive ID> 1
--------------------
[gd_team]
type = drive
client_id =
client_secret =
scope = drive
service_account_file = /root/AutoRclone/accounts/00af8bc13083978e5ad222c77380c3b6877fdeda.json
service_account_file_path = /root/AutoRclone/accounts/
team_drive = 0AL9kUqtQlodwUk9PVA
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:
Name Type
==== ====
gd_team drive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> q

注意:指令需指定 Name,所以需要記得你的 Name 是叫什麼,例如我的是 gd_team,也可以使用 gclone config 或是 rclone config 兩種都吃同一個設定檔

指令使用方式#

gclone 指令#

作者 github(在新分頁開啟)

Terminal window
gclone copy gd_team:{源} gd_team:my team name/資料夾1/資料夾2/ --drive-server-side-across-configs -v

實際使用方法:

我的共享盤名稱是:share_gogogo

要 copy 的檔案:https://drive.google.com/uc?id=1l_dr7tqoNZja94b_AZ3LLmfLr8xk4g8x(在新分頁開啟)

取得網址的 ID,1l_dr7tqoNZja94b_AZ3LLmfLr8xk4g8x

因為設定時候已經設定好 team drive,所以不用再次輸入 team drive 名稱

Terminal window
gclone copy gd_team:{1l_dr7tqoNZja94b_AZ3LLmfLr8xk4g8x} gd_team:雷電模擬器 --drive-server-side-across-configs -v

資料夾結構

也可以指定其他 team drive,注意必須要加入權限帳號,有允許權限才能複製到指定的雲端硬碟

Terminal window
gclone copy gc:{folde_id1} gc:{folde_id2} --drive-server-side-across-configs

Autorclone 指令#

使用 Autorclone copy 資料

需求:rclone

  • rclone 安裝指令 To install rclone on Linux/macOS/BSD systems, run:
    Terminal window
    curl https://rclone.org/install.sh | sudo bash
    輸入 rclone -h ,有顯示一推指令說明 代表安裝正常

SourceID = 源檔案,需 copy 的資料 ID DestinationID = 自己的 team drive id DestinationPathName = 路徑(資料夾) 會自動建立資料夾,不需要手動新增

python rclone_sa_magic.py -s SourceID -d DestinationID -dp DestinationPathName -b 1 -e 600

取得自己 team drive id

檢查大小 如果沒有源路徑及目標路徑 不需輸入,路徑也就是資料夾意思 rclone —config rclone.conf size —disable ListR src001<源路徑> rclone —config rclone.conf size —disable ListR dst001<目標路徑>

指令幫助說明

Terminal window
python3 rclone_sa_magic.py -h

AutoRclone 作者的教學 https://gsuitems.com/index.php/archives/13/(在新分頁開啟)

建立自己的 client_id#

修改 client_id 及 client_secret

Terminal window
c:\AutoRclone-master>gclone config
Current remotes:
Name Type
==== ====
gd_team drive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q> e
Choose a number from below, or type in an existing value
1 > gd_team
remote> 1
--------------------
[gd_team]
type = drive
scope = drive
service_account_file = /AutoRclone-master/accounts/00af8bc13083978e5ad222c77380c3b6877fdeda.json
service_account_file_path = /AutoRclone-master/accounts/
team_drive = 0AL9kUqtQlodwUk9PVA
--------------------
Edit remote
** See help for drive backend at: https://rclone.org/drive/ **
Value "client_id" = ""
Edit? (y/n)>
y) Yes
n) No (default)
y/n> y
Google Application Client Id
Setting your own is recommended.
See https://rclone.org/drive/#making-your-own-client-id for how to create your own.
If you leave this blank, it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id> 75484848268-tr0lm3aoi6a6uuedv0bn2fnrsjqqvqep.apps.googleusercontent.com
Value "client_secret" = ""
Edit? (y/n)>
y) Yes
n) No (default)
y/n> y
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> dJty5bR1TXYVzRyuS9JmQ8u0
Value "scope" = "drive"
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Value "root_folder_id" = ""
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Value "service_account_file" = "/AutoRclone-master/accounts/00af8bc13083978e5ad222c77380c3b6877fdeda.json"
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Value "service_account_file_path" = "/AutoRclone-master/accounts/"
Edit? (y/n)>
y) Yes
n) No (default)
y/n> n
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n
--------------------
[gd_team]
type = drive
scope = drive
service_account_file = /AutoRclone-master/accounts/00af8bc13083978e5ad222c77380c3b6877fdeda.json
service_account_file_path = /AutoRclone-master/accounts/
team_drive = 0AL9kUqtQlodwUk9PVA
client_id = 75484848268-tr0lm3aoi6a6uuedv0bn2fnrsjqqvqep.apps.googleusercontent.com
client_secret = dJty5bR1TXYVzRyuS9JmQ8u0
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
Remote config
Change current team drive ID "0AL9kUqtQlodwUk9PVA"?
y) Yes
n) No (default)
y/n> n
Current remotes:
Name Type
==== ====
gd_team drive
e) Edit existing remote
n) New remote
d) Delete remote
r) Rename remote
c) Copy remote
s) Set configuration password
q) Quit config
e/n/d/r/c/s/q>

問題#

使用 gclone 可以傳送幾個檔案之後會有錯誤訊息#

panic: runtime error: invalid memory address or nil pointer dereference

為了快速解決問題,不使用 gclone 指令,建議直接使用 Autorclone 指令處理

問題除錯#

編輯設定指令:gclone config root_folder_id 有錯誤的話,無法複製到你指定的共享資料夾及團隊盤 確定 root_folder_id 是否為""(空) 或是 輸入正確資料夾 ID

參考文章#

若此篇文章你還是看不懂,請看下列的文章及影片

Ubuntu-linux版-drive雲端硬碟大量複製檔案-Auto rclone + gclone
https://aishuafei.com/posts/ubuntu-linux版-drive雲端硬碟大量複製檔案-auto-rclone-gclone/
作者
愛耍廢
發佈於
2020-08-07
許可協議
CC BY-NC-SA 4.0