最近在学习Python,为了试验采集某视频网站数据,又重复造“轮子”,在CentOS中使用Rclone挂载OneDrive一直出错。这里记下出错过程以及Rclone挂载过程,避免以后再“重蹈覆辙”。
由于服务器使用的字符界面,所以这里要在本地获取授权token之后复制到服务器中。
客户端授权
在本地Windows电脑上下载rclone,下载地址:https://rclone.org/downloads/。然后解压出来,比如我解压到D盘,文件夹命名rclone,此时点击Win+R,然后输入cmd,确定。再输入以下命令:
1 2 |
cd /d d:\rclone rclone authorize "onedrive" |
输入命令回车后,出现下图界面
b) Business <商业版>
p) Personal <个人版>
根据自己的实际情况选择
安装Rclone
1 |
curl https://rclone.org/install.sh | sudo bash |
更多安装说明:https://rclone.org/install/
1 |
rclone config |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
n) New remote s) Set configuration password q) Quit config n/s/q> n #新建一个挂载 name> lange #这里名字后面要用到 Type of storage to configure. 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 / Local Disk \ "local" 14 / Mega \ "mega" 15 / Microsoft Azure Blob Storage \ "azureblob" 16 / Microsoft OneDrive \ "onedrive" 17 / OpenDrive \ "opendrive" 18 / Openstack Swift (Rackspace Cloud Files, Memset Memstore, OVH) \ "swift" 19 / Pcloud \ "pcloud" 20 / QingCloud Object Storage \ "qingstor" 21 / SSH/SFTP Connection \ "sftp" 22 / Webdav \ "webdav" 23 / Yandex Disk \ "yandex" 24 / http Connection \ "http" Storage>16 #选择16 Microsoft OneDrive Microsoft App Client Id - leave blank normally. client_id> #留空,直接回车 Microsoft App Client Secret - leave blank normally. client_secret> #留空,直接回车 Remote config Choose OneDrive account type? * Say b for a OneDrive business account * Say p for a personal OneDrive account b) Business p) Personal b/p>b #选择b 回车,和客户端获取选择相同 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,y要使用web获取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> #输入客户端获取的token值{"access_token":"XXXXXX},把这段复制粘贴到这里,包含{}。 y) Yes this is OK e) Edit this remote d) Delete this remote y/e/d> y 选择y Current remotes: Name Type ==== ==== lange 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 #选择q退出 |
挂载硬盘
1 |
yum install -y fuse |
注:OVZ机器有的没有开启fuse功能,要找服务商解决
1 2 3 4 |
#新建本地文件夹,路径自己定,即下面的LocalFolder mkdir /home/OneDrive #挂载为磁盘 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上的本地文件夹<使用绝对路径,如:/home/OneDrive>。
如果挂载过程中出现NOTICE: One drive root 'test': poll-interval is not supported by this remote错误,可以无视该错误。
1 |
fusermount -qzu LocalFolder 卸载磁盘 |
开机自动启动
1 |
wget https://www.moerats.com/usr/shell/rcloned |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
#!/bin/bash PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin:~/bin export PATH NAME_BIN="rclone" ### BEGIN INIT INFO # Provides: rclone # Required-Start: $all # Required-Stop: $all # Default-Start: 2 3 4 5 # Default-Stop: 0 1 6 # Short-Description: Start rclone at boot time # Description: Enable rclone by daemon. ### END INIT INFO NAME="" #rclone name名 REMOTE='' #远程文件夹 LOCAL='' #挂载地址 Green_font_prefix="\033[32m" && Red_font_prefix="\033[31m" && Green_background_prefix="\033[42;37m" && Red_background_prefix="\033[41;37m" && Font_color_suffix="\033[0m" Info="${Green_font_prefix}[信息]${Font_color_suffix}" Error="${Red_font_prefix}[错误]${Font_color_suffix}" RETVAL=0 check_running(){ PID="$(ps -C $NAME_BIN -o pid= |head -n1 |grep -o '[0-9]\{1,\}')" if [[ ! -z ${PID} ]]; then return 0 else return 1 fi } do_start(){ check_running if [[ $? -eq 0 ]]; then echo -e "${Info} $NAME_BIN (PID ${PID}) 正在运行..." && exit 0 else fusermount -zuq $LOCAL >/dev/null 2>&1 mkdir -p $LOCAL sudo /usr/bin/rclone mount $NAME:$REMOTE $LOCAL --copy-links --no-gzip-encoding --no-check-certificate --allow-other --allow-non-empty --umask 000 >/dev/null 2>&1 & sleep 2s check_running if [[ $? -eq 0 ]]; then echo -e "${Info} $NAME_BIN 启动成功 !" else echo -e "${Error} $NAME_BIN 启动失败 !" fi fi } do_stop(){ check_running if [[ $? -eq 0 ]]; then kill -9 ${PID} RETVAL=$? if [[ $RETVAL -eq 0 ]]; then echo -e "${Info} $NAME_BIN 停止成功 !" else echo -e "${Error} $NAME_BIN 停止失败 !" fi else echo -e "${Info} $NAME_BIN 未运行" RETVAL=1 fi fusermount -zuq $LOCAL >/dev/null 2>&1 } do_status(){ check_running if [[ $? -eq 0 ]]; then echo -e "${Info} $NAME_BIN (PID $(echo ${PID})) 正在运行..." else echo -e "${Info} $NAME_BIN 未运行 !" RETVAL=1 fi } do_restart(){ do_stop do_start } case "$1" in start|stop|restart|status) do_$1 ;; *) echo "使用方法: $0 { start | stop | restart | status }" RETVAL=1 ;; esac exit $RETVAL |
修改一下内容:
1 2 3 |
NAME="" #rclone name名 REMOTE='' #远程文件夹,OneDrive网盘里的挂载的一个文件夹 LOCAL='' #挂载地址,服务器本地目录,使用绝对路径 |
设置自启
1 2 3 |
mv rcloned /etc/init.d/rcloned chmod +x /etc/init.d/rcloned chkconfig --add rcloned |
文章参考:https://www.moerats.com/archives/491/
文章评论