rclone và các ổ đĩa đám mây

RPi được dùng nhiều như một máy tính không màn hình, vì vậy nó thích hợp với những ứng dụng được gọi chạy theo dòng lệnh.

rclone là ứng dụng chạy từ dòng lệnh để sync files và directories đến/từ:

  • Amazon Drive
  • Amazon S3
  • Backblaze B2
  • Box
  • Ceph
  • DigitalOcean Spaces
  • Dreamhost
  • Dropbox
  • FTP
  • Google Cloud Storage
  • Google Drive
  • HTTP
  • Hubic
  • IBM COS S3
  • Memset Memstore
  • Microsoft Azure Blob Storage
  • Microsoft OneDrive
  • Minio
  • Nextloud
  • OVH
  • Openstack Swift
  • Oracle Cloud Storage
  • Ownloud
  • pCloud
  • put.io
  • QingStor
  • Rackspace Cloud Files
  • SFTP
  • Wasabi
  • WebDAV
  • Yandex Disk
  • The local filesystem


Với các đặc điểm

  • MD5/SHA1 hashes checked at all times for file integrity
  • Timestamps preserved on files
  • Partial syncs supported on a whole file basis
  • Copy mode to just copy new/changed files
  • Sync (one way) mode to make a directory identical
  • Check mode to check for file hash equality
  • Can sync to and from network, eg two different cloud accounts
  • Optional encryption (Crypt)
  • Optional cache (Cache)
  • Optional FUSE mount (rclone mount)

Cài đặt trên RPi qua dòng lệnh

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

Sau đó cấu hình rclone qua dòng lệnh (thí dụ với Google Drive)

rclone config

 

2018/04/25 06:09:14 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> gdrive
Type of storage to configure.
Choose a number from below, or type in your own value
 1 / Alias for a existing remote
 \ "alias"
 2 / Amazon Drive
 \ "amazon cloud drive"
 3 / Amazon S3 (also Dreamhost, Ceph, Minio, IBM COS)
 \ "s3"
 4 / Backblaze B2
 \ "b2"
 5 / Box
 \ "box"
 6 / Cache a remote
 \ "cache"
 7 / Dropbox
 \ "dropbox"
 8 / Encrypt/Decrypt a remote
 \ "crypt"
 9 / FTP Connection
 \ "ftp"
10 / Google Cloud Storage (this is not Google Drive)
 \ "google cloud storage"
11 / Google Drive
 \ "drive"
12 / Hubic
 \ "hubic"
13 / Local Disk
 \ "local"
14 / Microsoft Azure Blob Storage
 \ "azureblob"
15 / Microsoft OneDrive
 \ "onedrive"
16 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
 \ "swift"
17 / Pcloud
 \ "pcloud"
18 / QingCloud Object Storage
 \ "qingstor"
19 / SSH/SFTP Connection
 \ "sftp"
20 / Webdav
 \ "webdav"
21 / Yandex Disk
 \ "yandex"
22 / http Connection
 \ "http"
Storage> 11
Google Application Client Id - leave blank normally.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>
Scope that rclone should use when requesting access from drive.
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
ID of the root folder - leave blank normally. Fill in to access "Computers" folders. (see docs).
root_folder_id> 120WemuVoZJ...xya
Service Account Credentials JSON file path - leave blank normally.
Needed only if you want use SA instead of interactive login.
service_account_file>
Remote config
Use auto config?
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine or Y didn't work
y) Yes
n) No
y/n> n
If your browser doesn't open automatically go to the following link: https://accounts.google.com/o/oauth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3Aietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&state=bb2d1...
Log in and authorize rclone for access
Enter verification code> 4/AADSM48NWt_abc...
Configure this as a team drive?
y) Yes
n) No
y/n> n
--------------------
[gdrive]
type = drive
client_id =
client_secret =
scope = drive
root_folder_id = 120...
service_account_file =
token = {"access_token":"..."}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
Current remotes:

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

 

  • Tên project mặc định là remote, ám chỉ ta đang làm việc với ổ đĩa từ xa, chấp nhận tên này.
  • Chọn mục 11 là google drive
  • Enter 2 lần để chấp nhận mặc định 2 mục kế tiếp
  • Chọn 1 để rclone quản lý cà ổ đĩa google drive
  • Enter 2 lần để chấp nhận mặc định 2 mục kế tiếp
  • Chọn No để tự lấy mã chứng thực vì RPi đang chạy ở console, không có browser. Copy url và chạy ở browser ngoài Windows rồi sao chép mã chứng thực vào màn hình config của RPi
  • Làm theo hướng dẫn tiếp theo cho đến khi kết thúc bằng phím q (quit)

