当一次性迁移或拷贝数据量比较大的内容到别的服务器,如迁移gitlab的仓库时,我们可以直接使用 tar 的 管道模式,直接把文件tar到远程服务器


tar -C /SourceDir -czf - -- . | ssh user@host  "( cd /DestDir ;  cat >  Name.tar.gz )"

CODE


上面命令中

  • SourceDir 是要打包的目录, DestDir 是目标目录, Name 是目标文件名称
  • -C  是跳转到指定目录, 使用 -C的好处是避免绝对路径, 如果不使用-C那么打包的文件带有绝对路径,解包时如果不注意经常会搞错
  • czf 不做过多解释, 创建、压缩
  •  - 在这里代表标准输出; -- 代表tar 后面没只能位置参数或没有参数了,使用--更严谨些,可以处理那些以“-”为前缀的目录和文件 
  • ssh 右面引号括号内的内容是奢scp tunnel 到另一一台服务器上,远程执行命令


关于双横杠--下面来自SO的解释比较通俗易懂, https://unix.stackexchange.com/questions/11376/what-does-double-dash-mean

More precisely, a double dash (--) is used in most Bash built-in commands and many other commands to signify the end of command options, after which only positional arguments are accepted.

Example use: Let's say you want to grep a file for the string -v - normally -v will be considered the option to reverse the matching meaning (only show lines that do not match), but with -- you can grep for the string -v like this:

grep -- -v file

简单的来书就是 -- 后面不能再有name类型的参数了, 上面grep命令的 -v 就不代表 not 的意思了, 而是有一个文件名是 -v