使用 Rclone 挂载 OneDrive 自动备份 VPS 数据

前言

前天下午太刺激了,不知道怎么回事,网站数据目录突然没了,顿时满头大汗汗流浃背,一边找文件目录一边连抽了两根烟,这个时候才想起来备份数据的重要,后悔,懊恼席卷全身,毫不夸张

赶紧谷歌搜索有没有vps恢复数据的方法,又问了GPT都没找到想要的答案,心里已经做好了最坏的打算,反正还有半月前的一次备份,大不了重新再来。同时也是满脸疑惑,只有网站数据目录没有了,证书,数据库目录都在,这是怎么回事,有病毒?还是FinalShell客户端的原因?

就在想的时候也误打误撞的找到了丢失的文件目录,原来是因为不知道什么时候手一哆嗦在FinalShell窗口把网站数据目录给挪到另外一个文件夹了

虽然经历了只有短短10分钟,可我感觉就像几个小时,这次算是给了一个小小的教训,再也不敢大意了

介绍

Rclone 是一个用于管理云存储上的文件的命令行程序。

Rclone 可以

  • 将文件备份(并加密)到云存储
  • 从云存储恢复(和解密)文件
  • 将云数据镜像到其他云服务或本地
  • 将数据迁移到云端,或在云存储供应商之间迁移
  • 将多个加密、缓存或多样化的云存储挂载为磁盘
  • 使用lsf、ljson、size、ncdu分析和解释云存储中保存的数据
  • 将文件系统合并在一起,将多个本地和/或云文件系统呈现为一个整体

Rclone官网:https://rclone.org

GitHub项目地址:https://github.com/rclone/rclone

获取OneDrive授权码

由于一般的 Linux 机器没有浏览器,所以需要先下载本地客户端通过浏览器打开 OneDrive 网页获取授权码给 Rclone 授权写入文件权限

图片[1]-使用 Rclone 挂载 OneDrive 自动备份 VPS 数据 - 我是Tron-我是Tron

地址:https://rclone.org/downloads/

我使用的是windows10 64位系统,所以使用第一个下载。按照自己的系统下载到本地电脑并解压即可

打开解压的文件夹在空白处按下Shift+右键,调出隐藏的Powershell菜单,打开Powershell

.\rclone.exe authorize "onedrive"

输入命令回车,会跳转到浏览器

图片[2]-使用 Rclone 挂载 OneDrive 自动备份 VPS 数据 - 我是Tron-我是Tron

登录微软账户

Success!

All done. Please go back to rclone.

网页提示以上信息说明获取授权码成功

图片[3]-使用 Rclone 挂载 OneDrive 自动备份 VPS 数据 - 我是Tron-我是Tron

返回Powershell窗口,复制 config_token 信息,保存下来备用

注意确保包含大括号,例如:{"access_token":"............................"}

安装Rclone

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

提示以下信息说明安装成功

rclone v1.66.0 has successfully installed. # rclone v1.66.0 已成功安装。
Now run "rclone config" for setup. Check https://rclone.org/docs/ for more details. #现在运行“rclone config”进行设置。 检查 https://rclone.org/docs/ 了解更多详细信息。

配置 Rclone

rclone config

输入rclone config 配置 Rclone

root@ser218576769304:~# rclone config # 这是rclone命令,用于配置rclone
2024/05/15 02:01:56 NOTICE: Config file "/root/.config/rclone/rclone.conf" not found - using defaults # 如果找不到配置文件,会提示未找到配置文件并使用默认设置
No remotes found, make a new one? # 没有找到远程服务,是否创建一个新的?
n) New remote # 输入'n'表示创建一个新的远程服务
s) Set configuration password # 输入's'表示设置配置密码
q) Quit config # 输入'q'退出配置
n/s/q> rclone config
This value must be a single character, one of the following: n, s, q. # 输入值必须是单个字符,其中之一:n,s,q。
n/s/q> n

输入n创建一个新的远程服务

Enter name for new remote. # 输入新远程服务的名称。
name> onedrive # 在这里输入了新远程服务的名称为 "onedrive"。

