利用rclone对lnmp环境下的网站备份
往记
撰写于 2023年 08月 06 日

换了lnmp以后,网站的数据备份是个问题,因为lnmp一键包虽然支持自动备份,但并没有提供远程上传到第三方存储的方案,所以需要自己手动配置上传信息,将数据传到其他平台以防服务器炸机丢失数据。
本文主要对操作经过进行一个记录,以防忘记。

windows获取api

下载windows客户端
下载地址:https://rclone.org/downloads/

获取api信息

解压后进入目录里,ctrl+shift打开终端
rclone authorize "onedrive"
打开网址登录后回到终端复制api密钥保存下来,后面要用
复制
linux端调试rclone

准备rclone

wget https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
chmod 0777 ./rclone-*/rclone
cp ./rclone-*/rclone /usr/bin/
rm -rf ./rclone-*

linux端配置rclone

#输入
rclone config

配置远程服务

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> 

选择n
显示如下
name>onedrive #这里随便输入服务名,后面要用,填完后回车下一步

选择网盘类型

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
 2 / Akamai NetStorage
   \ (netstorage)
 3 / Alias for an existing remote
   \ (alias)
 4 / Amazon Drive
   \ (amazon cloud drive)
 5 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, China Mobile, Cloudflare, ArvanCloud, DigitalOcean, Dreamhost, Huawei OBS, IBM COS, IDrive e2, IONOS Cloud, Liara, Lyve Cloud, Minio, Netease, RackCorp, Scaleway, SeaweedFS, StackPath, Storj, Tencent COS, Qiniu and Wasabi
   \ (s3)
 6 / Backblaze B2
   \ (b2)
 7 / Better checksums for other remotes
   \ (hasher)
 8 / Box
   \ (box)
 9 / Cache a remote
   \ (cache)
10 / Citrix Sharefile
   \ (sharefile)
11 / Combine several remotes into one
   \ (combine)
12 / Compress a remote
   \ (compress)
13 / Dropbox
   \ (dropbox)
14 / Encrypt/Decrypt a remote
   \ (crypt)
15 / Enterprise File Fabric
   \ (filefabric)
16 / FTP
   \ (ftp)
17 / Google Cloud Storage (this is not Google Drive)
   \ (google cloud storage)
18 / Google Drive
   \ (drive)
19 / Google Photos
   \ (google photos)
20 / HTTP
   \ (http)
21 / Hadoop distributed file system
   \ (hdfs)
22 / HiDrive
   \ (hidrive)
23 / In memory object storage system.
   \ (memory)
24 / Internet Archive
   \ (internetarchive)
25 / Jottacloud
   \ (jottacloud)
26 / Koofr, Digi Storage and other Koofr-compatible storage providers
   \ (koofr)
27 / Local Disk
   \ (local)
28 / Mail.ru Cloud
   \ (mailru)
29 / Mega
   \ (mega)
30 / Microsoft Azure Blob Storage
   \ (azureblob)
31 / Microsoft OneDrive
   \ (onedrive)
32 / OpenDrive
   \ (opendrive)
33 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ (swift)
34 / Oracle Cloud Infrastructure Object Storage
   \ (oracleobjectstorage)
35 / Pcloud
   \ (pcloud)
36 / Put.io
   \ (putio)
37 / QingCloud Object Storage
   \ (qingstor)
38 / SMB / CIFS
   \ (smb)
39 / SSH/SFTP
   \ (sftp)
40 / Sia Decentralized Cloud
   \ (sia)
41 / Storj Decentralized Cloud Storage
   \ (storj)
42 / Sugarsync
   \ (sugarsync)
43 / Transparently chunk/split large files
   \ (chunker)
44 / Union merges the contents of several upstream fs
   \ (union)
45 / Uptobox
   \ (uptobox)
46 / WebDAV
   \ (webdav)
47 / Yandex Disk
   \ (yandex)
48 / Zoho
   \ (zoho)
49 / premiumize.me
   \ (premiumizeme)
50 / seafile
   \ (seafile)
Storage> 31

