通过 DNMP 搭建的环境建站虽然没有宝塔面板方便但是对系统环境影响最小,可是备份是个问题。如果用的是大厂云服务器可以通过设置定时系统快照任务来备份,但是轻量云却不支持设置定时快照(腾讯云),宝塔面板自带很多易用的备份插件,我常用的是 OneDrive 备份,于是想到可以通过 Rclone 在服务器上挂载 OneDrive,然后编写一个简易备份脚本配合 Cron 任务来实现定时备份。

下载安装

使用脚本(也支持容器,但是我没看懂怎么用)在 Linux/macOS/BSD 系统上安装 rclone,运行:

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

初始化配置

安装完成后输入:

rclone config

可以看到:

No remotes found, make a new one?
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n

name> onedrive

输入 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, Digital Ocean, Dreamhost, IBM COS, Lyve Cloud, Minio, RackCorp, SeaweedFS, and Tencent COS
   \ (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 / Compress a remote
   \ (compress)
12 / Dropbox
   \ (dropbox)
13 / Encrypt/Decrypt a remote
   \ (crypt)
14 / Enterprise File Fabric
   \ (filefabric)
15 / FTP Connection
   \ (ftp)
16 / Google Cloud Storage (this is not Google Drive)
   \ (google cloud storage)
17 / Google Drive
   \ (drive)
18 / Google Photos
   \ (google photos)
19 / Hadoop distributed file system
   \ (hdfs)
20 / Hubic
   \ (hubic)
21 / In memory object storage system.
   \ (memory)
22 / Jottacloud
   \ (jottacloud)
23 / Koofr, Digi Storage and other Koofr-compatible storage providers
   \ (koofr)
24 / Local Disk
   \ (local)
25 / Mail.ru Cloud
   \ (mailru)
26 / Mega
   \ (mega)
27 / Microsoft Azure Blob Storage
   \ (azureblob)
28 / Microsoft OneDrive
   \ (onedrive)
29 / OpenDrive
   \ (opendrive)
30 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ (swift)
31 / Pcloud
   \ (pcloud)
32 / Put.io
   \ (putio)
33 / QingCloud Object Storage
   \ (qingstor)
34 / SSH/SFTP Connection
   \ (sftp)
35 / Sia Decentralized Cloud
   \ (sia)
36 / Storj Decentralized Cloud Storage
   \ (storj)
37 / Sugarsync
   \ (sugarsync)
38 / Transparently chunk/split large files
   \ (chunker)
39 / Union merges the contents of several upstream fs
   \ (union)
40 / Uptobox
   \ (uptobox)
41 / Webdav
   \ (webdav)
42 / Yandex Disk
   \ (yandex)
43 / Zoho
   \ (zoho)
44 / http Connection
   \ (http)
45 / premiumize.me
   \ (premiumizeme)
46 / seafile
   \ (seafile)
Storage> 28

输入你要挂载的网盘序号,我要挂载的 OneDrive 是 28

⚠️注意:序号不固定,请自行确认

client_idclient_secret 直接回车默认即可,

然后会出现 OneDrive 区域选择,如下:

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 21Vianet in China
   \ (cn)
region> 1

根据你自己的 OneDrive 版本来,这里我输入 1 选择全球,

然后是选择是否进行高级配置,保持默认回车即可:

Edit advanced config?
y) Yes
n) No (default)

接着是自动配置选择,如下:

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

因为你的服务器并没有图形化界面无法使用浏览器打开网页,这里需要选择 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> 

授权码需要在本地电脑上获取,访问 https://rclone.org/downloads/ 根据你的电脑系统版本下载对应的 Rclone,下载完成后解压,进入存放 Rclone 的目录可以看到 rclone.exe 的可执行程序,在空白处按住 shift 键鼠标右击 在 Windows 终端打开在此处打开 PowerShell 窗口,执行如下命令:

./rclone.exe authorize "onedrive"

浏览器应该会自动打开,如果浏览器没有打开可以根据提示复制地址访问。如果浏览器登录了 OneDrive 的话会直接显示授权确认信息,如果没有登录的话会弹出登录页面,登录 OneDrive 后也会显示授权确认信息。选择 接受 后在本地命令行会显示一串 JSON 格式的授权信息,复制授权信息包括首尾的 {},到 Linux 终端粘贴。

然后设置 OneDrive 的连接类型,如下:

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,

然后会显示找到的 OneDrive 账户,如下:

Drive OK?

Found drive "root" of type "business"
URL: https://xxxxxx-my.sharepoint.com/personal/xxxxxx/Documents


y) Yes (default)
n) No
y/n> 

确认无误后输入 y 或者直接回车,接着会显示你选择的配置信息,确认无误后输入 y 保存配置。