命名为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 S3 Compliant Storage Providers including AWS, Alibaba, ArvanCloud, Ceph, ChinaMobile, Cloudflare, DigitalOcean, Dreamhost, GCS, HuaweiOBS, IBMCOS, IDrive, IONOS, LyveCloud, Leviia, Liara, Linode, Minio, Netease, Petabox, RackCorp, Rclone, Scaleway, SeaweedFS, StackPath, Storj, Synology, TencentCOS, Wasabi, Qiniu and others
   \ (s3)
 5 / Backblaze B2
   \ (b2)
 6 / Better checksums for other remotes
   \ (hasher)
 7 / Box
   \ (box)
 8 / Cache a remote
   \ (cache)
 9 / Citrix Sharefile
   \ (sharefile)
10 / Combine several remotes into one
   \ (combine)
11 / Compress a remote
   \ (compress)
12 / Dropbox
   \ (dropbox)
13 / Encrypt/Decrypt a remote
   \ (crypt)
14 / Enterprise File Fabric
   \ (filefabric)
15 / FTP
   \ (ftp)
16 / Google Cloud Storage (this is not Google Drive)
   \ (google cloud storage)
17 / Google Drive
   \ (drive)
18 / Google Photos
   \ (google photos)
19 / HTTP
   \ (http)
20 / Hadoop distributed file system
   \ (hdfs)
21 / HiDrive
   \ (hidrive)
22 / ImageKit.io
   \ (imagekit)
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 / Linkbox
   \ (linkbox)
28 / Local Disk
   \ (local)
29 / Mail.ru Cloud
   \ (mailru)
30 / Mega
   \ (mega)
31 / Microsoft Azure Blob Storage
   \ (azureblob)
32 / Microsoft Azure Files
   \ (azurefiles)
33 / Microsoft OneDrive
   \ (onedrive)
34 / OpenDrive
   \ (opendrive)
35 / OpenStack Swift (Rackspace Cloud Files, Blomp Cloud Storage, Memset Memstore, OVH)
   \ (swift)
36 / Oracle Cloud Infrastructure Object Storage
   \ (oracleobjectstorage)
37 / Pcloud
   \ (pcloud)
38 / PikPak
   \ (pikpak)
39 / Proton Drive
   \ (protondrive)
40 / Put.io
   \ (putio)
41 / QingCloud Object Storage
   \ (qingstor)
42 / Quatrix by Maytech
   \ (quatrix)
43 / SMB / CIFS
   \ (smb)
44 / SSH/SFTP
   \ (sftp)
45 / Sia Decentralized Cloud
   \ (sia)
46 / Storj Decentralized Cloud Storage
   \ (storj)
47 / Sugarsync
   \ (sugarsync)
48 / Transparently chunk/split large files
   \ (chunker)
49 / Union merges the contents of several upstream fs
   \ (union)
50 / Uptobox
   \ (uptobox)
51 / WebDAV
   \ (webdav)
52 / Yandex Disk
   \ (yandex)
53 / Zoho
   \ (zoho)
54 / premiumize.me
   \ (premiumizeme)
55 / seafile
   \ (seafile)
Storage> 33

输入 33 ,选择Microsoft OneDrive。命令可能有所变更,以实际序号填写

Option client_id. # OAuth客户端ID。
Leave blank normally. # 通常情况下留空。
Enter a value. Press Enter to leave empty. # 输入一个值。按Enter键留空。
client_id> # 在这里输入OAuth客户端ID。如果没有,请直接按Enter键留空。

Option client_secret. # OAuth客户端秘钥。
Leave blank normally. # 通常情况下留空。
Enter a value. Press Enter to leave empty. # 输入一个值。按Enter键留空。
client_secret> # 在这里输入OAuth客户端秘钥。如果没有,请直接按Enter键留空。

Option client_id.Option client_secret.都输入n,回车即可

Option region. # 区域选项。
Choose national cloud region for OneDrive. # 选择 OneDrive 的国家云区域。
Choose a number from below, or type in your own string value. # 从下面选择一个数字,或者输入自己的字符串值。
Press Enter for the default (global). # 按 Enter 键使用默认值(全球)。
 1 / Microsoft Cloud Global # Microsoft 云全球
   \ (global)
 2 / Microsoft Cloud for US Government # 美国政府的 Microsoft 云
   \ (us)
 3 / Microsoft Cloud Germany # 德国的 Microsoft 云
   \ (de)
 4 / Azure and Office 365 operated by Vnet Group in China # 在中国由 Vnet Group 运营的 Azure 和 Office 365
   \ (cn)
