网站备份:使用Rclone进行备份到Google Drive

主要备份的是文件目录和mysql数据库。

前期准备

apt install -y unzip openssl curl

下载并安装Rclone

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

配置Rclone – 主要参考外馆

获取自己的(本馆档案 )google drive client id & client secret

rclone config
# 给Full access all files
# 如想要性能则填入自己的client_id & secret;不然默认
# root_folder_id留空
itlwin at ~ ❯ rclone config
2020/05/04 10:07:12 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_omgd

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
...
13 / Google Drive
   \ "drive"
...

Storage> 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> 207780074259-your-own-client-id.apps.googleusercontent.com

Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").

client_secret> 83Jy-your-own-secret-CsGyXh

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.

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>
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>
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=207780074259--your-own-client-id.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=xxxx
Log in and authorize rclone for access

Enter verification code> 4/zQEo8---shown-on-screen---CecDZoakJKg

Configure this as a team drive?
y) Yes
n) No (default)

y/n> y

Fetching team drive list...
No team drives found in your account--------------------
[gdrive_omgd]
type = drive
client_id = 207780074259-your-own-client-id.apps.googleusercontent.com
client_secret = 83J-your-own-secret-GyXh
scope = drive
token = {"access_token":"ya29.your-own-access-token-ipLb-rg-YroD_51ifEc0VMI","token_type":"Bearer","refresh_token":"1//03QEh9JYbbPZgCgYIARAAGAMSNwF-your-own-refersh-token-A048Ng","expiry":"2020-05-04T11:20:37.69167538+02:00"}
team_drive =
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote

y/e/d> y

Current remotes:

Name                 Type
====                 ====
gdrive_omgd          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
Config到最后就长这样子

验证: rclone about gdrive_omgd:

下载自动备份脚本,主要参考这里

wget --no-check-certificate https://github.com/teddysun/across/raw/master/backup.sh && chmod +x backup.sh
## 修改配置文件。最重要的就是 -
# ENCRYPTFLG BACKUPPASS MYSQL_ROOT_PASSWORD BACKUP[0]
# RCLONE_NAME RCLONE_FOLDER
## 一定要修改,不然Rclone不运行
# RCLONE_FLG=true
## 其他不用管了
vim ./backup.sh

运行自动备份

./backup.sh

运行成功了。

每周自动运行

# verify cron service is running
systemctl status cron
# 如有没有安装,则
apt install -y cron && systemctl enable cron
# list out cron
# system wide
vim /etc/crontab
# personal cron: crontab -e 
# list cron jobs of current user
crontab -l
#m h dom mon dow user  command
# run every wed 0110am
10 1 * * 3  /root/backup.sh

恢复解压的方法

# 先解密
openssl enc -aes256 -in [ENCRYPTED BACKUP] -out decrypted_backup.tgz -pass pass:[BACKUPPASS] -d -md sha1
# 再解压
tar -zxPf [DECRYPTION BACKUP FILE]
## 解释一下参数 -P:
# tar 压缩文件默认都是相对路径的。加个 -P 是为了 tar 能以绝对路径压缩文件。因此,解压的时候也要带个 -P 参数。

3 thoughts on “网站备份:使用Rclone进行备份到Google Drive”

  1. Pingback: Rclone技巧 & 加载Google Drive到本地目录 – 1024 & 4201

  2. Pingback: VPS上部署Aria2+Rclone自动移到GoogleDrive+GoIndex/Cloudflare – 1024 & 4201

  3. Pingback: 设立两个Google Drive之间的同步 – 1024 & 4201

Leave a Reply