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.
Xem thêm Rcolone Command tại đây
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@sv1 ~]# rclone config 2020/01/12 14:08:20 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 <=== 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 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 \ "drive" 14 / Google Photos \ "google photos" 15 / Hubic \ "hubic" 16 / JottaCloud \ "jottacloud" 17 / Koofr \ "koofr" 18 / Local Disk \ "local" 19 / Mail.ru Cloud \ "mailru" 20 / Mega \ "mega" 21 / Microsoft Azure Blob Storage \ "azureblob" 22 / Microsoft OneDrive \ "onedrive" 23 / OpenDrive \ "opendrive" 24 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH) \ "swift" 25 / Pcloud \ "pcloud" 26 / Put.io \ "putio" 27 / QingCloud Object Storage \ "qingstor" 28 / SSH/SFTP Connection \ "sftp" 29 / Transparently chunk/split large files \ "chunker" 30 / Union merges the contents of several remotes \ "union" 31 / Webdav \ "webdav" 32 / Yandex Disk \ "yandex" 33 / http Connection \ "http" 34 / premiumize.me \ "premiumizeme" Storage> 13 <== Mình chọn Backup lên Google Drive nên chọn 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> 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 team drive? y) Yes n) No y/n> n <== Không cấu hình Drive team -------------------- [linuxcanban] type = drive scope = drive token = {"access_token":"ya29.Il-5B3Cf1cER-SkNt9ZpVFwh8JRSCt2LD1xPRrmdeLTbDtln7pdh4n52L13woSRXlOmQLI98ZyJNhapHT0fyFsr7JoNNbKbAypBezrsBGHJB8b3hLi4O_B4AkYfhdZ6BKw","token_type":"Bearer","refresh_token":"1//0gScsRSNDBrgOCgYIARAAGBASNwF-L9IrczKgrl3T5napN5h_0lbmbpTHuV_1FPPA4VvtoDB7BLQ7irqEFOg_Z2aFvJ4iStgHM-E","expiry":"2020-01-12T15:21:11.384653782+07:00"} -------------------- y) Yes this is OK e) Edit this remote d) Delete this remote y/e/d> y <== Xác nhận Current remotes: Name Type ==== ==== linuxcanban 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 <== Thoát khỏi giao diện cấu hình
Bạn có thể kiểm tra thử xem đã hoạt động chưa bằng lệnh sau
rclone lsd linuxcanban:
Output [root@sv1 ~]# rclone lsd linuxcanban: -1 2020-01-12 14:49:50 -1 linuxcanban
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 linuxcanban, 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:linuxcanban.com
Output [root@sv1 ~]# rclone lsd remote:linuxcanban.com -1 2020-01-12 14:59:14 -1 2020-01-12
#!/bin/bash #Auth: Team Linux Can Ban SERVER_NAME=linuxcanban.com ## 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="s6faahjqHo66fpL" ## Nhap Pass Root MYSQL cua ban vao MYSQLDUMP=/usr/bin/mysqldump SECONDS=0 mkdir -p "$BACKUP_DIR/mysql" echo "Linuxcanban.com 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 "Linuxcanban.com Backup Database Successful"; echo ''; echo "Linuxcanban.com 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 "Linuxcanban.com 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."
Backup dữ liệu thành công, bạn mở Google Drive lên kiểm tra nhé
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:/linuxcanban/2020-12-01" /home/linuxcanban.com/public_html/
Lệnh trên sẽ copy folder 2020-12-01
trong thư mục linuxcanban
trên Cloud về thư mục /home/linuxcanban.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.