
Giới thiêu
Rclone có thể nói là một giải pháp Backup dữ liệu đồng bộ lên đám mây hiệu quả giúp dữ liệu của bạn luôn được backup định kỳ và lưu giữ an toàn tuyệt đối. Ở bài viết này mình sẽ hướng dẫn bạn thực hiện từng bước thật chi tiết từ việc cài đặt và chuyển dữ liệu lên Cloud, ở đây mình sử dụng Goolge Drive làm nơi lưu trữ. Rclone giúp bạn tiết kiệm khá nhiều rủi ro và chi phí mua thêm dữ liệu backup.
Cài đặt Rclone
Xem phiên bản mới nhất được phát hành tại trang chủ của Rclone tại đây
Ở đây mình cài đặt Rclone với HĐH Linux 64bit và là bản Rclone mới nhất v1.50.2. Rclone là một chương trình dòng lệnh nên bạn hãy download xuống rồi di chuyển file chạy đến thư mục /usr/sbin/
của VPS để sử dụng sau này.
cd /root/
wget https://downloads.rclone.org/v1.50.2/rclone-v1.50.2-linux-amd64.zip
unzip rclone-v*.zip
cp rclone-v*-linux-amd64/rclone /usr/sbin/
Các lệnh Rclone thường dùng
- rclone config – Cấu hình kết nối đến dịch vụ cloud.
- rclone copy – Copy files từ server đến cloud, skip nếu đã tồn tại dữ liệu.
- rclone sync – Synchronize giữa server và cloud, chỉ update dữ liệu trên cloud mà thôi.
- rclone move – Move files từ server lên cloud.
- rclone delete – Xóa dữ liệu của folder.
- rclone purge – Xóa dữ liệu của folder và toàn bộ nội dung bên trong.
- rclone mkdir – Tạo folder.
- rclone rmdir – Xóa folder trống tại đường dẫn.
- rclone rmdirs – Xóa toàn bộ folder trống tại đường dẫn. Bộ đếm thời gian bao gồm:
- ms – Milliseconds
- s – Seconds
- m – Minutes
- h – Hours
- d – Days
- w – Weeks
- M – Months
- y – Years
- rclone check – Kiểm tra dữ liệu server và cloud có đồng bộ hay không.
- rclone ls – Liệt kê toàn bộ dữ liệu gồm size và path.
- rclone lsd – Liệt kê toàn bộ thư mục.
- rclone lsl – Liệt kê toàn bộ dữ liệu gồm modification time, size và path.
- rclone size – Trả về kích thước thư mục.
Tạo Backup lên Cloud
Bây giờ bạn hãy chạy lệnh rclone config và làm theo các bước như bên dưới, bạn chú ý những chữ mình tô màu đỏ nhé
[root@ ~]# rclone config
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n <=== N để tạo mới
name> remote <=== Đặt tên cho backup
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 Providers including AWS, Alibaba, Ceph, Digital Ocean, Dreamhost, IBM COS, Minio, SeaweedFS, and Tencent COS
\ "s3"
5 / Backblaze B2
\ "b2"
6 / Box
\ "box"
7 / Cache a remote
\ "cache"
8 / Citrix Sharefile
\ "sharefile"
9 / Compress a remote
\ "compress"
10 / Dropbox
\ "dropbox"
11 / Encrypt/Decrypt a remote
\ "crypt"
12 / Enterprise File Fabric
\ "filefabric"
13 / FTP Connection
\ "ftp"
14 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
15 / Google Drive
\ "drive"
Storage> 15 <== Mình chọn Backup lên Google Drive nên chọn 15
** 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> Không nhập gì cả, Nhấn Enter để sử dụng mặc định
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret> Không nhập gì cả, Nhấn Enter để sử dụng mặc định
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 <== Chọn 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> Không nhập gì cả, Nhấn Enter để sử dụng mặc định
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 ("").
service_account_file> Không nhập gì cả, Nhấn Enter để sử dụng mặc định
Edit advanced config? (y/n)
y) Yes
n) No
y/n> n <== Chọn n Để không tùy chỉnh nâng cao, chỉ sử dụng cơ bản
Remote config
Use auto config?
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes
n) No
y/n> n <== Chọ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=mtDve3JCqeqDPrGMEKy4Ng
Log in and authorize rclone for access
## Bạn copy đường Link màu xanh trên vào trình duyệt và theo theo các bước ở trình duyệt. Sau khi get được mã bạn dán vào như bên dưới
Enter verification code> 4/vQEo6HRbnnZb98tetr0ZpNb28a6sf8e84fHfT65wfBTz8xvbKUAE6EI
Configure this as a Shared Drive (Team Drive)?
y) Yes
n) No (default)
y/n> n <= Chọn N để không cấu hình Drive team
--------------------
[da]
type = drive
scope = drive
token = {"access_token":"ya29.a0ARrdaM_z8fvjB4QmGeNmNyAOgjGtRj773tnXBZMa5AiT47npTjQEBXgNxv4B8HhNEO6ADh6PEX8kTjQ_J2pWdzgYpCSLpOJDxq8EJ5DNkgP3i85YyJiu476fL6ImPzmoR1LhC9A3xl6zsTqo5aYSWMBruZPW","token_type":"Bearer","refresh_token":"1//0g6QqPuoh3HBaCgYIARAAGBASNwF-L9Ir9dhX4soUISXlchHljWi1cYF2ybXySMDW19pmWemjtj7SyCXQ6uGzh4PnbJZv4pU_KP0","expiry":"2020-01-12T15:21:11.384653782+07:00"}
team_drive =
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y <= Chọn Y để đồng ý
Current remotes:
Name Type
==== ====
remote 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 <= Chọn q để thoát
root@:~#
Tạo Script Backup lên Cloud
- Script hoạt động với Rclone phiên bản 1.35 trở lên.
- Kết nối Rclone với Cloud tên remote, nếu bạn sử dụng kết nối khác thì thay đổi tên trong script.
- Tạo file Rclonebackup.sh ở thư mục /root/
- vi /root/Rclonebackup.sh
- Copy toàn bộ nội dung script bên dưới rồi paste vào
Sau đó bạn phân quyền thực thi cho script bằng lệnh chmod +x Rclonebackup.sh
Để thực hiện bạn chạy script với lệnh sau /root/Rclonebackup.sh
Bây giờ bạn thử kiểm tra trên Cloud xem có thư mục mới với dữ liệu backup chưa nhé, hoặc test với lệnh rclone lsd remote:vpscanban
Output
[root@sv1 ~]# rclone lsd remote:remote
-1 2022-06-01 14:59:14 -1 2022-06-01
Script auto Backup
#!/bin/bash
SERVER_NAME=vpscanban ## Tên thư mục trong Google Drive
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/root/backup/$TIMESTAMP"
MYSQL_USER="root"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="123456789@@" ## Nhap Pass Root MYSQL cua ban vao
MYSQLDUMP=/usr/bin/mysqldump
SECONDS=0
mkdir -p "$BACKUP_DIR/mysql"
echo "Backup Database In Process";
databases=`$MYSQL --user=$MYSQL_USER -p$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`
for db in $databases; do
$MYSQLDUMP --force --opt --user=$MYSQL_USER -p$MYSQL_PASSWORD $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done
echo "Backup Database Successful";
echo '';
echo "Backup Website In Process";
# Loop through /home directory
for D in /home/*; do
if [ -d "${D}" ]; then #If a directory
domain=${D##*/} # Domain name
echo "- "$domain;
zip -r $BACKUP_DIR/$domain.zip /home/$domain/public_html/ -q -x /home/$domain/public_html/wp-content/cache/**\* #Exclude cache
fi
done
echo "Backup Website Successful";
echo '';
echo "Dang tien hanh Backup len Google Drive";
/usr/sbin/rclone move $BACKUP_DIR "remote:$SERVER_NAME/$TIMESTAMP" >> /var/log/rclone.log 2>&1 #remote: remote config name created in previous step.
# Clean up
rm -rf $BACKUP_DIR #Delete backup directory on VPS
/usr/sbin/rclone -q --min-age 2w delete "remote:$SERVER_NAME" #Remove all backups older than 2 week on Google Drive Backup Directory
/usr/sbin/rclone -q --min-age 2w rmdirs "remote:$SERVER_NAME" #Remove all empty folders older than 2 week Google Drive Backup Directory
/usr/sbin/rclone cleanup "remote:" #Cleanup Trash , Delete all files in trash directory
echo "Da dua tat ca du lieu len Gooogle Drive hoan tat";
echo '';
duration=$SECONDS
echo "Total $size, $(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."
Tạo Crontab Auto Backup
Bạn nhập lệnh crontab -e
sau đó nhấn phím i hoặc Insert và nhập vào lịch sau 0 1 * * * /root/Rclonebackup.sh > /dev/null 2>&1
. Lịch này là cứ đúng 1 giờ sáng hằng ngày sẽ tự động chạy Backup move Google Drive.
Dữ liệu có thể nói là một tài sản vô cùng quý giá và với bài này mình mong các bạn sẽ sao lưu được dữ liệu của mình và bảo quản một cách tốt nhất.
Tải file backup từ Cloud xuống VPS
Cách đơn giản nhất để bạn khôi phục lại dữ liệu đó là tải file backup từ Cloud xuống máy tính, rồi tùy theo nhu cầu mà up trở lại lên VPS. Tuy nhiên, nếu muốn tải trực tiếp file backup về VPS, bạn có thể sử dụng luôn Rclone với câu lệnh copy.
rclone copy "remote:/vpscanban/2022-06-01" /home/vpscanban.com/public_html/
Lệnh trên sẽ copy folder 2022-06-01 trong thư mục vpscanban trên Cloud về thư mục /home/vpscanban.com/public_html/
của VPS. Tốc độ upload và download từ Cloud đều rất nhanh.
Sau khi copy dữ liệu backup về VPS, bạn tiến hành giải nén file zip, copy thư mục web và nginx về đúng vị trí đồng thời tiến hành import database.
Nếu bạn cần tôi hỗ trợ cấu hình Backup định kỳ xin hãy để lại thông tin tại Form bên dưới, tôi sẽ hỗ trợ bạn cấu hình