region> 1

选择 OneDrive 服务器区域时,如果使用的是 国际版输入 1 ,如果使用的是 世纪互联输入 4,一般选择1或者直接回车使用默认值

Edit advanced config? # 编辑高级配置?
y) Yes # 是
n) No (default) # 否(默认)
y/n> n

不编辑高级配置,输入n

Use web browser to automatically authenticate rclone with remote? # 使用 Web 浏览器自动进行 rclone 与远程服务的身份验证吗?
 * Say Y if the machine running rclone has a web browser you can use # 如果运行 rclone 的机器有可用的 Web 浏览器,请选择 Y
 * Say N if running rclone on a (remote) machine without web browser access # 如果在没有 Web 浏览器访问权限的(远程)机器上运行 rclone,请选择 N
If not sure try Y. If Y failed, try N. # 如果不确定,请尝试选择 Y。如果 Y 失败,请尝试选择 N。

y) Yes (default) # 是(默认)
n) No # 否
y/n> n 

前面已经保存了 config_token,所以输入n ,不使用 Web 浏览器进行自动身份验证

图片[4]-使用 Rclone 挂载 OneDrive 自动备份 VPS 数据 - 我是Tron-我是Tron

输入前面已经保存了 config_token,回车

Option config_type. # 连接类型选项。
Type of connection # 连接的类型
Choose a number from below, or type in an existing string value. # 从下面选择一个数字,或者输入一个现有的字符串值。
Press Enter for the default (onedrive). # 按 Enter 键使用默认值(onedrive)。
 1 / OneDrive Personal or Business # OneDrive 个人版或商业版
   \ (onedrive)
 2 / Root Sharepoint site # 根 Sharepoint 站点
   \ (sharepoint)
   / Sharepoint site name or URL # Sharepoint 站点名称或 URL
 3 | E.g. mysite or https://contoso.sharepoint.com/sites/mysite # 例如 mysite 或 https://contoso.sharepoint.com/sites/mysite
   \ (url)
 4 / Search for a Sharepoint site # 搜索 Sharepoint 站点
   \ (search)
 5 / Type in driveID (advanced) # 输入 driveID(高级)
   \ (driveid)
 6 / Type in SiteID (advanced) # 输入 SiteID(高级)
   \ (siteid)
   / Sharepoint server-relative path (advanced) # Sharepoint 服务器相对路径(高级)
 7 | E.g. /teams/hr # 例如 /teams/hr
   \ (path)
config_type> 1

输入1,选择 OneDrive 个人版或商业版

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 (b!***************************************************************1). # 按 Enter 键使用默认值(b!***************************************************************1)。
 1 / OneDrive (personal) # OneDrive (个人)
   \ (b!xYzAbCdEfGhIjKlMnOpQrStUvWxYz1234567890)
 2 / OneDrive (personal) # OneDrive (个人)
   \ (b!aBcDeFgHiJkLmNoPqRsTuVwXyZ0987654321)
 3 / OneDrive (personal) # OneDrive (个人)
   \ (1**************6)
config_driveid> 3

输入3,回车,括号中的16位字符1**************6是 OneDrive的 cid,后续可以通过淘宝花¥1.5让别人刷邀请获取10G免费容量

Drive OK? # 驱动器是否正确?
Found drive "root" of type "personal" # 找到类型为“personal”的驱动器“root”
URL: https://onedrive.live.com?cid=1**************6&id=0********************************Z

y) Yes (default) # 是(默认)
n) No # 否
y/n> y

输入y,确认

Configuration complete. # 配置完成。
Options: # 选项:
- type: onedrive # 类型:onedrive
- token: # 会显示之前输入的token,此处省略
- drive_id: 1**************6 # 驱动器ID
- drive_type: personal # 驱动器类型:个人版
Keep this "onedrive" remote? # 保留此 "onedrive" 远程服务吗?
y) Yes this is OK (default) # 是,这是正确的(默认)
e) Edit this remote # 编辑此远程服务
d) Delete this remote # 删除此远程服务
y/e/d> y

输入y,确定保留此 onedrive 远程服务

Current remotes: # 当前的远程服务:
Name                 Type # 名称                类型
====                 ==== # ====                ====
onedrive             onedrive # onedrive             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

输入q 退出配置

配置完成

测试备份

rclone copy /www/guaishou.txt onedrive:backup