挂载

  1. 首先安装 fuse
    # CentOS/Fedora
    yum install -y fuse fuse3
    # Ubuntu/Debian
    apt install -y fuse fuse3
    
  2. 挂载为本地磁盘
    # 新建本地目录,即下面的 LocalFolder
    mkdir /onedrive
    # 挂载为磁盘,下面的 DriveName、Folder、LocalFolder 参数根据说明自行替换
    rclone mount DriveName:Folder LocalFolder --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000
    

    DriveName为初始化配置填的nameFolderOneDrive里的文件夹,LocalFolderVPS上的本地文件夹。

    挂载过程中出现 NOTICE: One drive root '/xxx': --vfs-cache-mode writes or full is recommended for this remot 错误,可以无视。

  3. 挂载成功后,新建一个 SSH 连接窗口输入 df -h 命令即可查看

  4. 卸载磁盘

    fusermount -qzu LocalFolder
    

开机自启

使用如下命令新建一个 systemd 配置文件,适用于 CentOS\Debian\Ubuntu

cat > /etc/systemd/system/rclone.service <<EOF
[Unit]
Description=Rclone
After=network-online.target

[Service]
User=root
Type=simple
# 替换成你的手动运行命令,例:
ExecStart=/usr/bin/rclone mount DriveName:Folder LocalFolder --copy-links --allow-other --allow-non-empty --umask 000 --attr-timeout 5m --vfs-cache-mode full --vfs-cache-max-age 24h --vfs-cache-max-size 5G --buffer-size 300M
ExecStop=fusermount -qzu LocalFolder
Restart=always

[Install]
WantedBy=default.target
EOF

./rclone mount:"添加网盘时的配置名称":"网盘指定目录" "挂载至本地的目录"

[--添加参数]
--allow-other:允许非当前 rclone 用户外其它用户进行访问
--attr-timeout 5m:文件属性缓存,(大小,修改时间等)的时间。如果 VPS 配置比较低,建议适当提高这个值,避免过多内核交互,降低资源占用。
--vfs-cache-mode full:开启 VFS 文件缓存,可减少 rclone 与 API 交互,同时可提高文件读写效率
--vfs-cache-max-age 24h:VFS 文件缓存时间,这里设置 24 小时,如果文件很少更改,建议设置更长时间
--vfs-cache-max-size 5G:VFS 文件缓存上限大小,请根据服务器剩余磁盘自行调节
--vfs-read-chunk-size-limit 300M:分块读取大小,这里设置的是300M,可以提高文件读取的效率,比如1G的文件,大致分为10个块进行读取,但与此同时 API 请求次数也会增多
--buffer-size 300M:设置内存缓存,请根据服务器内存大小自行设置
--daemon:后台运行程序

然后使用如下命令设置开机自启:

systemctl enable rclone --now

# 更多
重启:systemctl restart rclone
停止:systemctl stop rclone
状态:systemctl status rclone

如果你想挂载多个网盘,那么将 systemd 配置文件的 rclone.service 重命名成不冲突的文件名即可,如 rclone1.service

使用私有 API

使用 Rclone 给 OneDrive 传输文件时可能会遇到速度非常慢、断联等一些问题,其根源是触发了 OneDrive API 的限制,而默认的 Rclone 内置 API 由于非常多人在同时在使用所以这些问题也就愈发明显。使用自建的私有 API 连接 OneDrive 可大幅改善这些情况,而且对于三个月的 Of­fice 365 E5 开发者试用版,平时偶尔用用 Rclone 还会有自动续订的可能性,比起刻意刷 API 的方式更为安全和稳定。此外自建的 API 还可以给其它的账号和其它应用使用。

API 创建教程请参考:Rclone 进阶使用教程 – 自建私有 API 挂载 OneDrive,同类教程很多,这里不做赘述。

简易备份脚本

一个简易的备份脚本,可以打包备份目录和导出数据库,利用 Cron 定时任务实现定时备份,保存为 backup.sh 执行即可。

#!/bin/bash
# 要备份文件目录,绝对路径
backpath='/opt'
# 备份到哪里,绝对路径
savepath='/onedrive'
# 备份文件名
name="opt"
# 时间戳
date=`date "+%Y%m%d%H%M%S"`
# 拼接文件名
filename="${name}_${date}.tgz"

cd $backpath
# 数据库备份,多个数据库依次类推
mkdir database
cd database
# 例:docker exec -it {container_name} mysqldump -u{db_user} -p{db_password} {database} > {file_path}
docker exec -it mysql mysqldump -uroot -pmy-passwrd mydatabase > mydatabase.sql
# 目录打包备份
cd ..
tar -czf $filename --exclude=containerd $backpath
# 设置备份保留几份,这里是7天
find $savepath -type f -mtime +7 -exec rm -f {} \;

# 移动并清理残留
mv $filename $savepath
rm -rf database
rm -f $filename

参考

https://rclone.org/install/

https://www.misterma.com/archives/900/


Never give up your dreams.