Home Linux Tutorials Cài đặt Rclone backup dữ liệu lên Google Drive

Cài đặt Rclone backup dữ liệu lên Google Drive

by Quốc Cường
52 views

Điều kiện tiên quyết

Tải các package cơ bản:

yum install wget unzip -y

Cài đặt Rclone

Xem và tải phiên bản mới nhất của Rclone tại đây

cd ~/
wget https://downloads.rclone.org/v1.53.3/rclone-v1.53.3-linux-amd64.zip

Giải nén file vừa tải về:

unzip rclone-v*.zip

Copy file thực thi rclone vào thư mục /usr/bin:

cp rclone-v*-linux-amd64/rclone /usr/sbin/

Cấu hình backup lên Google Drive

[[email protected] ~]# rclone config
2020/12/15 10:08:50 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
 ==> Nhập n để tạo mới cấu hình remote
name> systuts
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, Tencent COS, 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 / In memory object storage system.
   \ "memory"
17 / Jottacloud
   \ "jottacloud"
18 / Koofr
   \ "koofr"
19 / Local Disk
   \ "local"
20 / Mail.ru Cloud
   \ "mailru"
21 / Mega
   \ "mega"
22 / Microsoft Azure Blob Storage
   \ "azureblob"
23 / Microsoft OneDrive
   \ "onedrive"
24 / OpenDrive
   \ "opendrive"
25 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
26 / Pcloud
   \ "pcloud"
27 / Put.io
   \ "putio"
28 / QingCloud Object Storage
   \ "qingstor"
29 / SSH/SFTP Connection
   \ "sftp"
30 / Sugarsync
   \ "sugarsync"
31 / Tardigrade Decentralized Cloud Storage
   \ "tardigrade"
32 / Transparently chunk/split large files
   \ "chunker"
33 / Union merges the contents of several upstream fs
   \ "union"
34 / Webdav
   \ "webdav"
35 / Yandex Disk
   \ "yandex"
36 / http Connection
   \ "http"
37 / premiumize.me
   \ "premiumizeme"
38 / seafile
   \ "seafile"
Storage> 13
 ==> Chọn Google Drive
** 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> ==> 
Nhập Enter để bỏ qua.
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> 
==> 
Nhập Enter để bỏ qua.
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

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.

Enter a string value. Press Enter for the default ("").
root_folder_id> 
==> 
Nhập Enter để bỏ qua.
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 string value. Press Enter for the default ("").
service_account_file> 
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> 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 (default)
n) No
y/n> n

Please 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=L9LRuvZYSUI1CIOOJjwoqw

Log in and authorize rclone for access
Enter verification code>     

Copy đường link màu xanh và dán vào trình duyệt, chọn tài khoản Google Drive.

Lưu ý: cần đăng nhập vào tài khoản Google

Cài đặt Rclone backup dữ liệu lên Google Drive

Cho phép Rclone quyền truy cập vào Google Drive.

Cài đặt Rclone backup dữ liệu lên Google Drive

Copy đoạn code đăng nhập và paste vào command line.

Cài đặt Rclone backup dữ liệu lên Google Drive
Please 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=L9LRuvZYSUI1CIOOJjwoqw

Log in and authorize rclone for access
Enter verification code> 4/1AY0e-g5_vGJGhert4ow35ZvE2M6OIydXILuNzDFf4kifpbahoXl0evpQNAw       
Configure this as a team drive?
y) Yes
n) No (default)
y/n> n

--------------------
[systuts]
type = drive
scope = drive
token = {"access_token":"ya29.a0AfH6SMA7mJ2RRbLdUrRCEFHTGDGehdwgj2fskDdghkkCo7eb_SMI2dq616HaaK-lQg2uDZ_dIgL8wc98XJjZJ-DqDqOSfjeh43bf8ydjfhD4g3l3HNe0J1QihmEHFgdfhkia24fh9bafkdhAFJAF4RmXSV7YB8OsDptBhd13ak84ZD6-u0Xy4","token_type":"Bearer","refresh_token":"1//0eP7SSx0vyFDFgrhiihfiDfihSFJNwF-L9IrABXjwYnaN__ltcAXQp_X5q3yqQ2DVN28CQGrBR-xDfhkol39dfFhdkrMHW9mb_j7NTSMUB9SI","expiry":"2020-12-15T11:16:36.678494245+07:00"}
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y

Current remotes:

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

Sau khi hoàn tất, chúng ta hãy thử kiểm tra xem tài khoản Google Drive đã được liên kết với Rclone chưa bằng lệnh rclone lsd <remote-name>:

rclone lsd systuts:
Cài đặt Rclone backup dữ liệu lên Google Drive

Tạo script & thiết lập crontab tự động backup

Tạo file script autobackup.sh

vi /etc/cron.d/autobackup.sh

[expand title=”Nhấp vào đây để xem backup script” tag=”h4″]

#!/bin/bash
#Auth: systuts.net
 
SERVER_NAME=systuts ## Tên thư mục trong Google Drive 
 
TIMESTAMP=$(date +"%F")
BACKUP_DIR="/root/backup/$TIMESTAMP"
MYSQL_USER="username"  ## nhập username database

MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="your-password" ## nhập password database
MYSQLDUMP=/usr/bin/mysqldump
SECONDS=0
 
mkdir -p "$BACKUP_DIR/mysql"

echo "Database backup is in progress ...";
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 "Database backup successfully";
echo '';
 
echo "Website backup is 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 "Website backup successfully";
echo '';

echo "Synchronize data with 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 "All data is synced with Google Drive";
echo '';
 
duration=$SECONDS
echo "Total $size, $(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."

[/expand]

Phân quyền:

chmod +x /etc/cron.d/autobackup.sh

Lập lịch backup tự động:

crontab -e

Thêm vào:

0 1 * * * root /etc/cron.d/autobackup.sh > /dev/null 2>&1

Dưới đây là biểu đồ cách thiết lập thời gian chạy script

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

Tải file backup từ Google Drive về VPS:

Trong trường hợp các bạn muốn restore lại dữ liệu backup từ Google Drive, các bạn tải file backup bằng lệnh:

rclone copy "remote:/systuts/2020-12-15" /home/systuts.net/public_html/

Sau khi tải hoàn tất các bạn có thể sử dụng file backup này để restore vào hệ thống.

Chúc các bạn thành công.

0 comment

You may also like

Leave a Comment