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

网站所有资源都有,可联系VX:bjlyxy1688
图片[1]-网站备份:使用Rclone进行备份到Google Drive-天时网

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

前期准备

<span class="token function">apt</span> <span class="token function">install</span> -y <span class="token function">unzip</span> openssl <span class="token function">curl</span>
<span class="token function">apt</span> <span class="token function">install</span> -y <span class="token function">unzip</span> openssl <span class="token function">curl</span>
apt install -y unzip openssl curl

下载并安装Rclone

<span class="token function">curl</span> https://rclone.org/install.sh <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token function">bash</span>
<span class="token function">curl</span> https://rclone.org/install.sh <span class="token operator">|</span> <span class="token function">sudo</span> <span class="token function">bash</span>
curl https://rclone.org/install.sh | sudo bash

配置Rclone – 主要参考外馆

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

rclone config
<span class="token comment"># 给Full access all files</span>
<span class="token comment"># 如想要性能则填入自己的client_id & secret;不然默认</span>
<span class="token comment"># root_folder_id留空</span>
rclone config
<span class="token comment"># 给Full access all files</span>
<span class="token comment"># 如想要性能则填入自己的client_id & secret;不然默认</span>
<span class="token comment"># root_folder_id留空</span>
rclone config # 给Full access all files # 如想要性能则填入自己的client_id & secret;不然默认 # root_folder_id留空
itlwin at ~ ❯ rclone config
<span class="token key atrule">2020/05/04 10:07:12 NOTICE</span><span class="token punctuation">:</span> Config file "/root/.config/rclone/rclone.conf" not found <span class="token punctuation">-</span> using defaults
No remotes found <span class="token punctuation">-</span> make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q<span class="token punctuation">></span> n
name<span class="token punctuation">></span> gdrive_omgd
Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below<span class="token punctuation">,</span> or type in your own value
<span class="token punctuation">...</span>
13 / Google Drive
"drive"
<span class="token punctuation">...</span>
Storage<span class="token punctuation">></span> 13
<span class="token important">**</span> <span class="token key atrule">See help for drive backend at</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//rclone.org/drive/ <span class="token important">**</span>
Google Application Client Id
Setting your own is recommended.
See https<span class="token punctuation">:</span>//rclone.org/drive/<span class="token comment">#making-your-own-client-id for how to create your own.</span>
If you leave this blank<span class="token punctuation">,</span> it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").
client_id<span class="token punctuation">></span> 207780074259<span class="token punctuation">-</span>your<span class="token punctuation">-</span>own<span class="token punctuation">-</span>client<span class="token punctuation">-</span>id.apps.googleusercontent.com
Google Application Client Secret
Setting your own is recommended.
Enter a string value. Press Enter for the default ("").
client_secret<span class="token punctuation">></span> 83Jy<span class="token punctuation">-</span>your<span class="token punctuation">-</span>own<span class="token punctuation">-</span>secret<span class="token punctuation">-</span>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<span class="token punctuation">,</span> or type in your own value
1 / Full access all files<span class="token punctuation">,</span> excluding Application Data Folder.
"drive"
2 / Read<span class="token punctuation">-</span>only access to file metadata and file contents.
"drive.readonly"
/ Access to files created by rclone only.
3 <span class="token punctuation">|</span> These are visible in the drive website.
<span class="token punctuation">|</span> File authorization is revoked when the user deauthorizes the app.
"drive.file"
/ Allows read and write access to the Application Data folder.
4 <span class="token punctuation">|</span> This is not visible in the drive website.
"drive.appfolder"
/ Allows read<span class="token punctuation">-</span>only access to file metadata but
5 <span class="token punctuation">|</span> does not allow any access to read or download file content.
"drive.metadata.readonly"
scope<span class="token punctuation">></span> 1
ID of the root folder
Leave blank normally.
Fill in to access "Computers" folders (see docs)<span class="token punctuation">,</span> or for rclone to use
a non root folder as its starting point.
Note that if this is blank<span class="token punctuation">,</span> 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<span class="token punctuation">></span>
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<span class="token punctuation">></span>
Edit advanced config<span class="token punctuation">?</span> (y/n)
y) Yes
n) No (default)
y/n<span class="token punctuation">></span> n
Remote config
Use auto config<span class="token punctuation">?</span>
* Say Y if not sure
* Say N if you are working on a remote or headless machine
y) Yes (default)
n) No
y/n<span class="token punctuation">></span> n
<span class="token key atrule">Please go to the following link</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//accounts.google.com/o/oauth2/auth<span class="token punctuation">?</span>access_type=offline<span class="token important">&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</span>
Log in and authorize rclone for access
Enter verification code<span class="token punctuation">></span> 4/zQEo8<span class="token punctuation">---</span>shown<span class="token punctuation">-</span>on<span class="token punctuation">-</span>screen<span class="token punctuation">---</span>CecDZoakJKg
Configure this as a team drive<span class="token punctuation">?</span>
y) Yes
n) No (default)
y/n<span class="token punctuation">></span> y
Fetching team drive list<span class="token punctuation">...</span>
No team drives found in your account<span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">-</span><span class="token punctuation">-</span>
<span class="token punctuation">[</span>gdrive_omgd<span class="token punctuation">]</span>
type = drive
client_id = 207780074259<span class="token punctuation">-</span>your<span class="token punctuation">-</span>own<span class="token punctuation">-</span>client<span class="token punctuation">-</span>id.apps.googleusercontent.com
client_secret = 83J<span class="token punctuation">-</span>your<span class="token punctuation">-</span>own<span class="token punctuation">-</span>secret<span class="token punctuation">-</span>GyXh
scope = drive
token = <span class="token punctuation">{</span>"access_token"<span class="token punctuation">:</span><span class="token string">"ya29.your-own-access-token-ipLb-rg-YroD_51ifEc0VMI"</span><span class="token punctuation">,</span>"token_type"<span class="token punctuation">:</span><span class="token string">"Bearer"</span><span class="token punctuation">,</span>"refresh_token"<span class="token punctuation">:</span><span class="token string">"1//03QEh9JYbbPZgCgYIARAAGAMSNwF-your-own-refersh-token-A048Ng"</span><span class="token punctuation">,</span>"expiry"<span class="token punctuation">:</span><span class="token string">"2020-05-04T11:20:37.69167538+02:00"</span><span class="token punctuation">}</span>
team_drive =
<span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">---</span><span class="token punctuation">-</span><span class="token punctuation">-</span>
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d<span class="token punctuation">></span> y
<span class="token key atrule">Current remotes</span><span class="token punctuation">:</span>
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<span class="token punctuation">></span>q
itlwin at ~ ❯ rclone config
<span class="token key atrule">2020/05/04 10:07:12 NOTICE</span><span class="token punctuation">:</span> Config file "/root/.config/rclone/rclone.conf" not found <span class="token punctuation">-</span> using defaults
No remotes found <span class="token punctuation">-</span> make a new one
n) New remote
s) Set configuration password
q) Quit config

