欢迎来到 岁月小筑

将网盘“变”为电脑的硬盘:网盘的WebDAV挂载的妙用,支持开机启动

作者:Vicer 来自:hostloc

拥有的网盘越来越多,但是闲置的占绝大部分,如何将它们落到实处,应用一个脚本就能将网盘挂载成电脑的目录。

注:只有支持webdav功能的网盘才能使用,百度网盘等大多数国内网盘并不支持。

安装:
bash WebDAV.sh [WebDAV] [User] [Password] [MountPath]
复制下面完整代码,重命名为WebDAV.sh。
如要修改,请在能读懂的情况下修改。
安装完成后,可以开机自动挂载。

示例:
bash WebDAV.sh ‘https://xxx.com/remote.php/webdav’ ‘UserName’ ‘Password’ ‘/CloudDAV’
第一个字段: 填写完整的WebDAV地址;
第二个字段: 填写你的用户名;
第三个字段: 填写你的密码;
第四个字段: 填写本地完整路径(你要挂载在本地的什么地方,如果没有则新建此文件夹。)
最好用单引号,不要搞非主流字符。因为没做兼容。

用法:
webdav.mnt start 挂载目录
webdav.mnt stop 卸载目录
webdav.mnt restart 重新挂载
注:当正在上传文件时,卸载目录可能不会成功。

将以下代码保存为WebDAV.sh(注意,linux下区分大小写

#!/bin/sh
[ $# -ne '4' ] && echo -e "Usage:\n      bash $0 [WebDAV] [User] [Password] [MountPath]" && exit 1
WebDAV="$1"
User="$2"
Pwd="$3"
WebMount="$4"
apt-get install -y -qq davfs2 fuse-utils libneon27-gnutls
[ $? -ne '0' ] && echo "Install davfs2 fail! " && exit 1
mkdir -p "$WebMount"
rm -rf /etc/davfs2/davfs2.conf
cat >>/etc/davfs2/davfs2.conf<<EOF
dav_user        root
dav_group        root
kernel_fs        fuse
buf_size        4
use_locks       0
cache_dir        /tmp/WebDAV
cache_size        4
delay_upload        3

EOF
rm -rf /etc/davfs2/secrets
cat >>/etc/davfs2/secrets<<EOF
$WebDAV $User $Pwd

EOF
chmod 600 /etc/davfs2/secrets
chmod 644 /etc/davfs2/davfs2.conf
rm -rf /etc/davfs2/webdav.mnt
cat >>/etc/davfs2/webdav.mnt<<EOF
#!/bin/sh
### BEGIN INIT INFO
# Provides:          webdav.mnt
# Required-Start:    \$all
# Required-Stop:     \$network \$remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start daemon at boot time
# Description:       Enable davfs by daemon.
### END INIT INFO
DAEMON=/usr/sbin/mount.davfs
PID=/var/run/webdav.pid
WebDAV="$WebDAV"
DavMnt="$WebMount"

test -x \$DAEMON || exit 1
case "\$1" in
start)
[ -n "\$(pgrep mount.davfs)" ] && echo "davfs already running. " && exit 0 || {
exec start-stop-daemon --start --pidfile \$PID --exec \$DAEMON \$WebDAV \$DavMnt -- -f >> /dev/null 2>&1 &
sleep 3
[ -n "\$(pgrep mount.davfs)" ] && echo "davfs start success. " || echo "davfs start fail. "
}
;;
stop)
[ -z "\$(pgrep mount.davfs)" ] && echo "davfs not running. " && exit 0 || {
umount \$DavMnt>> /dev/null 2>&1 &
sleep 3
[ -z "\$(pgrep mount.davfs)" ] && echo "davfs stop success. " || echo "davfs stop fail. "
}
;;
restart)
[ -z "\$(pgrep mount.davfs)" ] || {
umount \$DavMnt>> /dev/null 2>&1 &
sleep 3
[ -z "\$(pgrep mount.davfs)" ] && echo "davfs stop success. " || echo "davfs stop fail. "
}
exec start-stop-daemon --start --pidfile \$PID --exec \$DAEMON \$WebDAV \$DavMnt -- -f >> /dev/null 2>&1 &
sleep 3
[ -n "\$(pgrep mount.davfs)" ] && echo "davfs start success. " || echo "davfs start fail. "
;;
*)
echo "Usage: webdav.mnt {start|stop|restart}"
exit 1
esac
exit 0

EOF
chmod 755 /etc/davfs2/webdav.mnt
ln -sf /etc/davfs2/webdav.mnt /etc/init.d/webdav.mnt
ln -sf /etc/davfs2/webdav.mnt /usr/local/bin/webdav.mnt
update-rc.d -f webdav.mnt remove >>/dev/null 2>&1
update-rc.d webdav.mnt defaults

 

点赞
  1. qwe说道:

    怎么上传文件呀

    1. littleplus littleplus说道:

      理论上文件夹里面有文件就会自动上传啦(我也不懂)

  2. qwe说道:

    哦 已解决

发表评论

电子邮件地址不会被公开。