Sử dụng Rclone để sync, copy giữa các Google Drive, Dropbox và OneDrive

SolarWinds

Super Moderate
Nov 16, 2006
51,322
25
48
Hanoi
www.hanoiyeu.com
1700029084685.png



Rclone là giải pháp miễn phí, mã nguồn mở, được sử dụng rộng rãi trên toàn thế giới. Bài viết chỉ liệt kê 3 cloud tiêu biểu, nhưng bạn có thể dùng cách này với hơn 40 các dịch vụ cloud khác nhau mà Rclone hỗ trợ.

1. CHUẨN BỊ

Bước 1: VPS chạy Ubuntu

Bạn hoàn toàn có thể sử dụng trên máy tính cá nhân nhưng để tiết kiệm thời gian và tránh hao mòn thiết bị ta sẽ sử dụng trên VPS – giống như máy tính của bạn nhưng chạy trên hạ tầng internet tốc độ cao. Ngọc chọn dịch vụ thuê VPS của Vultr – thằng này khá uy tín và tính phí theo giờ và băng thông sử dụng.

Truy cập vultr.com để đăng ký tài khoản. Vultr có thể sẽ yêu cầu bạn thanh toán 5$ bằng thẻ thanh toán Quốc tế hoặc Paypal để xác thực tài khoản. Mình thấy đang có khuyến mãi tặng 100$ để dùng thử trong 14 ngày.

Ngọc cần chuyển 3TB dữ liệu nên sẽ chọn 1 con VPS 2vCPU, 4GB RAM và 5TB bandwidth. Bạn xem video bên dưới nếu đây là lần đầu tiên bạn dùng Vultr.


Xác nhận có thể đồng bộ với Rclone hơn 750GB/ngày với Google Drive. Khi dùng cỡ 3TB/ngày trở lên thì cả Google Drive và OneDrive đều limit băng thông, khá chán (thử trên trên tài khoản trả phí của các dịch vụ).

Bạn có thể kiểm tra mức phí bandwidth theo giờ tại đây (3TB ~ 30$). Nếu được tặng 100$ dùng thử thì chẳng mất đồng nào. Phí sẽ được tính vào cuối tháng, một số bạn bùng $ tạo tiền lệ xấu khiến các nhà cung cấp VPS theo giờ dần dần từ chối Việt Nam.


Tạo VPS xong đợi vài phút sẽ có email gửi về chứa mật khẩu đăng nhập vào VPS.

Ngoài ra bạn cũng có thể đăng ký Google Cloud, tài khoản người dùng mới được +300$ dùng thử, không bị mất tiền.

Bước 2: Cài đặt ZOC

Đây là trình kết nối SSH Ngọc yêu thích, có cả phiên bản cho macOS và Windows. Tải về tại emtec.com/zoc, cài đặt như các phần mềm thông thường.

Trên giao diện của ZOC, bấm Quick Connection trên thanh công cụ, trong mục Connection type chọn là Secure Shell, sẽ hiện ra ô để bạn nhập Username (root) và Password (được Vultr gửi qua email) như hình:

1700029202722.png


2. KẾT NỐI CLOUD

Ta sẽ chạy lệnh trên giao diện của ZOC, copy dán vào bấm enter là được.

Đầu tiên ta cần chạy cập nhật cho Ubuntu:
sudo apt-get update && sudo apt-get upgrade
Bước tiếp theo, cài đặt Rclone:

Để kết nối với cloud thì ta dùng lệnh rclone config, bạn tham khảo hướng dẫn chi tiết từng loại cloud bên dưới (phần in đậm là phần người dùng nhập).

Phức tạp nhất có lẽ là bước lấy config_token, bạn cần cài Rclone trên máy tính cá nhân, mở một cửa sổ Terminal (macOS) hoặc CMD (Windows) chạy lệnh rclone authorize… (copy từ yêu cầu của Rclone), đăng nhập vô cloud để xác minh ủy quyền, lấy mã trong cửa sổ lệnh dán lại vào ZOC. Đọc thêm tại rclone.org/remote_setup.

Kết nối rclone với Google Drive:
root@ubuntu:~# rclone config
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n

Enter name for new remote.
name> drive

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
...
13 / Dropbox
\ (dropbox)
...
18 / Google Drive
\ (drive)
...
31 / Microsoft OneDrive
\ (onedrive)
...
Storage> 18