n/s/q<span class="token punctuation">></span> n

name<span class="token punctuation">></span> gdrive_omgd

Type of storage to configure.
Enter a string value. Press Enter for the default ("").
Choose a number from below<span class="token punctuation">,</span> or type in your own value
<span class="token punctuation">...</span>
13 / Google Drive
    "drive"
<span class="token punctuation">...</span>

Storage<span class="token punctuation">></span> 13

<span class="token important">**</span> <span class="token key atrule">See help for drive backend at</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//rclone.org/drive/ <span class="token important">**</span>

Google Application Client Id
Setting your own is recommended.
See https<span class="token punctuation">:</span>//rclone.org/drive/<span class="token comment">#making-your-own-client-id for how to create your own.</span>
If you leave this blank<span class="token punctuation">,</span> it will use an internal key which is low performance.
Enter a string value. Press Enter for the default ("").

client_id<span class="token punctuation">></span> 207780074259<span class="token punctuation">-</span>your<span class="token punctuation">-</span>own<span class="token punctuation">-</span>client<span class="token punctuation">-</span>id.apps.googleusercontent.com

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

client_secret<span class="token punctuation">></span> 83Jy<span class="token punctuation">-</span>your<span class="token punctuation">-</span>own<span class="token punctuation">-</span>secret<span class="token punctuation">-</span>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<span class="token punctuation">,</span> or type in your own value
 1 / Full access all files<span class="token punctuation">,</span> excluding Application Data Folder.
    "drive"
 2 / Read<span class="token punctuation">-</span>only access to file metadata and file contents.
    "drive.readonly"
   / Access to files created by rclone only.
 3 <span class="token punctuation">|</span> These are visible in the drive website.
   <span class="token punctuation">|</span> File authorization is revoked when the user deauthorizes the app.
    "drive.file"
   / Allows read and write access to the Application Data folder.
 4 <span class="token punctuation">|</span> This is not visible in the drive website.
    "drive.appfolder"
   / Allows read<span class="token punctuation">-</span>only access to file metadata but
 5 <span class="token punctuation">|</span> does not allow any access to read or download file content.
    "drive.metadata.readonly"

scope<span class="token punctuation">></span> 1

ID of the root folder
Leave blank normally.

Fill in to access "Computers" folders (see docs)<span class="token punctuation">,</span> or for rclone to use
a non root folder as its starting point.

Note that if this is blank<span class="token punctuation">,</span> 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<span class="token punctuation">></span>
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<span class="token punctuation">></span>
Edit advanced config<span class="token punctuation">?</span> (y/n)
y) Yes
n) No (default)

y/n<span class="token punctuation">></span> n

