Cấu hình tự động Backup và Upload lên Cloud

Bài viết tham khảo và trích dẫn từ: https://hocvps.com/rclone/, cảm ơn tác giả

1. Cài đặt Rclone

Rclone là một công cụ đồng bộ hóa dữ liệu tương tự Rsync nhưng lại được tập trung phát triển chức năng kết nối với các dịch vụ lưu trữ đám mây.

Rclone là một chương trình dòng lệnh nên mình sẽ down 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.

Chú ý : Nếu bạn đã cài đặt phiên bản cũ, cũng chỉ cần chạy chuỗi lệnh dưới để update. Tham khảo Changelog các phiên bản. Và các Commands của Rclone trước khi thực hiện các thao tác liên quan Rclone.

– Cài đặt với phiên bản hệ điều hành Linux 64bit
cd /root/
wget https://downloads.rclone.org/rclone-v1.39-linux-amd64.zip
unzip rclone-v1.39-linux-amd64.zip
\cp rclone-v*-linux-amd64/rclone /usr/sbin/
rm -rf rclone-*

– Cài đặt với phiên bản hệ điều hành Linux 32bit
cd /root/
wget https://downloads.rclone.org/rclone-v1.39-linux-386.zip
unzip rclone-v1.39-linux-386.zip
\cp rclone-v*-linux-386/rclone /usr/sbin/
rm -rf rclone-*

Link download trực tiếp các phiên bản Rclone.

Giờ bạn có thể sử dụng lệnh rclone để xem thêm thông tin sử dụng.

2. Tạo kết nối với Google Drive

Đầu tiên chúng ta sẽ cấu hình kết nối Rclone với Google Drive, việc này chỉ phải làm 1 lần duy nhất. Kết nối được tạo tên remote

Kết nối SSH với VPS rồi chạy lệnh:
rclone config
Các bước thực hiện đại khái như thế này:

[root@vpsxxxxx ~]# rclone config
2018/01/07 23:34:35 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defauls
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
name> remote
Type of storage to configure.
Choose a number from below, or type in your own value
1 / Amazon Drive
\ "amazon cloud drive"
2 / Amazon S3 (also Dreamhost, Ceph, Minio)
\ "s3"
3 / Backblaze B2
\ "b2"
4 / Box
\ "box"
5 / Cache a remote
\ "cache"
6 / Dropbox
\ "dropbox"
7 / Encrypt/Decrypt a remote
\ "crypt"
8 / FTP Connection
\ "ftp"
9 / Google Cloud Storage (this is not Google Drive)
\ "google cloud storage"
10 / Google Drive
\ "drive"
11 / Hubic
\ "hubic"
12 / Local Disk
\ "local"
13 / Microsoft Azure Blob Storage
\ "azureblob"
14 / Microsoft OneDrive
\ "onedrive"
15 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
\ "swift"
16 / Pcloud
\ "pcloud"
17 / QingCloud Object Storage
\ "qingstor"
18 / SSH/SFTP Connection
\ "sftp"
19 / Webdav
\ "webdav"
20 / Yandex Disk
\ "yandex"
21 / http Connection
\ "http"
Storage> 10
Google Application Client Id - leave blank normally.
client_id>
Google Application Client Secret - leave blank normally.
client_secret>
Service Account Credentials JSON file path - needed only if you want use SA instead of interactive
ogin.
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/
auth2/auth?access_type=offline&client_id=202264815644.apps.googleusercontent.com&redirect_uri=urn%3
ietf%3Awg%3Aoauth%3A2.0%3Aoob&response_type=code&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdr
ve&state=xxxxxxxxxxxxxxxxxxxxxxxxxx
Log in and authorize rclone for access
Enter verification code> x/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Configure this as a team drive?
y) Yes
n) No
y/n> n
--------------------
[remote]
client_id =
client_secret =
service_account_file =
token = {"access_token":"ya29.xxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxxx_xxxxxxxxxxxxxxx-xxxxxxxxxxxxxxx_xxxxxxxxxxxxxxx","token_type":"Bearer","refresh_token":"x/xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx__xxxxx","expiry":"2018-01-08T00:38:28.231514165+07:00"}
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y
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