Option client_id.
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 value. Press Enter to leave empty.
client_id> Bỏ trống

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> Bỏ trống

Option scope.
Scope that rclone should use when requesting access from drive.
Choose a number from below, or type in your own value.
Press Enter to leave empty.
1 / Full access all files, excluding Application Data Folder.
\ (drive)
...
scope> 1

Option service_account_file.
Service Account Credentials JSON file path.
Leave blank normally.
Needed only if you want use SA instead of interactive login.
Leading `~` will be expanded in the file name as will environment variables such as `${RCLONE_CONFIG_DIR}`.
Enter a value. Press Enter to leave empty.
service_account_file> Bỏ trống

Edit advanced config?
y) Yes
n) No (default)
y/n> n

Use web browser to automatically authenticate rclone with remote?
* Say Y if the machine running rclone has a web browser you can use
* Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes (default)
n) No
y/n> n

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
rclone authorize "drive" "eyJzY29wZSI6ImRyaXZlIn0"
Then paste the result.
Enter a value.
config_token> eyJ0b2tl...BcIn0ifQ

Configure this as a Shared Drive (Team Drive)?
y) Yes
n) No (default)
y/n> n

Configuration complete.
Options:
- type: drive
- scope: drive
- token: {"access_token":"ya29.a0...sqxi_r-2eAXTc","expiry":"2023-07-26T05:50:03.317295+07:00"}
- team_drive:
Keep this "drive" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Current remotes:

Name Type
==== ====
drive 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

Kết nối OneDrive:
root@ubuntu:~# rclone config
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> n

Enter name for new remote.
name> onedrive

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
...
13 / Dropbox
\ (dropbox)
...
18 / Google Drive
\ (drive)
...
31 / Microsoft OneDrive
\ (onedrive)
...
Storage> 31

Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id> Bỏ trống

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> Bỏ trống

Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own string value.
Press Enter for the default (global).
1 / Microsoft Cloud Global
\ (global)
...
region> 1

Edit advanced config?
y) Yes
n) No (default)
y/n> n

Use web browser to automatically authenticate rclone with remote?
* Say Y if the machine running rclone has a web browser you can use
* Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes (default)
n) No
y/n> n

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
rclone authorize "onedrive"
Then paste the result.
Enter a value.
config_token> {"access_token":"eyJ0eXAiOiJKV1QiLCJ...MEwav3jSLfSmRQeG3K23JvpOY_OEtNG8_8uWWtaMBKCmFuON55EMcFJgGZnoqgYM4QDG5fzdzDmRTQ5YWqA","expiry":"2023-07-26T06:03:49.851088+07:00"}

Option config_type.
Type of connection
Choose a number from below, or type in an existing string value.
Press Enter for the default (onedrive).
1 / OneDrive Personal or Business
\ (onedrive)
...
config_type> 1

Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own string value.
Press Enter for the default (b!8MNTFKp3zUeC-F9_wmtSHZvSny3kpXZAluzrW9n_wU9ScSmUqzcASJVAaeWHbIIh).
1 / OneDrive (business)
\ (b!8MNTFKp3zUeC-F9_wmtSHZvSny3kpXZAluzrW9n_wU9ScSmUqzcASJVAaeWHbIIh)
config_driveid> 1

Drive OK?
Found drive "root" of type "business"
URL: https://tuangiao-my.sharepoint.com/personal/namng7892_tuangiao_edu_vn/Documents
y) Yes (default)
n) No
y/n> y

Configuration complete.
Options:
- type: onedrive
- token: {"access_token":"eyJ0eXAiOiJKV1QiLCJ...MEwav3jSLfSmRQeG3K23JvpOY_OEtNG8_8uWWtaMBKCmFuON55EMcFJgGZnoqgYM4QDG5fzdzDmRTQ5YWqA","expiry":"2023-07-26T06:03:49.851088+07:00"}
- drive_id: b!8MNTFKp3zUeC-F9_wmtSHZvSny3kpXZAluzrW9n_wU9ScSmUqzcASJVAaeWHbIIh
- drive_type: business
Keep this "onedrive" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Current remotes:

Name Type
==== ====
onedrive onedrive

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