这条命令会将vps本地文件 /www/guaishou.txt 复制到名为 onedrive 的远程服务OneDrivebackup 文件夹中。
此命令也适用于资料不多的手动备份方案

/www/guaishou.txt是要备份的vps路径的文件或文件夹,onedrive是一开始设置的远程服务名称
backupOneDrive 网盘里面的文件夹,一定要先创建好,否则同步时候会找不到这个目录

自行修改相关路径和名称

运行后窗口没有信息反馈,打开 OneDrive 网盘查看测试文件是否同步成功

创建Rclone定时备份

创建Rclone备份脚本文件

nano backup.sh

新建并编辑backup.sh文件,该文件存放在/root文件夹下

#!/bin/bash
WEEKDAY=$(date +%u)
tar -zcvf "/root/backup_${WEEKDAY}.tar.gz" "/www/html"
rclone copy "/root/backup_${WEEKDAY}.tar.gz" onedrive:backup
rm "/root/backup_${WEEKDAY}.tar.gz"
#!/bin/bash

# 获取当前星期几的数字(1-7,1代表星期一,7代表星期日)
WEEKDAY=$(date +%u)

# 将/www/html文件夹打包成以星期几命名的压缩文件,backup_是文件前缀,如:backup_1.tar.gz
tar -zcvf "/root/backup_${WEEKDAY}.tar.gz" "/www/html"

# 将压缩后的文件上传到 OneDrive 的 backup 文件夹中
rclone copy "/root/backup_${WEEKDAY}.tar.gz" onedrive:backup

# 删除本地的压缩文件
rm "/root/backup_${WEEKDAY}.tar.gz"

粘贴命令, Ctrl+x 保存并退出,再输入 y 确定,回车

这个脚本会将 /www/html 文件夹打包成以星期几数字命名的压缩文件,并将其上传到 OneDrive 的 Backup 文件夹中,最后删除本地的压缩文件。

以星期命名文件能循环覆盖保存7份备份文件,如果按照年月日备份的话,每个文件名都不同,不能自动覆盖,OneDrive会一大堆以日期命名的压缩包

由于OneDrive空间只有15G而我的资料就有4G,如果使用7天循环的话空间容量也是不够的

所以我使用backup_1backup_2backup_3的循环备份,按照每天的日期循环命名文件为backup_1backup_2backup_3并上传到OneDrive,这样就不会超出容量限制

#!/bin/bash

# 获取当前日期(格式为 YYYYMMDD)
DATE=$(date +%Y%m%d)

# 计算备份文件名的编号(1, 2, 3 循环)
BACKUP_NUMBER=$(( (($DATE - 20240520) % 3) + 1 ))

# 备份文件名格式为 backup_1.tar.gz、backup_2.tar.gz、backup_3.tar.gz
BACKUP_FILENAME="/root/backup_${BACKUP_NUMBER}.tar.gz"

# 创建一个压缩的备份文件
tar -zcvf "$BACKUP_FILENAME" "/www/html"

# 使用 rclone 将备份文件上传到 OneDrive 的 backup 目录
rclone copy "$BACKUP_FILENAME" onedrive:backup

# 删除本地备份文件
rm "$BACKUP_FILENAME"

赋予脚本755权限

chmod 755 backup.sh

设置Rclone自动任务

使用vps自带的定时任务工具 cron 设置定时任务,自动执行备份脚本

crontab -e
# 提示
no crontab for root - using an empty one

Select an editor.  To change later, run 'select-editor'.
  1. /bin/nano        <---- easiest
  2. /usr/bin/vim.basic
  3. /usr/bin/vim.tiny

选择 1 ,添加 cron 任务列表

0 2 * * * /root/backup.sh

命令表示每天凌晨 2 点执行backup.sh备份脚本

# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
# 
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
# 
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
# 
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
# 
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
# 
# For more information see the manual pages of crontab(5) and cron(8)
# 
# m h  dom mon dow   command
0 2 * * * /root/backup.sh

粘贴命令到底部, Ctrl+x 保存并退出,再输入 y 确定,回车

立即执行脚本命令

./backup.sh

完成

© 版权声明
THE END
喜欢就支持一下吧!
点赞0发电
评论 抢沙发
头像
尊重知识,真诚评论!
提交
头像

昵称

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

    暂无评论内容