【发布时间】:2015-02-25 18:12:24
【问题描述】:
我想使用tar 命令和Php 中的exec 备份我的网站,我编写了一个小脚本来执行此操作,但没有任何反应……我错在哪里?我有 php 5.6.5 和托管 linux,它启用了 exec 和 tar 命令可用。这是我想做的Php 示例。
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
# root is /web/htdocs/www.example.com/home/
$name = "backup_" . date("[d-m-Y][H-i]") . ".tar.gz";
# name is backup_[25-02-2015][18-57].tar.gz
$skip = "*.gz";
# skip is the file I want to exclude (example: skip backup_[25-02-2015][18-57].tar.gz)
if ((substr($_SERVER['DOCUMENT_ROOT'],-1,1) == "/") && (substr($_SERVER['PHP_SELF'],0,1) =="/")) {
$sdir = $_SERVER['DOCUMENT_ROOT'] . substr(dirname($_SERVER['PHP_SELF']),1);
} else {
$sdir = $_SERVER['DOCUMENT_ROOT'] . dirname($_SERVER['PHP_SELF']);
}
# sdir is /web/htdocs/www.example.com/home/bak/ and is the path where the script lives
# out is the output
# oky is the success o failed exec command
function backup() {
exec("tar -cvf $sdir/$name $root/* --exclude='$sdir/$skip' ", $out, $oky);
}
backup();
if (!$oky) {
echo "$out: Backup Completed!";
} else {
echo "$out: Backup Not Completed!";
}
?>
感谢任何帮助!
【问题讨论】:
-
函数
backup()中字符串中的变量在函数内部都不可用,另外两个在函数外部不可用:php.net/manual/en/language.variables.scope.php