通过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_id和client_secret直接回车默认即可,
之后会出现OneDrive区域选择linux定时器的使用,如下:
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选择全球linux伊甸园,
之后是选择是否进行中级配置,保持默认回车即可:
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
由于你的服务器并没有图形化界面难以使用浏览器打开网页linux定时器的使用,这儿须要选择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>
授权码须要在本地笔记本上获取,访问按照你的笔记本系统版本下载对应的Rclone,下载完成后解压,步入储存Rclone的目录可以看见rclone.exe的可执行程序,在空白处按住shift键键盘右击在Windows终端打开或在此处打开PowerShell窗口,执行如下命令:
./rclone.exe authorize "onedrive"
浏览器应当会手动打开,假如浏览器没有打开可以按照提示复制地址访问。假如浏览器登入了OneDrive的话会直接显示授权确认信息,假如没有登陆的话会弹出登入页面,登陆OneDrive后也会显示授权确认信息。选择接受后在本地命令行会显示一串JSON格式的授权信息,复制授权信息包括首尾的{}嵌入式linux培训,到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保存配置。
挂载首先安装fuse
# CentOS/Fedora
yum install -y fuse fuse3
# Ubuntu/Debian
apt install -y fuse fuse3
挂载为本地c盘
# 新建本地目录,即下面的 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为初始化配置填的name,Folder为OneDrive里的文件夹,LocalFolder为VPS上的本地文件夹。
挂载过程中出现NOTICE:Onedriveroot'/xxx':--vfs-cache-modewritesorfullisrecommendedforthisremot错误,可以无视。
挂载成功后,新建一个SSH联接窗口输入df-h命令即可查看
卸载c盘
fusermount -qzu LocalFolder
开机自启
使用如下命令新建一个systemd配置文件,适用于CentOSDebianUbuntu
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
./rclonemount:"添加云盘时的配置名称":"云盘指定目录""挂载至本地的目录"
[--添加参数]
--allow-other:准许非当前rclone用户外其它用户进行访问
--attr-timeout5m:文件属性缓存,(大小,更改时间等)的时间。假如VPS配置比较低,建议适当提升这个值,防止过多内核交互,增加资源占用。
--vfs-cache-modefull:开启VFS文件缓存,可降低rclone与API交互,同时可提升文件读写效率
--vfs-cache-max-age24h:VFS文件缓存时间,这儿设置24小时,假如文件极少修改,建议设置更长时间
--vfs-cache-max-size5G:VFS文件缓存上限大小,请依照服务器剩余c盘自行调节
--vfs-read-chunk-size-limit300M:分块读取大小,这儿设置的是300M,可以提升文件读取的效率,例如1G的文件,大致分为10个块进行读取,但与此同时API恳求次数也会增多
--buffer-size300M:设置显存缓存,请依照服务器显存大小自行设置
--daemon:后台运行程序
之后使用如下命令设置开机自启:
systemctl enable rclone --now
# 更多
重启:systemctl restart rclone
停止:systemctl stop rclone
状态:systemctl status rclone
假如你想挂载多个云盘,这么将systemd配置文件的rclone.service重命名成不冲突的文件名即可,如rclone1.service。
使用私有API
使用Rclone给OneDrive传输文件时可能会碰到速率十分慢、断联等一些问题,其症结是触发了OneDriveAPI的限制,而默认的Rclone外置API因为十分多人在同时在使用所以那些问题也就更加显著。使用自建的私有API联接OneDrive可急剧改善这种情况,并且对于三个月的Office365E5开发者试用版,平常时常用用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
参考