Vậy là xong, giờ bạn có thể test với lệnh liệt kê thư mục trong kết nối remote:
rclone lsd remote:

3. Tạo shell script thực hiện backup và upload

– Tạo file backup.sh ở thư mục /root/
nano /root/backup.sh
– Copy toàn bộ nội dung script bên dưới rồi paste vào

#!/bin/bash
SERVER_NAME=VIETMETA_VPS_BACKUP
TIMESTAMP=$(date +"%Y-%m-%d_%H-%M")
BACKUP_DIR="/root/backup/$TIMESTAMP"
MYSQL_USER="root"
MYSQL=/usr/bin/mysql
MYSQL_PASSWORD="theMySQLPassword"
MYSQLDUMP=/usr/bin/mysqldump
SECONDS=0

mkdir -p "$BACKUP_DIR/mysql"

echo "Starting Backup Database";
databases=`$MYSQL --user=$MYSQL_USER --password=$MYSQL_PASSWORD -e "SHOW DATABASES;" | grep -Ev "(Database|information_schema|performance_schema|mysql)"`

for db in $databases; do
$MYSQLDUMP --user=$MYSQL_USER --password=$MYSQL_PASSWORD --force --opt --databases $db | gzip > "$BACKUP_DIR/mysql/$db.gz"
done

echo "Finished";
echo '';

echo "Starting Backup Website";
for U in /home/*; do
if [ -d "${U}" ]; then #If a directory
user=${U##*/} # User name
for D in /home/$user/domains/*; do
if [ -d "${D}/public_html" -a $user != "res" ]; then #If a directory 
domain=${D##*/} # Domain name
mkdir -p "$BACKUP_DIR/user/$user/"
echo "- /home/"$user"/domains/"$domain;
zip -r $BACKUP_DIR/user/$user/$domain.zip /home/$user/domains/$domain/public_html -q -x /home/$user/domains/$domain/public_html/wp-content/cache/**\* #Exclude cache
fi
done
fi
done

echo "Finished";
echo '';

#echo "Starting Backup Nginx Configuration";
#cp -r /etc/nginx/conf.d/ $BACKUP_DIR/nginx/
#echo "Finished";
#echo '';

size=$(du -sh $BACKUP_DIR | awk '{ print $1}')

echo "Starting Uploading Backup";
/usr/sbin/rclone move $BACKUP_DIR "remote:$SERVER_NAME/$TIMESTAMP" >> /var/log/rclone.log 2>&1
# Clean up
rm -rf $BACKUP_DIR
/usr/sbin/rclone -q --min-age 2w delete "remote:$SERVER_NAME" #Remove all backups older than 2 week
/usr/sbin/rclone -q --min-age 2w rmdirs "remote:$SERVER_NAME" #Remove all empty folders older than 2 week
echo "Finished";
echo '';

duration=$SECONDS
echo "Total $size, $(($duration / 60)) minutes and $(($duration % 60)) seconds elapsed."

– Nhấn Ctrl+O, Enter để lưu và Ctrl+X để thoát.

– Phân quyền cho script
chmod +x /root/backup.sh
– Vậy là xong rồi đó, giờ bạn có thể test lại bằng cách chạy lệnh:
/root/backup.sh
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 lsl remote:VIETMETA_VPS_BACKUP
Giờ mình sẽ cho script tự động chạy lúc 2h00 sáng.
EDITOR=nano crontab -e
Dán nội dung sau vào cửa sổ Terminal
0 2 * * * /root/backup.sh > /dev/null 2>&1
Nhấn Ctrl+O, Enter để lưu và Ctrl+X để thoát

Vậy là xong, cứ 2h sáng hàng ngày script sẽ tự động chạy, backup toàn bộ dữ liệu của VPS rồi upload lên Cloud. Đồng thời, dữ liệu backup trên VPS sẽ được xóa luôn sau khi upload xong.

Mr.Phan

KTS, KSXD, Developer

You may also like...

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *