rclone那点事儿

<p>在没有图形界面的服务器上,或不想使用客户端软件时,如何方便地使用网盘是个问题。</p>
<p>Rclone 就是为此而生的,它可以方便在命令行下挂载网盘,目前已经支持包括 Google Drive、OneDrive 在内的几十个网盘,而且除了速度慢一点外,可以模拟本地磁盘,对于容量有限的 VPS 来说就是免费扩容,配合 aria2、Transmission 等工具,就可以打造离线下载服务器。</p>

<h1 id="安装">安装</h1><p>Debian 的官方源中 Rclone 版本过于陈旧,建议到官网下载:https://rclone.org/downloads/</p>
<p>或者脚本一键安装:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl https://rclone.org/install.sh | sudo bash</span></span>
</pre></td></tr></table></figure>

<h1 id="配置">配置</h1><p>Rclone 提供了简单的交互式配置方式,运行 rclone config 即可,一般就是打开浏览器获取一个 token,网上有很多教程,在此就不赘述了。</p>
<p>以 OneDrive for Business 为例,主要说明一下如何在没有图形界面的情况下获取 token。</p>
<p>一种是先用其他带图形界面的系统配置一次,直接把 token 复制过去。</p>
<p>另一种就是在命令行下执行到这一步时:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">8</span>
<span class="line">9</span>
</pre></td><td class="code"><pre><span class="line">Use auto config?</span>
<span class="line"> Say Y if not sure</span>
<span class="line">
Say N if you are working on a remote or headless machine</span>
<span class="line">y) Yes</span>
<span class="line">n) No</span>
<span class="line"><span class="meta">y/n></span><span class="bash"> y</span></span>
<span class="line">If your browser doesn't open automatically go to the following link: http://127.0.0.1:53682/auth?state=xxxxxxxxxxxxxxxxxxxxxx</span>
<span class="line">Log in and authorize rclone for access</span>
<span class="line">Waiting for code...</span>
</pre></td></tr></table></figure>

<p>选择 y 的话理论上会打开浏览器访问那个地址,当然由于是服务器不可能打开浏览器,这时我们重开一个 SSH 连接,并用 curl 访问:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
<span class="line">2</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> curl http://127.0.0.1:53682/auth?state=xxxxxxxxxxxxxxxxxxxxxx</span></span>
<span class="line"><a href="https://login.microsoftonline.com/common/oauth2/v2.0/authorize?access_type=offline&amp;client_id=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&amp;redirect_uri=http%3A%2F%2Flocalhost%3A53682%2F&amp;response_type=code&amp;scope=Files.Read+Files.ReadWrite+Files.Read.All+Files.ReadWrite.All+offline_access&amp;state=xxxxxxxxxxxxxxxxxxxxxx">Temporary Redirect</a>.</span>
</pre></td></tr></table></figure>

<p>复制 href 中的链接,对其 URL 解码,可以使用 Python 或其它工具,如:https://tool.chinaz.com/tools/urlencode.aspx</p>
<p>接着(客户端)使用浏览器访问解码后的 URL 并登陆账号,然后会重定向到一个本地 URL,在服务器上用 curl 访问此 URL。</p>
<p>这时 Rclone 的配置应该会自动进行到下一步,其它照常配置即可。如果不行就多试几次。</p>
<h1 id="挂载">挂载</h1><p>安装 FUSE:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> sudo apt install fuse</span></span>
</pre></td></tr></table></figure>

<p>将名为 onedrive 的配置挂载到 /mnt/onedrive 下:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> rclone mount onedrive:/ /mnt/onedrive --vfs-cache-mode full --vfs-cache-max-age 24h --vfs-cache-max-size 12G</span></span>
</pre></td></tr></table></figure>

<p>其中 vfs-cache-mode 可选参数为 off|minimal|writes|full,默认为 off,写入文件时无法随机寻址,因此无法用于 aria2、Transmission 等分块下载软件。writesfull 则可以很好地模拟本地文件系统,writes 模式下只读打开的文件无缓存,full 模式下读写均有缓存。</p>
<p>vfs-cache-max-age 设置缓存保存时间,vfs-cache-max-size 设置缓存大小。</p>
<p>参数 -vv 可查看 log 以排查错误,--daemon 以守护进程运行等。更多可参考:https://rclone.org/commands/rclone_mount/</p>
<p>查看一下是否已经挂载:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> df -h</span></span>
<span class="line">Filesystem Size Used Avail Use% Mounted on</span>
<span class="line">onedrive: 5.0T 18G 5.0T 1% /mnt/onedrive</span>
</pre></td></tr></table></figure>

<h1 id="开机自启">开机自启</h1><p>systemd 脚本:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
<span class="line">2</span>
<span class="line">3</span>
<span class="line">4</span>
<span class="line">5</span>
<span class="line">6</span>
<span class="line">7</span>
<span class="line">8</span>
<span class="line">9</span>
<span class="line">10</span>
<span class="line">11</span>
<span class="line">12</span>
<span class="line">13</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> cat /lib/systemd/system/rclone.service</span></span>
<span class="line">[Unit]</span>
<span class="line">Description=Rclone Service</span>
<span class="line">After=network.target</span>
<span class="line"></span>
<span class="line">[Service]</span>
<span class="line">Type=simple</span>
<span class="line">User=username</span>
<span class="line">ExecStart=/usr/bin/rclone mount onedrive:/ /mnt/onedrive --vfs-cache-mode full --vfs-cache-max-age 24h --vfs-cache-max-size 12G</span>
<span class="line">ExecStopPost=/bin/fusermount -qzu /mnt/onedrive</span>
<span class="line"></span>
<span class="line">[Install]</span>
<span class="line">WantedBy=multi-user.target</span>
</pre></td></tr></table></figure>

<p>其中 User 设置为所运行的用户。</p>
<p>我实际使用时,停止进程后无法自动取消挂载,文件夹异常,因此用 fusermount -qzu /mnt/onedrive 取消挂载。</p>
<p>另外,开机时 Transmission 可能会在 Rclone 之前运行,此时目录未挂载,Transmission 就会出错,于是修改其 systemd 脚本:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span>
</pre></td><td class="code"><pre><span class="line"><span class="meta">$</span><span class="bash"> vim /lib/systemd/system/transmission-daemon.service</span></span>
</pre></td></tr></table></figure>

<p>将 After=network.target 修改为 After=network.target rclone.service,这样 Transmission 就在 Rclone 后启动。</p>

卸载

卸载很简单就是直接umount 掉就行了

#卸载
umount /mnt/google-drive-test
#确认是否已卸载
df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            483M     0  483M   0% /dev
tmpfs            99M   13M   87M  13% /run
/dev/sda1       9.8G  7.6G  1.8G  82% /
tmpfs           495M     0  495M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           495M     0  495M   0% /sys/fs/cgroup
添加新评论