Kết nối Dropbox:
root@ubuntu-s-2vcpu-4gb-sgp1-01:~# rclone config
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> n

Enter name for new remote.
name> dropbox

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
...
13 / Dropbox
\ (dropbox)
...
18 / Google Drive
\ (drive)
...
31 / Microsoft OneDrive
\ (onedrive)
...
Storage> 13

Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id> Bỏ trống

Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> Bỏ trống

Edit advanced config?
y) Yes
n) No (default)
y/n> n

Use web browser to automatically authenticate rclone with remote?
* Say Y if the machine running rclone has a web browser you can use
* Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes (default)
n) No
y/n> n

Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
rclone authorize "dropbox"
Then paste the result.
Enter a value.
config_token> {"access_token":"sl.Bi41EZc...AAAYdu0PCuUcu4DApdY9Hxrnbltkz-SIewjlcQ4dOdz2ao","expiry":"2023-07-26T09:43:40.462984+07:00"}

Configuration complete.
Options:
- type: dropbox
- token: {"access_token":"sl.Bi41EZc...AAAYdu0PCuUcu4DApdY9Hxrnbltkz-SIewjlcQ4dOdz2ao","expiry":"2023-07-26T09:43:40.462984+07:00"}
Keep this "dropbox" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Current remotes:

Name Type
==== ====
dropbox dropbox

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

BƯỚC 3. SYNC, COPY

sync là rclone sẽ tự động so sánh file ở cloud đích với cloud nguồn, nếu có sự khác biệt (bao gồm cả thời gian sửa) thì tệp ở cloud đích sẽ bị thay thế, sync cũng tự động xóa bỏ và thêm mới các file được cập nhật ở cloud nguồn.
rclone sync --progress --transfers=10 drive:Backup dropbox:Backup

Lệnh này sẽ đồng bộ thư mục Backup trên Google Drive với thư mục Backup trên Dropbox. Trong đó:

  • drive:, dropbox: là các tên cloud, thằng đứng trước là cloud nguồn, đứng sau là cloud đích, thay bằng cloud bạn muốn.
  • Backup là tên folder, thay bằng thư mục hoặc đường dẫn thư mục bạn muốn đồng bộ, đặt trong dấu “” nếu tên folder chứa dấu cách hoặc ký tự tiếng Việt
  • progress: hiển thị tiến trình
  • transfers=10: thực hiện chuyển 10 file cùng lúc, bạn có thể đặt giá trị này cao hơn, nhưng có thể bị cloud từ chối do có quá nhiều kết nối cùng lúc, thử nghiệm cho thấy với OneDrive, chỉ nên đặt transfers=5.
Giữ cửa sổ ZOC chạy, tránh để máy tính sleep tự động. Trường hợp bạn muốn chạy trong nền (để không phải bật máy tính và giữ kết nối SSH) thì dùng nohup:

nohup rclone sync --progress --transfers=10 drive:Backup dropbox:Backup > log.txt &

Minh họa trường hợp đang chuyển từ Google Drive sang Dropbox, chuyển 3TB mất khoảng hơn 4 tiếng, tốc độ trung bình trên 200MiB/s:

root@ubuntu:~# rclone sync --progress --transfers=20 drive:Backup dropbox:Backup
Transferred: 2.828 TiB / 2.966 TiB, 95%, 209.254 MiB/s, ETA 12m37s
Transferred: 3956 / 4345, 91%
Elapsed time: 4h15m7.0s


1700029454851.png

1700029461131.png


Sau này, khi ta có dữ liệu cần đồng bộ ít hơn, chỉ cần thiết lập 1 VPS cỡ 5$/tháng với 1TB Bandwidth là đủ để sync.

Trong khi đó với lệnh copy + cờ update, rclone sẽ chỉ so sánh tên, thêm mới các file có ở cloud nguồn mà chưa có ở cloud đích.
rclone copy --update --progress --transfers=10 drive:Backup dropbox:Backup

Một lưu ý khi bạn chạy lệnh đồng bộ, thằng OneDrive sẽ tự động tạo ra các version history và nó tính vào dung lượng chung, mình đã từng bị lố hơn 1TB, chạy lệnh sau để xóa hết các version history, chỉ giữ lại bản mới nhất:

rclone -i cleanup onedrive:
Ảnh bìa: nphunghung.com
Sưu tầm