前言
你是否遇到过使用scp/rsync/ftps等依赖SSH的软件下载或上传速度较慢的情况,而使用基于http协议或者其它协议的软件速度比较快。这里我以这样一个例子来分享我一般是怎么解决的: 你有很多文件需要从服务器上下载,但一个一个下载会很慢。演示环境: Ubuntu 20.04.3 LTS
CPU架构: x86_64
搭建http上传及下载服务
这里可以不开放上传的功能,看个人实际需求。使用到的项目 miniserve: https://github.com/svenstaro/miniserve
下载地址: https://github.com/svenstaro/miniserve/ ... linux-musl
为什么这里选musl而不是带gnu的二进制可执行文件
原因是ubuntu 20.04.3 的glibc 版本不符合该软件的最低要求,因此采用musl c函数库编译的二进制可执行文件代码: 全选
For when you really just want to serve some files over HTTP right now!
Usage: miniserve [OPTIONS] [PATH]
Arguments:
[PATH]
Which path to serve
[env: MINISERVE_PATH=]
Options:
-v, --verbose
Be verbose, includes emitting access logs
[env: MINISERVE_VERBOSE=]
--index <INDEX>
The name of a directory index file to serve, like "index.html"
Normally, when miniserve serves a directory, it creates a listing for that directory. However, if a directory
contains this file, miniserve will serve that file instead.
[env: MINISERVE_INDEX=]
--spa
Activate SPA (Single Page Application) mode
This will cause the file given by --index to be served for all non-existing file paths. In effect, this will serve
the index file whenever a 404 would otherwise occur in order to allow the SPA router to handle the request instead.
[env: MINISERVE_SPA=]
--pretty-urls
Activate Pretty URLs mode
This will cause the server to serve the equivalent `.html` file indicated by the path.
`/about` will try to find `about.html` and serve it.
[env: MINISERVE_PRETTY_URLS=]
-p, --port <PORT>
Port to use
[env: MINISERVE_PORT=]
[default: 8080]
-i, --interfaces <INTERFACES>
Interface to listen on
[env: MINISERVE_INTERFACE=]
-a, --auth <AUTH>
Set authentication
Currently supported formats:
username:password, username:sha256:hash, username:sha512:hash
(e.g. joe:123, joe:sha256:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3)
[env: MINISERVE_AUTH=]
--auth-file <AUTH_FILE>
Read authentication values from a file
Example file content:
joe:123
bob:sha256:a665a45920422f9d417e4867efdc4fb8a04a1f3fff1fa07e998e86f7f7a27ae3
bill:
[env: MINISERVE_AUTH_FILE=]
--route-prefix <ROUTE_PREFIX>
Use a specific route prefix
[env: MINISERVE_ROUTE_PREFIX=]
--random-route
Generate a random 6-hexdigit route
[env: MINISERVE_RANDOM_ROUTE=]
-P, --no-symlinks
Hide symlinks in listing and prevent them from being followed
[env: MINISERVE_NO_SYMLINKS=]
-H, --hidden
Show hidden files
[env: MINISERVE_HIDDEN=]
-S, --default-sorting-method <DEFAULT_SORTING_METHOD>
Default sorting method for file list
[env: MINISERVE_DEFAULT_SORTING_METHOD=]
[default: name]
Possible values:
- name: Sort by name
- size: Sort by size
- date: Sort by last modification date (natural sort: follows alphanumerical order)
-O, --default-sorting-order <DEFAULT_SORTING_ORDER>
Default sorting order for file list
[env: MINISERVE_DEFAULT_SORTING_ORDER=]
[default: desc]
Possible values:
- asc: Ascending order
- desc: Descending order
-c, --color-scheme <COLOR_SCHEME>
Default color scheme
[env: MINISERVE_COLOR_SCHEME=]
[default: squirrel]
[possible values: squirrel, archlinux, zenburn, monokai]
-d, --color-scheme-dark <COLOR_SCHEME_DARK>
Default color scheme
[env: MINISERVE_COLOR_SCHEME_DARK=]
[default: archlinux]
[possible values: squirrel, archlinux, zenburn, monokai]
-q, --qrcode
Enable QR code display
[env: MINISERVE_QRCODE=]
-u, --upload-files [<ALLOWED_UPLOAD_DIR>]
Enable file uploading (and optionally specify for which directory)
[env: MINISERVE_ALLOWED_UPLOAD_DIR=]
-U, --mkdir
Enable creating directories
[env: MINISERVE_MKDIR_ENABLED=]
-m, --media-type <MEDIA_TYPE>
Specify uploadable media types
[env: MINISERVE_MEDIA_TYPE=]
[possible values: image, audio, video]
-M, --raw-media-type <MEDIA_TYPE_RAW>
Directly specify the uploadable media type expression
[env: MINISERVE_RAW_MEDIA_TYPE=]
-o, --overwrite-files
Enable overriding existing files during file upload
[env: OVERWRITE_FILES=]
-r, --enable-tar
Enable uncompressed tar archive generation
[env: MINISERVE_ENABLE_TAR=]
-g, --enable-tar-gz
Enable gz-compressed tar archive generation
[env: MINISERVE_ENABLE_TAR_GZ=]
-z, --enable-zip
Enable zip archive generation
WARNING: Zipping large directories can result in out-of-memory exception because zip generation is done in memory
and cannot be sent on the fly
[env: MINISERVE_ENABLE_ZIP=]
-C, --compress-response
Compress response
WARNING: Enabling this option may slow down transfers due to CPU overhead, so it is disabled by default.
Only enable this option if you know that your users have slow connections or if you want to minimize your server's bandwidth usage.
[env: MINISERVE_COMPRESS_RESPONSE=]
-D, --dirs-first
List directories first
[env: MINISERVE_DIRS_FIRST=]
-t, --title <TITLE>
Shown instead of host in page title and heading
[env: MINISERVE_TITLE=]
--header <HEADER>
Set custom header for responses
[env: MINISERVE_HEADER=]
-l, --show-symlink-info
Visualize symlinks in directory listing
[env: MINISERVE_SHOW_SYMLINK_INFO=]
-F, --hide-version-footer
Hide version footer
[env: MINISERVE_HIDE_VERSION_FOOTER=]
--hide-theme-selector
Hide theme selector
[env: MINISERVE_HIDE_THEME_SELECTOR=]
-W, --show-wget-footer
If enabled, display a wget command to recursively download the current directory
[env: MINISERVE_SHOW_WGET_FOOTER=]
--print-completions <shell>
Generate completion file for a shell
[possible values: bash, elvish, fish, powershell, zsh]
--print-manpage
Generate man page
--tls-cert <TLS_CERT>
TLS certificate to use
[env: MINISERVE_TLS_CERT=]
--tls-key <TLS_KEY>
TLS private key to use
[env: MINISERVE_TLS_KEY=]
--readme
Enable README.md rendering in directories
[env: MINISERVE_README=]
-I, --disable-indexing
Disable indexing
This will prevent directory listings from being generated and return an error instead.
[env: MINISERVE_DISABLE_INDEXING=]
-h, --help
Print help (see a summary with '-h')
-V, --version
Print version
/home/bobmaster/Downloads
miniserve二进制可执行文件就在该路径下,我想要分享的路径为
/home/bobmaster/Downloads/mysharing
我一般会这么运行 miniserve,首先用 screen 创建一个名为miniserve的任务窗口(当然你也可以改为其它名称,这里是没有要求的)避免SSH中断后导致程序退出
代码: 全选
screen -S miniserve
代码: 全选
./miniserve -p 10086 -a bobmaster:bobmaster -q -u -D /home/bobmaster/Downloads/mysharing
-p {端口}
-a 用户名:密码
-q 开启二维码分享
-u 开启上传
-D 把文件夹排在最上面
最后一个参数是你要分享的文件夹路径
你会得到类似这样的响应,然后根据内网还是外网访问即可。(如果你不希望将程序直接暴露至外网,可以使用
-i 127.0.0.1
参数)![图片](https://api.quanquan.space/imageproxy/https://i.imgur.com/ZMVqniU.jpeg)
然后我们按 Ctrl+A+D键将窗口切换至后台
screen简要使用指南
善用Tab键补全!!!新建窗口: screen -S 窗口名称
列出有多少个窗口: screen -ls
切换至特定窗口: screen -r 窗口名称
终止当前窗口: exit
杀掉某个窗口: screen kill 窗口名称
Ctrl+A+D键 可以将当前激活的窗口转至后台(detach)模式,哪怕SSH关闭了程序照样不会中断,类似功能的还有 tmux 以及 nohup
压缩指南
我们经常会遇到一核有难多核围观的情况,Linux下常用的tar,zip等命令只能使用单个核心,这不利于我们提高压缩的速度,尤其是数据量很大的时候。好在7z压缩文件可以直接使用所有核心,那还等什么,就决定是你了。
7z 命令介绍: https://linux.die.net/man/1/7z
Debian/Ubuntu可以使用
apt
安装
代码: 全选
sudo apt install p7zip-full
代码: 全选
7z a 压缩包名称.7z 被压缩的文件夹
a
代表添加到压缩包,也就是创建压缩包的命令-t7z
表示创建7z格式,如果需要创建zip压缩包改为-tzip
即可,省略默认为7z格式倒数第二个参数是压缩包名称
最后一个参数是被压缩文件或文件夹的路径
PS: 这里我们不在乎7z导致的Linux用户组元数据丢失,相比zip等格式7z压缩率更高,传输更节省带宽
压缩好后,将压缩包 move 到我们前面 miniserve 共享的文件夹下即可。