Remote config
Use auto config<span class="token punctuation">?</span>
 * Say Y if not sure
 * Say N if you are working on a remote or headless machine
y) Yes (default)
n) No

y/n<span class="token punctuation">></span> n

<span class="token key atrule">Please go to the following link</span><span class="token punctuation">:</span> https<span class="token punctuation">:</span>//accounts.google.com/o/oauth2/auth<span class="token punctuation">?</span>access_type=offline<span class="token important">&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</span>
Log in and authorize rclone for access

Enter verification code<span class="token punctuation">></span> 4/zQEo8<span class="token punctuation">---</span>shown<span class="token punctuation">-</span>on<span class="token punctuation">-</span>screen<span class="token punctuation">---</span>CecDZoakJKg

Configure this as a team drive<span class="token punctuation">?</span>
y) Yes
n) No (default)

y/n<span class="token punctuation">></span> y

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

y/e/d<span class="token punctuation">></span> y

<span class="token key atrule">Current remotes</span><span class="token punctuation">:</span>

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<span class="token punctuation">></span>q
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
图片[2]-网站备份:使用Rclone进行备份到Google Drive-天时网
Config到最后就长这样子

验证: rclone about gdrive_omgd:

图片[3]-网站备份:使用Rclone进行备份到Google Drive-天时网

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

<span class="token function">wget</span> --no-check-certificate https://github.com/teddysun/across/raw/master/backup.sh <span class="token operator">&&</span> <span class="token function">chmod</span> +x backup.sh
<span class="token comment">## 修改配置文件。最重要的就是 -</span>
<span class="token comment"># ENCRYPTFLG BACKUPPASS MYSQL_ROOT_PASSWORD BACKUP[0]</span>
<span class="token comment"># RCLONE_NAME RCLONE_FOLDER</span>
<span class="token comment">## 一定要修改,不然Rclone不运行</span>
<span class="token comment"># RCLONE_FLG=true</span>
<span class="token comment">## 其他不用管了</span>
<span class="token function">vim</span> ./backup.sh
<span class="token function">wget</span> --no-check-certificate https://github.com/teddysun/across/raw/master/backup.sh <span class="token operator">&&</span> <span class="token function">chmod</span> +x backup.sh
<span class="token comment">## 修改配置文件。最重要的就是 -</span>
<span class="token comment"># ENCRYPTFLG BACKUPPASS MYSQL_ROOT_PASSWORD BACKUP[0]</span>
<span class="token comment"># RCLONE_NAME RCLONE_FOLDER</span>
<span class="token comment">## 一定要修改,不然Rclone不运行</span>
<span class="token comment"># RCLONE_FLG=true</span>
<span class="token comment">## 其他不用管了</span>
<span class="token function">vim</span> ./backup.sh
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
./backup.sh
./backup.sh
图片[4]-网站备份:使用Rclone进行备份到Google Drive-天时网

运行成功了。

图片[5]-网站备份:使用Rclone进行备份到Google Drive-天时网

每周自动运行

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

恢复解压的方法

<span class="token comment"># 先解密</span>
openssl enc -aes256 -in <span class="token punctuation">[</span>ENCRYPTED BACKUP<span class="token punctuation">]</span> -out decrypted_backup.tgz -pass pass:<span class="token punctuation">[</span>BACKUPPASS<span class="token punctuation">]</span> -d -md sha1
<span class="token comment"># 再解压</span>
<span class="token function">tar</span> -zxPf <span class="token punctuation">[</span>DECRYPTION BACKUP FILE<span class="token punctuation">]</span>
<span class="token comment">## 解释一下参数 -P:</span>
<span class="token comment"># tar 压缩文件默认都是相对路径的。加个 -P 是为了 tar 能以绝对路径压缩文件。因此,解压的时候也要带个 -P 参数。</span>
<span class="token comment"># 先解密</span>
openssl enc -aes256 -in <span class="token punctuation">[</span>ENCRYPTED BACKUP<span class="token punctuation">]</span> -out decrypted_backup.tgz -pass pass:<span class="token punctuation">[</span>BACKUPPASS<span class="token punctuation">]</span> -d -md sha1
<span class="token comment"># 再解压</span>
<span class="token function">tar</span> -zxPf <span class="token punctuation">[</span>DECRYPTION BACKUP FILE<span class="token punctuation">]</span>
<span class="token comment">## 解释一下参数 -P:</span>
<span class="token comment"># tar 压缩文件默认都是相对路径的。加个 -P 是为了 tar 能以绝对路径压缩文件。因此,解压的时候也要带个 -P 参数。</span>
# 先解密 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 参数。
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享
Your dream is like a flower. if you water it patiently, the flower will come out beautifully.
即使是最简单的梦想,用心浇灌,也能开出绚烂的花
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片快捷回复

    暂无评论内容