我这里onedrive序号是31,所以storage里填31,填完后回车进入下一步

设置应用程序id和密钥

#上一步操作完会显示如下
Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id> 
##这里client_id不用填,回车下一步

#上一步操作完会显示如下
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> 
#这里也是回车,不用填

#上一步操作完会显示如下
Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own string value.
Press Enter for the default (global).
 1 / Microsoft Cloud Global
   \ (global)
 2 / Microsoft Cloud for US Government
   \ (us)
 3 / Microsoft Cloud Germany
   \ (de)
 4 / Azure and Office 365 operated by Vnet Group in China
   \ (cn)
region> 
#这里region输入1,1代表全球版,2代表美版,3代表德国版,4代表中国世纪互联版,按自己的微软账号选择

#上一步操作完会显示如下
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> 
#这里选择n

#上一步操作完会显示如下
Use web browser to automatically authenticate rclone with remote?
 * Say Y if the machine running rclone has a web browser you can use
 * Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes (default)
n) No
y/n> n
#这里选择n

#上一步操作完会显示如下
Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
    rclone authorize "onedrive"
Then paste the result.
Enter a value.
config_token>
#token为最前面在windows端获取的api密钥,粘贴到这里回车

#上一步操作完会显示如下
Option config_type.
Type of connection
Choose a number from below, or type in an existing string value.
Press Enter for the default (onedrive).
 1 / OneDrive Personal or Business
   \ (onedrive)
 2 / Root Sharepoint site
   \ (sharepoint)
   / Sharepoint site name or URL
 3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
   \ (url)
 4 / Search for a Sharepoint site
   \ (search)
 5 / Type in driveID (advanced)
   \ (driveid)
 6 / Type in SiteID (advanced)
   \ (siteid)
   / Sharepoint server-relative path (advanced)
 7 | E.g. /teams/hr
   \ (path)
config_type>
#这里填1,1代表家庭和商业版office,2代表sharepoint版(开发者)

#上一步操作完会显示如下
Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own string value.
Press Enter for the default (6f3d01205a57f0b1).
 1 /  (personal)
   \ (2323232312b1)
config_driveid>
#这里显示你的账号所拥有的版本,是1就填1,其他数字就填其他

#上一步操作完会显示如下
Drive OK?
Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=232344f0b1
y) Yes (default)
n) No
y/n> 
#意思是是否确认这个id,输入y确认

#上一步操作完显示如下
Configuration complete.
Options:
- type: onedrive
- token: {"access_token":"abcdwwewesauwwyewhsheew","expiry":"2023-04-12T01:14:31.2324320844+08:00"}
- drive_id: 6f3d01205a57f0b1
- drive_type: personal
Keep this "cysui" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> 
#这里输入y确认

#上一步操作完会显示如下
Current remotes:

Name                 Type
====                 ====
qwert                onedrive

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表示完成了退出配置页面

配置备份脚本参数

#修改lnmp的默认自动备份参数
vi /root/lnmp1.9/tools/backup.sh
##以下为参考配置
Backup_Home="/home/backup/"  # 此为备份文件存放目录
MySQL_Dump="/usr/local/mysql/bin/mysqldump"  # mysqldump路径,如果是mariadb,替换/usr/local/mysql为/usr/local/mariadb
######~Set Directory you want to backup~######
Backup_Dir=("/home/wwwroot/123456.com" "/home/wwwroot/123456.com")  # 要备份的目录,目录用双引号括起来,多个目录空格隔开

######~Set MySQL Database you want to backup~######
Backup_Database=("lnmp")  # 要备份的数据库,目录用双引号括起来,多个目录空格隔开

######~Set MySQL UserName and password~######
MYSQL_UserName='root'  # MySQL root账号
MYSQL_PassWord='yourrootpassword'  # MySQL root密码

######~Enable Ftp Backup~######
Enable_FTP=0  # 是否启用ftp备份。0 启用;1 不启用