Sử dụng

  • Sao chép: mặc định rclone mở 4 luồng để tăng tôc độ sao chép
rclone copy nguồn đích [flags]
rclone copy /home/arun/Documents/linoxide/ remote:linoxide --transfers 10 --checkers 16
rclone copy remote:picture /mnt/picture
  • Đồng bộ: mặc định xử lý 4 file song song, test với tham số –dry-run
rclone sync source:path dest:path [flags]
rclone sync /home/arun/Documents/linoxide/ remote:linoxide --update --transfers 10 --checkers 16
  • Liệt kê:
#liệt kê file
rclone ls remote:
#liệt kê với size
rclone lsl remote:
#liệt kê thư mục
rclone lsd remote:
  • Thư mục:
#Tạo thư mục
rclone mkdir remote:dirname
#Xóa thư mục rỗng
rclone rmdir remote:dirname
#Xóa thư mục và file bên trong
rclone purge remote:dirname
  • Check: kiểm tra sự giống nhau của nguồn và đích qua size và md5
rclone check /home/linux.tar remote:/linoxide/linux.tar
  • md5sum: Tạo md5 của mọi đối tượng ở đích
rclone md5sum remote:/linoxide/
  • Mount ổ đĩa Google: cần phải có fuse
apt-get install fuse
#mount
rclone mount remote: /mnt/gdrive &
#unmount
fusermount -u /mnt/gdrive

Ứng dụng

  • Backup: tất nhiên rồi! Chúng ta có thể cho inotify giám sát một vài thư mục, khi có sự thay đổi nội dung thì gọi rclone cập nhật bản sao lưu.
inotifywait -m -e close_write --format '%w%f' /mnt/picture | while read file; do rclone copy "$file" remote:backup; done

rclone cũng có bản chạy trên Windows, MacOS

  • Dùng như thư mục dữ liệu cho website: imgur cho phép lưu trữ ảnh và trả lại url mỗi khi ảnh được upload lên website, nhưng đôi khi chúng ta cần upload file không phải là ảnh. Chọn một cloud service khác và dùng rclone

Chú thích

  • Hôm nay đứt cáp APG, tốc độ upload ảnh là 100GB trong 10 giờ. Tuy nhiên vì RPi chạy 24 giờ/ngày nên tốc độ này là chấp nhận được. Upload lên Flickr tốc độ cũng tương tự…
  • Trong lúc upload kho ảnh/tài liệu lớn, đề phòng rclone ngừng chạy vì lý do nào đó, thêm vào crontab dòng
*/30 * * * * [ -z "$(pidof rclone)" ] && rclone copy from_folder remote:to_folder
  • Sau khi upload xong thì cài đặt crontab cập nhật dữ liệu
@reboot /usr/bin/inotifywait -m -e close_write --format '%w%f' from_folder | while read file; do rclone copy "$file" remote:to_folder --update; done
  • rclone không chiếm dụng quá nhiều tài nguyên, khoảng 45% của 4 x 100% CPU và 120/1024MB Ram (nghĩa là rclone dùng hơn 10% trên mỗi CPU)
  • Google không giới hạn dung lượng googledrive cho các email của tên miền edu.

Comments Off on rclone và các ổ đĩa đám mây

Filed under Software

Comments are closed.