通过SSH隧道使用gzip TAR的速度非常快。幅度比纯粹的scp快,特别是关于许多小文件。下面是Linux命令行一个示例:
用户@本地#坎德拉/源极/; tar czf - * | SSH用户@远程“CD /目标/焦油xzf - ”
更新:按照要求,在这里你去用纯PHP解决方案 - 有一些有趣的摆弄出这个有点棘手。
注意:您需要PHPs libssh extension才能正常工作。此外,STDIN似乎只在使用SSH流封装时才可用。
这几乎没有开销,因为它是在流直接操作,你的CPU是最有可能永远比你所使用的传输网络链接速度更快。
要交易网络与CPU速度,您可以从命令行中删除选项-z。 (更少的CPU使用率,但在电线上更多的数据)
代码示例:
$local_cmd = "cd /tmp/source && tar -czf - *";
$remote_cmd = "tar -C /tmp/target -xzf -";
$ssh = new SSH_Connection('localhost');
$auth = $ssh->auth_password('gast', 'gast');
$bytes = $ssh->command_pipe($local_cmd, $remote_cmd);
echo "finished: $bytes bytes of data transfered\n";
class SSH_Connection {
private $link;
private $auth;
function __construct ($host, $port=22) {
$this->link = @ssh2_connect('localhost', 22);
}
function auth_password ($username, $password) {
if (!is_resource($this->link))
return false;
$this->auth = @ssh2_auth_password($this->link, $username, $password);
return $this->auth;
}
function command_pipe ($localcmd, $remotecmd) {
if (!is_resource($this->link) || !$this->auth)
return false;
// open remote command stream (STDIN)
$remote_stream = fopen("ssh2.exec://{$this->link}/$remotecmd", 'rw');
if (!is_resource($remote_stream))
return false;
// open local command stream (STDOUT)
$local_stream = popen($localcmd, 'r');
if (!is_resource($local_stream))
return false;
// connect both, pipe data from local to remote
$bytes = 0;
while (!feof($local_stream))
$bytes += fwrite($remote_stream,fread($local_stream,8192));
// close streams
fclose($local_stream);
fclose($remote_stream);
return $bytes;
}
function is_connected() { return is_resource($this->link); }
function is_authenticated() { return $this->auth; }
}