0: enable; 1: disable
######~Set FTP Information~###### ftp账号信息
FTP_Host='1.2.3.4'
FTP_Username='123456.com'
FTP_Password='yourftppassword'
FTP_Dir="backup"  # ftp服务器上存放备份的目录

#Values Setting END! 默认备份文件保存3天,可以修改backup.sh里的-3day为你指定的天数。

####特别注意,ftp那项不用写,那些是传输到其他ftp服务器的配置

配置自动上传脚本

#编辑上传脚本,建议在root目录下执行,如cd /root
vi backupload.sh

#脚本内容
#!/bin/bash
BakDate=`date +%Y%m%d%H%M`
/root/lnmp1.9/tools/backup.sh
cp -r /home/backup /home/123-${BakDate}  ##该行表示将lnmp备份目录下的文件复制到(123+年月日)这个文件夹
rclone move /home/123-${BakDate} cysui:/abc    #123可自定义自己喜欢的,cysui改为在rclone中创建的服务名,/abc为onedrive里的目录,该行表示将(123-${BakDate})目录下的文件上传到abc这个文件夹
rm -rf /home/123-${BakDate}  #删除临时创建的(123-${BakDate})这文件夹

#按esc退出编辑,输入:wq!保存

#赋予这个脚本权限
chmod +x backupload.sh

#手动运行一次脚本测试,去onedrive目录查看文件是否上传成功
/root/backupload.sh

配置定时执行任务

#输入
crontab -e
#选择vim或者nano编辑器都行

#在最后一行加入如下内容
0 3 * * * /root/backupload.sh
#该参数表示每天3点的时候自动执行这个脚本

教程结束

就可以快乐的玩耍了!

利用rclone对lnmp环境下的网站备份

换了lnmp以后,网站的数据备份是个问题,因为lnmp一键包虽然支持自动备份,但并没有提供远程上传到第三方存储的方案,所以需要自己手动配置上传信息,将数据传到其他平台以防服务器炸机丢失数据。
本文主要对操作经过进行一个记录,以防忘记。

windows获取api

下载windows客户端
下载地址:https://rclone.org/downloads/

获取api信息

解压后进入目录里,ctrl+shift打开终端
rclone authorize "onedrive"
打开网址登录后回到终端复制api密钥保存下来,后面要用
复制
linux端调试rclone

准备rclone

wget https://downloads.rclone.org/rclone-current-linux-amd64.zip
unzip rclone-current-linux-amd64.zip
chmod 0777 ./rclone-*/rclone
cp ./rclone-*/rclone /usr/bin/
rm -rf ./rclone-*

linux端配置rclone

#输入
rclone config

配置远程服务

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> 

选择n
显示如下
name>onedrive #这里随便输入服务名,后面要用,填完后回车下一步

选择网盘类型

Option Storage.
Type of storage to configure.
Choose a number from below, or type in your own value.
 1 / 1Fichier
   \ (fichier)
 2 / Akamai NetStorage
   \ (netstorage)
 3 / Alias for an existing remote
   \ (alias)
 4 / Amazon Drive
   \ (amazon cloud drive)
 5 / Amazon S3 Compliant Storage Providers including AWS, Alibaba, Ceph, China Mobile, Cloudflare, ArvanCloud, DigitalOcean, Dreamhost, Huawei OBS, IBM COS, IDrive e2, IONOS Cloud, Liara, Lyve Cloud, Minio, Netease, RackCorp, Scaleway, SeaweedFS, StackPath, Storj, Tencent COS, Qiniu and Wasabi
   \ (s3)
 6 / Backblaze B2
   \ (b2)
 7 / Better checksums for other remotes
   \ (hasher)
 8 / Box
   \ (box)
 9 / Cache a remote
   \ (cache)
10 / Citrix Sharefile
   \ (sharefile)
11 / Combine several remotes into one
   \ (combine)
12 / Compress a remote
   \ (compress)
13 / Dropbox
   \ (dropbox)
14 / Encrypt/Decrypt a remote
   \ (crypt)
15 / Enterprise File Fabric
   \ (filefabric)
16 / FTP
   \ (ftp)
