文件结构- PHP LFTP数据镜像输出


file structure - PHP LFTP data mirror output

我使用的是Linux本地计算机,需要定期备份/镜像一些非常大的文件结构。我只能访问SFTP

我追求的是一个简单的一键解决方案。我最初试图在BASH中编写这个小脚本,但我以前从未使用过它,也不熟悉语法,所以我求助于PHP。(我知道PHP不是为这种工作设计的,但是我的时间很紧,没有时间进入BASH)

<?php
//init
parse_str(implode('&', array_slice($argv, 1)), $_GET);
$error = array();
$lPrefix         = '/home/hozza/Sites/';
$archiveLocation = '/home/hozza/Backups/';
$lDir = isset($_GET['l']) ? $_GET['l'] : $error[] = 'Local Directory Required';
$rDir = isset($_GET['r']) ? $_GET['r'] : $error[] = 'Remote Directory Required';
$bookmark = isset($_GET['b']) ? $_GET['b'] : $error[] = 'lftp Bookmark Required';
//Check for args
if(count($error) == 0) {
    $archiveName = end(explode('/', $lDir)) . '_' . date('Y-m-d_H-i');
    //Validate local dir
    if(is_dir($lPrefix . $lDir)) {
        //preserve Sublime Text 2 config SFTP files
        $ST2_SFTP_conf = false;
        if(file_exists($lPrefix . $lDir . '/sftp-config.json')) {
            $ST2_SFTP_conf = file_get_contents($lPrefix . $lDir . '/sftp-config.json');
            unlink($lPrefix . $lDir . '/sftp-config.json');
        }
        //Start mirror
        $lftOutput = explode("'n", shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark));
        //Tar regardless of lftp error or success
        $tarOutput = shell_exec('cd ' . $lPrefix . ' && tar -czf ' . $archiveLocation . $archiveName . '.tar.gz ' . $lDir);
        //Output completion or errors
        shell_exec('notify-send -i gnome-network-properties -t 0 "Mirror & Archive Complete" "' . $archiveName . ''n'n' . implode(''n', $lftOutput) . $tarOutput . '"');
        //put back ST2 SFTP conf
        if($ST2_SFTP_conf != false) file_put_contents($lPrefix . $lDir . '/sftp-config.json', $ST2_SFTP_conf);
        exit;
    }
    else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . ''n' . $lDir . ' 'n Does not exist! D:"');
}
else shell_exec('notify-send -i error -t 0 "Mirror & Archive Error" "' . date('Y-m-d') . ' ' . date('H-i') . ''n' . implode(''n', $error) . '"');
?>

它可以通过像这样的快捷方式在许多站点运行…

terminator -T "Mirror & Archive" -e "php ~/Programs/mirror.php l=local-dir_path r=./ b=lftp-bookmark-name"

如果LFTP书签中没有密码(不应该有,因为它是纯文本存储的),终端会提示输入密码,在脚本运行后,会给出一个很好的通知,其中包含一些关于文件/文件夹/速度等的信息。

然而,当脚本在终端中运行时,只有"输入密码"位输出到终端,我希望在终端中显示所有输出(通常会显示当前正在处理的文件/文件夹等)

有人知道怎么做吗?

IIRC,您看到密码提示输出到终端的原因是它正在使用stderr。您可以尝试将stdout重定向到stderr的命令,这将显示您的"实时"进度。将此附加到shell_exec()命令的末尾:1>&2

ie:

shell_exec('lftp -e "mirror -e -p --parallel=10 --log=' . $archiveLocation . 'logs/' . $archiveName . '.txt ' . $rDir . '/ ' . $lPrefix . $lDir . '/; exit top" ' . $bookmark . ' 1>&2')

但是,这将阻止您使用shell_exec返回的任何用于日志记录的内容。我的建议是:

$log_stem = '/tmp/' . time() . '_';  // ie: /tmp/1357581737_
$lfOutfile = $log_stem . 'lftp.log';
$tarOutfile = $log_stem . 'tar.log';
shell_exec('lftp -blah | tee ' . $lfOutfile ' 1>&2' );
shell_exec('tar -blah | tee ' . $tarOutfile ' 1>&2' );
$lfOut = file_get_contents($lfOutfile);
$tarOut = file_get_contetns(tarOutfile);
// remove tmp files
unlink($lfOutfile);
unlink($tarOutfile);

在将输出重定向到stderr之前,它将捕获输出的副本到文件中,以便您可以实时观看。

然而,如果你想通过cron运行这个,我建议反对向stderr写入任何不是错误的东西,否则cron每次运行都会发送警告电子邮件。

我认为最后一个答案很接近:

这:

shell_exec('lftp -blah |& tee ' . $lfOutfile );
shell_exec('tar -blah |& tee ' . $tarOutfile );

或者如果仍然不起作用,试试这个:

shell_exec('lftp -blah 2>&1 | tee ' . $lfOutfile );
shell_exec('tar -blah 2>&1 | tee ' . $tarOutfile );