LINUXTHỦ THUẬT HAYWINDOWS

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

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. Bài viết này sẽ hướng dẫn sử dụng 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. Hoặc ai chơi tiny PC hoặc Raspberry Pi mình thấy cũng ổn.

Truy cập Vultr (truy cập vào link này để ủng hộ mình kiếm thêm Referral, nếu không thích thì bạn vào thẳng trang chủ 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. Vultr vẫn đang có khuyến mãi tặng 100$ để dùng thử trong 14 ngày cho người dùng mới, đăng ký bằng link nào cũng được khuyến mại này.

Video dưới đây là thao tác tạo VPS có cấu hình 2vCPU, 4GB RAM và 5TB bandwidth:

Tạo VPS xong đợi vài phút sẽ có email gửi về báo thành công, bạn click vào VPS là thấy:

  • Username: root
  • Password: ******
NOTE:
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

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

Đây là trình kết nối SSH mình đang sử dụng, có cả phiên bản cho macOS và Windows. Tải về tại termius.com/download/, cài đặt như các phần mềm thông thường. Ai có mail edu thì đăng ký Github Edu và đăng ký thằng này được dùng như gói pro nhé.

Trên giao diện Termius:

  1. Chọn NEW HOST
  2. Nhập tên tại mục Label để dễ quản lý
  3. Copy IP của VPS trong Vultr và paste vào mục Address
  4. Nhập Port 22 hoặc bỏ trống cũng được, nó tự nhận Port 22
  5. Username là root
  6. Password thì copy trong Vultr và paste vào.

Sau đó click đúp vào tên VPS vừa đặt tại mục Hosts để connect vào VPS

2/ Kết nối cloud

Ta sẽ chạy lệnh trên giao diện của Termius, 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 -y

Bước tiếp theo, cài đặt Rclone:

sudo -v ; curl https://rclone.org/install.sh | sudo bash

Để 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 Termius. Đọ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://vnrom-my.sharepoint.com/personal/backup_vnrom_net/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:~# 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

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ổ Termius 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
TIP:
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:

Chia sẻ: Lục Ngọc

Có thể bạn chưa biết

Nhận thông báo qua email
Nhận thông báo cho
guest

0 Bình luận
Phản hồi nội tuyến
Xem tất cả bình luận