VPS+Rclone 实现自动备份及OneDrive、Google Drive资源对拷

这里主要是为了解决两个问题才写的这篇文章

  1. OneDrive文件拷贝,OneDrive官方是不管是客户端还是网页端,对于别人分享过来的文件都只支持下载,那么我们要把分享过来的文件保存在自己的网盘就需要一次上传操作,这里就是把下载、上传这两个动作实现自动化。
  2. VPS上的网站数据库实现备份,之前数据都是使用快照存储在服务商那边,最近有出现各家删机的操作,就需要自己手里有份数据备份,网盘是为数不多的选择。

环境准备

  • VPS系统:本次演示使用cen­tos7
  • Rclone官网:https://rclone.org/
  • Windows设备一台
  • Windows版Rclone安装包一份

Reclone 安装

这次采用rclone官方一键脚本安装,将下面代码复制进ssh执行即可

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

获取OneDrive授权码

https://rclone.org/downloads/ 下载 win­dows平台的Rclone
解压下载到的文件,在解压出来的文件夹中使用shift+鼠标右键->在此处打开power shell
在弹出的命令行界面执行以下代码

rclone authorize "onedrive"

这时会弹出浏览器,登录你的 OneDrive,获取 code,形式如:

 {“access_token”:”<code>”,”expiry”:”time”}

注:【”<code>”,”expiry”:”time”】这部分代表你的 code 相关数据,并不是最终代码,具体请参照您获取到的 code 代码段!

这一段保存好,可以反复使用。

连接 onedrive 网盘

shell工具通过ssh 连接vps,输入

rclone config

#~:rclone config
No remotes found - make a new one
n) New remote 连接新远程网盘
s) Set configuration password
q) Quit config
n/s/q> n


name> onedrive1 随便起个名字

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
 1 / Alias for a existing remote
   \ "alias"
 2 / Amazon Drive
   \ "amazon cloud drive"
 3 / Amazon S3 Compliant Storage Providers (AWS, Ceph, Dreamhost, IBM COS, Minio)
   \ "s3"
 4 / Backblaze B2
   \ "b2"
 5 / Box
   \ "box"
 6 / Cache a remote
   \ "cache"
 7 / Dropbox
   \ "dropbox"
 8 / Encrypt/Decrypt a remote
   \ "crypt"
 9 / FTP Connection
   \ "ftp"
10 / Google Cloud Storage (this is not Google Drive)
   \ "google cloud storage"
11 / Google Drive
   \ "drive"
12 / Hubic
   \ "hubic"
13 / JottaCloud
   \ "jottacloud"
14 / Local Disk
   \ "local"
15 / Mega
   \ "mega"
16 / Microsoft Azure Blob Storage
   \ "azureblob"
17 / Microsoft OneDrive
   \ "onedrive"
18 / OpenDrive
   \ "opendrive"
19 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH)
   \ "swift"
20 / Pcloud
   \ "pcloud"
21 / QingCloud Object Storage
   \ "qingstor"
22 / SSH/SFTP Connection
   \ "sftp"
23 / Webdav
   \ "webdav"
24 / Yandex Disk
   \ "yandex"
25 / http Connection
   \ "http"
Storage> 18 这里连接的onedrive,所以选18(注意这里的选项随时会变,按照自己实际情况操作)

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 填n
Chunk size to upload files with - must be multiple of 320k.
Above this size files will be chunked - must be multiple of 320k. Note
that the chunks will be buffered into memory.
Enter a size with suffix k,M,G,T. Press Enter for the default ("10M").
chunk_size> 直接回车就好

The ID of the drive to use
Enter a string value. Press Enter for the default ("").
drive_id> 直接回车就好
The type of the drive ( personal | business | documentLibrary )
Enter a string value. Press Enter for the default ("").
drive_type> 直接回车就好

By default rclone will hide OneNote files in directory listings because
operations like "Open" and "Update" won't work on them.  But this
behaviour may also prevent you from deleting them.  If you want to
delete OneNote files or otherwise want them to show up in directory
listing, set this option.
Enter a boolean value (true or false). Press Enter for the default ("false").
expose_onenote_files> 直接回车就好
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

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> 这里填入刚刚电脑浏览器获取的OneDrive授权码。

选择类型

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

Found 1 drives, please select the one you want to use:
0: OneDrive (business) id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Chose drive to use:> 这里检测到你账户拥有的OneDrive,填0
Found drive 'root' of type 'business', URL: https://lookertbsteduau-my.sharepoint.com/personal/xxxxxxxxx/Documents
Is that okay?
y) Yes
n) No
y/n> 填y确认

[one]
type = onedrive
token = {"access_token":"xxxxxxxxxxx"}
drive_id = xxxxxxxxxxxx
drive_type = business

y) Yes this is OK
e) Edit this remote
d) Delete this remote
y/e/d> 再填y确认就完事了

然后我们再输入

rclone config

即可看到刚刚添加的网盘了。

然后重复上述步骤,再连接一个 onedrive。

两个网盘都添加完成后,就可以正式开始对拷了

退出 rclone,然后输入

screen rclone copy onedrive1: onedrive2:

就能断开 ssh 让它后台备份了。(也可使用screen挂在后台具体参考下面文章)

付费阅读0.5
109/109

配合 crontab 定时执行,比如每天 4:44 执行一次,日志写到 /root/rclone.log。执行 crontab -e,i,添加下面代码进去,按一下 Esc,然后输入 :wq 回车保存退出。

44 4 * * * /usr/bin/rclone sync rclone copy onedrive1:/galgame onedrive2:/galgame >> /root/rclone.log 2>&1

注:不需要日志的话把 >> /root/rclone.log 2>&1 去掉。

写在最后

需要提醒的是,因为即使是对拷,用的也是服务器本机的流量,包括下载和上传的流量。所以如果你想拷 50G 的文件,就要花费至少 100G 的流量,这是需要计费的。
对本文仍有疑问的,请在评论区留言

附rclone 命令大全

如果用来备份或者同步文件,用到的命令可能就两个:rclone_copy 和 rclone_sync。

rclone_copy 是复制,配合附加参数 –ignore-existing 可以假装增量备份;

rclone_sync 是源目录同步到目标目录,使两者一致,会删除目标目录多余内容,测试时可以加上 –dry-run,检查会复制和删除那些文件。

rclone 目前可用命令如下

参考:

© 版权声明
THE END
喜欢就支持以下吧
点赞20赞赏
分享
评论 共1条

请登录后发表评论