17 / Google Cloud Storage (this is not Google Drive)
   \ (google cloud storage)
18 / Google Drive
   \ (drive)
19 / Google Photos
   \ (google photos)
20 / HTTP
   \ (http)
21 / Hadoop distributed file system
   \ (hdfs)
22 / HiDrive
   \ (hidrive)
23 / In memory object storage system.
   \ (memory)
24 / Internet Archive
   \ (internetarchive)
25 / Jottacloud
   \ (jottacloud)
26 / Koofr, Digi Storage and other Koofr-compatible storage providers
   \ (koofr)
27 / Local Disk
   \ (local)
28 / Mail.ru Cloud
   \ (mailru)
29 / Mega
   \ (mega)
30 / Microsoft Azure Blob Storage
   \ (azureblob)
31 / Microsoft OneDrive
   \ (onedrive)
32 / OpenDrive
   \ (opendrive)
33 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ (swift)
34 / Oracle Cloud Infrastructure Object Storage
   \ (oracleobjectstorage)
35 / Pcloud
   \ (pcloud)
36 / Put.io
   \ (putio)
37 / QingCloud Object Storage
   \ (qingstor)
38 / SMB / CIFS
   \ (smb)
39 / SSH/SFTP
   \ (sftp)
40 / Sia Decentralized Cloud
   \ (sia)
41 / Storj Decentralized Cloud Storage
   \ (storj)
42 / Sugarsync
   \ (sugarsync)
43 / Transparently chunk/split large files
   \ (chunker)
44 / Union merges the contents of several upstream fs
   \ (union)
45 / Uptobox
   \ (uptobox)
46 / WebDAV
   \ (webdav)
47 / Yandex Disk
   \ (yandex)
48 / Zoho
   \ (zoho)
49 / premiumize.me
   \ (premiumizeme)
50 / seafile
   \ (seafile)
Storage> 31

我这里onedrive序号是31,所以storage里填31,填完后回车进入下一步

设置应用程序id和密钥

#上一步操作完会显示如下
Option client_id.
OAuth Client Id.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_id> 
##这里client_id不用填,回车下一步

#上一步操作完会显示如下
Option client_secret.
OAuth Client Secret.
Leave blank normally.
Enter a value. Press Enter to leave empty.
client_secret> 
#这里也是回车,不用填

#上一步操作完会显示如下
Option region.
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own string value.
Press Enter for the default (global).
 1 / Microsoft Cloud Global
   \ (global)
 2 / Microsoft Cloud for US Government
   \ (us)
 3 / Microsoft Cloud Germany
   \ (de)
 4 / Azure and Office 365 operated by Vnet Group in China
   \ (cn)
region> 
#这里region输入1,1代表全球版,2代表美版,3代表德国版,4代表中国世纪互联版,按自己的微软账号选择

#上一步操作完会显示如下
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> 
#这里选择n

#上一步操作完会显示如下
Use web browser to automatically authenticate rclone with remote?
 * Say Y if the machine running rclone has a web browser you can use
 * Say N if running rclone on a (remote) machine without web browser access
If not sure try Y. If Y failed, try N.
y) Yes (default)
n) No
y/n> n
#这里选择n

#上一步操作完会显示如下
Option config_token.
For this to work, you will need rclone available on a machine that has
a web browser available.
For more help and alternate methods see: https://rclone.org/remote_setup/
Execute the following on the machine with the web browser (same rclone
version recommended):
    rclone authorize "onedrive"
Then paste the result.
Enter a value.
config_token>
#token为最前面在windows端获取的api密钥,粘贴到这里回车

#上一步操作完会显示如下
Option config_type.
Type of connection
Choose a number from below, or type in an existing string value.
Press Enter for the default (onedrive).
 1 / OneDrive Personal or Business
   \ (onedrive)
 2 / Root Sharepoint site
   \ (sharepoint)
   / Sharepoint site name or URL
 3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite
   \ (url)
 4 / Search for a Sharepoint site
   \ (search)
 5 / Type in driveID (advanced)
   \ (driveid)
 6 / Type in SiteID (advanced)
   \ (siteid)
   / Sharepoint server-relative path (advanced)
 7 | E.g. /teams/hr
   \ (path)
