利用 Rclone备份 VPS 数据到 onedrive

数据无价,时常备份服务器中的数据是良好的习惯。Rclone (“rsync for cloud storage”) 是一个命令行程序,用于将文件、目录与不同云存储提供商进行同步。利用 Rclone备份 VPS中的数据到onedrive、google drive等云盘中,保障我们的数据安全。

Github地址

https://github.com/rclone/rclone

准备onedrive的授权

  1. 准备一个rclone支持的云盘账户,我这里选用的是onedrive,需要进行oendrive授权。
  2. Rclone下载页面下载与自己电脑系统对应的程序,我这里选择的是macOS-ARM-64 Bit
  3. 将软件下载到Downloads(下载)文件夹后,解压,并修改文件夹名字为rclone
  4. Mac上打开软件Terminal(终端),输入以下命令
cd Downloads/rclone  # Downloads/rclone 为解压文件的路径
./rclone authorize "onedrive" #如果提示错误,需要进 系统偏好-安全与隐私 中允许软件运行
  1. 成功后会自动弹出浏览器进入onedrive登录界面,授权完成后,在Terminal窗口可以看到授权码的出现,将授权码复杂下来备用,需要包含大括号。
{"access_token":"EwCAA8l6BAAUwihrrCrmQ4wuIJX5mbj7rQla6TUAAS7oJFl1pmRc4whIlOj5am6oPQ+zf1kOcygsi5JHL2IgXh55prjh3r6P/asfdjkhasjkfd/kHywyZnEvM8kDr55B1J//SXLJ1sGNsNR9vH0Rmz9xMrnl6GFrF8z+UQxi+GL3L3MpBN3NenacA0+pmVXKk8hghj64a8xxEFhlc+b0LJCp4yMg+96548752OPQwLZEPikbJ1zB5Wm5IUzuq03zNKa5lOicGET0l8zWW+itYg2Ca+mCS9DRm5e8NcPL5yiLZmRIm4vCI3pIQGKxEKghQG2lLmjMnWJj8BMotxH9Px2/XWs2TJ3BdLbuX2EjdYy8MNoBbaYXafe3uHx!UpdOwFHCiKnJqYomn*8cCFNP7RM1W8DMzXNBAlgTnuaRo6v7xjxFX3xIrXWonyCk*l1Z7glOXEmRS","expiry":"2022-07-12T15:47:47.8171297+08:00"}

配置Rclone与onedrive连接

  1. 安装rclone,SSH进入VPS后,输入以下命令
curl https://rclone.org/install.sh | sudo bash
  1. 然后输入rclone config
  2. 由于刚安装还没有配置,在以下弹出的消息后选择n
No remotes found - make a new one
n) New remote
s) Set configuration password
q) Quit config
n/s/q> n
  1. 为新创建的连接取一个名字,我这里选择backup2onedrive
Enter name for new remote.
name> backup2onedrive
  1. 然后选择自己要同步的网盘,这里选择了32(随着更新,数字可能会发生变化,请根据提示选择)
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, Digital Ocean, Dreamhost, Huawei OBS, IBM COS, IDrive e2, Lyve Cloud, Minio, Netease, RackCorp, Scaleway, SeaweedFS, StackPath, Storj, Tencent COS 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 / Hubic
  \ (hubic)
24 / In memory object storage system.
  \ (memory)
25 / Internet Archive
  \ (internetarchive)
26 / Jottacloud
  \ (jottacloud)
27 / Koofr, Digi Storage and other Koofr-compatible storage providers
  \ (koofr)
28 / Local Disk
  \ (local)
29 / Mail.ru Cloud
  \ (mailru)
30 / Mega
  \ (mega)
31 / Microsoft Azure Blob Storage
  \ (azureblob)
32 / Microsoft OneDrive
  \ (onedrive)
33 / OpenDrive
  \ (opendrive)
34 / OpenStack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
  \ (swift)
35 / Pcloud
  \ (pcloud)
36 / Put.io
  \ (putio)
37 / QingCloud Object Storage
  \ (qingstor)
38 / SSH/SFTP
  \ (sftp)
39 / Sia Decentralized Cloud
  \ (sia)
40 / Storj Decentralized Cloud Storage
  \ (storj)
41 / Sugarsync
  \ (sugarsync)
42 / Transparently chunk/split large files
  \ (chunker)
43 / Union merges the contents of several upstream fs
  \ (union)
44 / Uptobox
  \ (uptobox)
45 / WebDAV
  \ (webdav)
46 / Yandex Disk
  \ (yandex)
47 / Zoho
  \ (zoho)
48 / premiumize.me
  \ (premiumizeme)
49 / seafile
  \ (seafile)
Storage> 32
  1. Client ID不需要填写,直接 回车
