此前备份一直是用宝塔完成的每日自动备份,但是宝塔在上半年被爆出在隐私方面的重大问题。且在实际环境中由于不明原因,我使用宝塔设置的防火墙策略会在重启后失效,直接导致某些敏感服务可以被公网访问。最终在近日忍痛将服务器重装,并使用oneinstack完成LNMP环境的配置。
Rclone可以将Onedrive等云盘映射成一个linux目录,这样一来就可以让备份灵活方便很多。
Onestack+Rclone实现云备份
oneinstack自带mysql备份脚本,在oneinstack目录下的“backup_setup.sh”
oneinstack已经内建了对于上述OSS服务的云备份支持,但是本文的需求是:使用onedrive完成备份,所以需要首先配置rclone
思路如下:
oneinstack的脚本中可以设定备份目的地,1为localhost也就是本地文件夹,只需要将onedrive映射到一个文件夹,再将该脚本的备份路径设定为该文件夹即可。
安装rclone
首先是安装rclone,GITHUB项目传送门:https://github.com/rclone/rclone
如果不希望一步步安装,Rclone提供了一个脚本一键安装,如下:
curl https://rclone.org/install.sh | sudo bash
当然你也可以使用包管理器完成安装,不过版本可能会比较低。
Debian/Ubuntu: apt install rclone
CentOS: yum install rclone
【可选】获取微软鉴权TOKEN
安装完成后,首先需要获取OneDrive的授权TOKEN,建议在本地也下载一份rclone,传送门:https://downloads.rclone.org/rclone-current-windows-amd64.zip
随后解压,在目录中打开powershell,输入如下指令
.\rclone.exe authorize "onedrive"
接下来会输出一串URL,在游览器中打开它:
不要关闭这个窗口,在游览器中打开链接以后会要求登陆M$账号,并要求授权,登陆并授权即可。随后窗口中会出现很长的一串东西,大概长这样:
这些便是用于登陆网盘的token,复制下来这些待用,如果你是看着这篇文章一步步做,建议不要关闭这个窗口。
配置rclone
回到服务器中,输入 rclone config
# 后面的文字为注释
2022/07/08 22:55:17 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> onedrive # 输入名字,后面挂载要用。
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
# 这里选择对接的平台,rclone支持对接的网盘/文件存储服务很多,可以到他们的github项目查看,此处不做赘述。
Storage> onedrive # 我们使用的OneDrive,也可以输入其他选项完成其他配置
** See help for onedrive backend at: https://rclone.org/onedrive/ **
Microsoft App Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id> # 一般不需要填写
Microsoft App Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret> # 一般不需要填写
Edit advanced config? (y/n)
y) Yes
n) No
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
n) No
y/n> N # 这里输入n,使用我们刚刚在本地获得的token
For this to work, you will need rclone available on a machine that has a web browser available.
Execute the following on your machine:
rclone authorize "onedrive"
Then paste the result below:
result> {"access_token":"....."} # 这里输入刚刚我们获得的TOKEN
Choose a number from below, or type in an existing value
1 / OneDrive Personal or Business
\ "onedrive"
2 / Root Sharepoint site
\ "sharepoint"
3 / Type in driveID
\ "driveid"
4 / Type in SiteID
\ "siteid"
5 / Search a Sharepoint site
\ "search"
Your choice> 1 # 我使用的是个人用户,如果你是购买的5T Onedrive啥的请根据情况填写
Found 1 drives, please select the one you want to use:
0: (personal) id=[...]
Chose drive to use:> 0 # 一个账号对应多个onedrive云盘的时候输入对应的编号选择云盘
Is that okay?
y) Yes
n) No
y/n> y # 完成配置
--------------------
[onedrive]
token = {"access_token":"..."}
drive_id = [...]
drive_type = personal
--------------------
y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> y # 再次确认
Current remotes:
Name Type
==== ====
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 # 退出配置
完成配置以后,输入 rclone config 应该能看到你刚刚配置的OneDrive
挂载目录
首先,创建一个文件夹用于挂载OneDrive,教程用例是”/mnt/od”,这个自行创建不做赘述。
随后执行指令,将OD挂载到刚刚的文件夹中:
rclone mount [在OD配置过程中设定的名字,本文是onedrive]: /mnt/od --allow-other --daemon
这一步有可能会报错:Failed to create file system for “od:”: didn’t find section in config file,该错误是因为rclone依赖fuse包,使用包管理器直接安装再次执行命令即可。
随后挂载完成,cd过去看一看:
现在在这里新建一个叫做“nature.txt”的文件,并写入“NiceNature is Best!”
打开Onedrive,这里使用的onedrive网页版,如果配置正确的话,就可以看到刚刚创建的nature.txt了。
如果需要取消挂载,使用umount指令即可。
umount -a /mnt/od
【可选】配置开机自启
本文完成于7月8日,但是7月9日上午我忘记续费服务器了,所以IDC把我的服务器关机了。续费以后服务器被重启,随后在7月10日检查备份的时候挂载掉了……查了一些资料,发现默认情况下rclone重启就会掉挂载,特此补充。
试着使用以下systemctl指令,没有的话就出门左转看看怎么安装,接下来假设有这条指令。
首先为了以防万一,应该解除先前配置过的挂载,输入 umount -a [挂载目录] 即可
随后,进入到 /etc/systemd/system 目录,随后写一个新文件,这里的名字就叫做 RcloneOD.service
接着就输入以下内容:
[Unit]
Description=OneDrive Cloud Backup # 备注
[Service]
User=root
ExecStart=rclone mount [在OD配置过程中设定的名字,本文是onedrive]: /mnt/od --allow-other # 挂载目录时使用的指令,但是请去掉"--daemon"
Restart=on-abort
[Install]
WantedBy=multi-user.target
随后输入 systemctl enable RcloneOD.service 启用服务(开机自启)
最后手动输入 systemctl start RcloneOD.service 手动开启一次服务即可。
【可选】配置OneInStack备份
回到oneinstack安装目录,执行 bash backup_setup.sh 开始配置oneinstack自带的备份
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
# Setup the backup parameters #
# For more information please visit https://oneinstack.com #
#######################################################################
# OneinStack for CentOS/RedHat 7+ Debian 8+ and Ubuntu 16+ #
# Setup the backup parameters #
# For more information please visit https://oneinstack.com #
#######################################################################
Please select your backup destination:
1. Localhost
2. Remote host
3. Aliyun OSS
4. Qcloud COS
5. UPYUN
6. QINIU
7. Amazon S3
8. Dropbox
Please input numbers:(Default 1 press Enter) 1
Please select your backup content:
1. Only Database
2. Only Website
3. Database and Website
Please input a number:(Default 1 press Enter) 1 # 我们只备份数据库,请根据需求修改此处
Please enter the directory for save the backup file:
(Default directory: /mnt/od): /mnt/od # 这里填写挂载到的目录
Please enter a valid backup number of days:
(Default days: 5): 5 # 这里填写备份留存的天数,此处未作修改
Enter password: # 此处填写mysql的root密码
Please enter one or more name for database, separate multiple database names with commas:
(Default database: [Censored])
You have to backup the content:
Database: db1,db2 # 这里输入需要备份的数据库名,如有多个数据库,使用英文逗号做分割
随后执行 bash backup.sh 测试备份情况,此处有一个坑点:可能会要求你输入密码,此时你需要修改options.conf中的dbrootpwd这一项为root密码。
接着创建一个定时任务,设定每天备份一次即可。
# crontab -e
Select an editor. To change later, run 'select-editor'.
1. /bin/nano <---- easiest
2. /usr/bin/vim.basic
3. /usr/bin/vim.tiny
Choose 1-3 [1]: 1
crontab: installing new crontab
0 1 * * * ~/oneinstack/backup.sh > /dev/null 2>&1 &
到此我们就完成了每日将mysql数据库备份到OneDrive的配置,建议定期上OneDrive检查备份情况。
【可选】配置MongoDB备份
结语
oneinstack的备份脚本存在一些问题,你也可以自己写备份脚本,备份到od挂载目录即可。
也不一定要使用OneDrive,rclone支持很多厂商的云盘。rclone也可以用在扩容方面,可以让硬盘较小的机器多一个数据盘(挂Bt下载这样),实现扩容。