config_type>
#这里填1,1代表家庭和商业版office,2代表sharepoint版(开发者)

#上一步操作完会显示如下
Option config_driveid.
Select drive you want to use
Choose a number from below, or type in your own string value.
Press Enter for the default (6f3d01205a57f0b1).
 1 /  (personal)
   \ (2323232312b1)
config_driveid>
#这里显示你的账号所拥有的版本,是1就填1,其他数字就填其他

#上一步操作完会显示如下
Drive OK?
Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=232344f0b1
y) Yes (default)
n) No
y/n> 
#意思是是否确认这个id,输入y确认

#上一步操作完显示如下
Configuration complete.
Options:
- type: onedrive
- token: {"access_token":"abcdwwewesauwwyewhsheew","expiry":"2023-04-12T01:14:31.2324320844+08:00"}
- drive_id: 6f3d01205a57f0b1
- drive_type: personal
Keep this "cysui" remote?
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> 
#这里输入y确认

#上一步操作完会显示如下
Current remotes:

Name                 Type
====                 ====
qwert                onedrive

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表示完成了退出配置页面

配置备份脚本参数

#修改lnmp的默认自动备份参数
vi /root/lnmp1.9/tools/backup.sh
##以下为参考配置
Backup_Home="/home/backup/"  # 此为备份文件存放目录
MySQL_Dump="/usr/local/mysql/bin/mysqldump"  # mysqldump路径,如果是mariadb,替换/usr/local/mysql为/usr/local/mariadb
######~Set Directory you want to backup~######
Backup_Dir=("/home/wwwroot/123456.com" "/home/wwwroot/123456.com")  # 要备份的目录,目录用双引号括起来,多个目录空格隔开

######~Set MySQL Database you want to backup~######
Backup_Database=("lnmp")  # 要备份的数据库,目录用双引号括起来,多个目录空格隔开

######~Set MySQL UserName and password~######
MYSQL_UserName='root'  # MySQL root账号
MYSQL_PassWord='yourrootpassword'  # MySQL root密码

######~Enable Ftp Backup~######
Enable_FTP=0  # 是否启用ftp备份。0 启用;1 不启用

0: enable; 1: disable
######~Set FTP Information~###### ftp账号信息
FTP_Host='1.2.3.4'
FTP_Username='123456.com'
FTP_Password='yourftppassword'
FTP_Dir="backup"  # ftp服务器上存放备份的目录

#Values Setting END! 默认备份文件保存3天,可以修改backup.sh里的-3day为你指定的天数。

####特别注意,ftp那项不用写,那些是传输到其他ftp服务器的配置

配置自动上传脚本

#编辑上传脚本,建议在root目录下执行,如cd /root
vi backupload.sh

#脚本内容
#!/bin/bash
BakDate=`date +%Y%m%d%H%M`
/root/lnmp1.9/tools/backup.sh
cp -r /home/backup /home/123-${BakDate}  ##该行表示将lnmp备份目录下的文件复制到(123+年月日)这个文件夹
rclone move /home/123-${BakDate} cysui:/abc    #123可自定义自己喜欢的,cysui改为在rclone中创建的服务名,/abc为onedrive里的目录,该行表示将(123-${BakDate})目录下的文件上传到abc这个文件夹
rm -rf /home/123-${BakDate}  #删除临时创建的(123-${BakDate})这文件夹

#按esc退出编辑,输入:wq!保存

#赋予这个脚本权限
chmod +x backupload.sh

#手动运行一次脚本测试,去onedrive目录查看文件是否上传成功
/root/backupload.sh

配置定时执行任务

#输入
crontab -e
#选择vim或者nano编辑器都行

#在最后一行加入如下内容
0 3 * * * /root/backupload.sh
#该参数表示每天3点的时候自动执行这个脚本

教程结束

就可以快乐的玩耍了!

赞 (0)

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论

点击验证后再提交