OAuth Client Id
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_id>
  1. Client Secret也不需要填写,直接 回车
OAuth Client Secret
Leave blank normally.
Enter a string value. Press Enter for the default ("").
client_secret>
  1. 选择OneDrive服务器区域,我使用用的是国际版输入1,如果使用的是世纪互联,那就输入4
Choose national cloud region for OneDrive.
Choose a number from below, or type in your own value
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
  1. 高级配置,选择 n
Edit advanced config? (y/n)
y) Yes
n) No (default)
y/n> n
  1. 自动配置,也选择n
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
y/n> n
  1. 提示输入授权码,这时候将之前准备的授权码输入(记得包含大括号)。
Enter a string value. Press Enter for the default ("").
config_token> {"access_token":"EwCAA8l6BAAUwihrrCrmQ4wuIJX5mbj7rQla6TUAAS7oJFl1pmRc4whIlOj5am6oPQ+zf1kOcygsi5JHL2IgXh55prjh3r6P/asfdjkhasjkfd/kHywyZnEvM8kDr55B1J//SXLJ1sGNsNR9vH0Rmz9xMrnl6GFrF8z+UQxi+GL3L3MpBN3NenacA0+pmVXKk8hghj64a8xxEFhlc+b0LJCp4yMg+96548752OPQwLZEPikbJ1zB5Wm5IUzuq03zNKa5lOicGET0l8zWW+itYg2Ca+mCS9DRm5e8NcPL5yiLZmRIm4vCI3pIQGKxEKghQG2lLmjMnWJj8BMotxH9Px2/XWs2TJ3BdLbuX2EjdYy8MNoBbaYXafe3uHx!UpdOwFHCiKnJqYomn*8cCFNP7RM1W8DMzXNBAlgTnuaRo6v7xjxFX3xIrXWonyCk*l1Z7glOXEmRS","expiry":"2022-07-12T15:47:47.8171297+08:00"}
  1. Rclone连接到onedrive网盘,输入y确认。
Drive OK?
Found drive "root" of type "personal"
URL: https://onedrive.live.com/?cid=a50d72ae9
y) Yes (default)
n) No
y/n> y
  1. 配置完成后,显示所有信息,选择 y 确认。
[backup2onedrive]
type = onedrive
token = {"access_token":...
drive_id = a50d72ae9
drive_type = personal
--------------------
y) Yes this is OK (default)
e) Edit this remote
d) Delete this remote
y/e/d> y
  1. 完成后程序退回到了初始命令处,输入 q 退出。
Current remotes:
Name                 Type
====                 ====
backup2onedrive     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备份 脚本

  1. 首先确定你需要同步的文件夹,我这里选择的是/home/backup
  2. SSH进入VPS,目前我的路径为/root
  3. 输入以下命令创建脚本
vim backup.sh
  1. 按一下键盘上的 i,进入编辑模式。并输入以下内容。其中/home/backup为VPS上需要备份的文件夹,backup/backup_for_vps为onedrive上的存放路径。需要注意的是,下面的命令为同步,即如果backup/backup_for_vps下有其他文件,同步完成后会被抹除,只剩下VPS同步过去的文件。所以建议路径指向onedrive中的一个空文件夹。
#!/bin/bash
rclone sync /home/backup backup2onedrive:backup/backup_for_vps
  1. 完成编写后,按键盘Esc后输入:wq即可保存并退出编辑。
  2. 为创建的脚本赋予执行权限
chmod +x backup.sh
  1. 此时如果输入./backup.sh即可运行该脚本,并完成文件的同步。但是我们需要设置一个定时任务,让脚本以指定频率自动运行。输入以下命令
crontab -e
  1. (如果弹出指令,让你选择编辑工具,选择vim,)进入到定时任务的编辑文件后,按一下键盘上的 i,进入编辑模式。并在最后一行,加入以下内容。前面五个星号的位置可以修改 * * * * *,从前往后分别对应 分钟(0-59) 小时(0-23) 一个月中第几天(1-31) 月份(1-12) 星期(0-6)
30 2 * * 2,4,6  /root/backup.sh >> /root/backup.log 2>&1 #周二、四、六的凌晨2点30分运行脚本,并将运行日志记录到backup.log中
  1. 完成后,即可在指定时间结束后,在onedrive中查看备份的结果。建议时不时上onedrive中查看同步的结果,避免Rclone出错未成功备份数据。

参考文章

https://walixz.com/rclone-sync-onedrive.html

4 评论

留下评论

您的电子邮箱地址不会被公开。 必填项已用*标注

    • 我看了一眼,一个月到现在,按照设置的时间稳定备份,看上去挺可靠的。但rclone这个是同步,会覆盖掉文件夹下的文件,所以还是要新建一个文件夹放备份保险一点。