2013-04-17 50 views
1

運行bash文件(.SH)讓我承認,我從來沒有遇到來自PHP一個bash文件......,我無法做到這一點,雖然我試着用搜索引擎它使用shell_exec & system()從FTP或PHP

我收到了一個bgent腳本,它帶回了magento db,文件名是backu.sh。該文件位於magento根/var/www/html/backup.sh,我該如何運行此文件?

我試過到目前爲止

$output=shell_exec('sh backup.sh >/dev/null &'); echo "<pre>$output</pre>";

,但它並不顯示任何東西,也不是我能看到生成的文件...我的bash腳本看起來像

`#!/bin/sh 

# Connection parameters 
DBHOST= 
DBUSER= 
DBNAME= 
TBLPRF= 

# Include DB logs option 
SKIPLOGS=1 

# Magento folder 
MAGENTOROOT=./ 
LOCALXMLPATH=${MAGENTOROOT}app/etc/local.xml 

# Output path 
OUTPUTPATH=$MAGENTOROOT 

# Content of file archive 
DISTR=" 
app 
downloader 
errors 
includes 
js 
lib 
pkginfo 
shell 
skin 
.htaccess 
cron.php 
cron.sh 
get.php 
index.php 
install.php 
mage 
*.patch" 

# Ignored table names 
IGNOREDTABLES=" 
core_cache 
core_cache_option 
core_cache_tag 
core_session 
log_customer 
log_quote 
log_summary 
log_summary_type 
log_url 
log_url_info 
log_visitor 
log_visitor_info 
log_visitor_online 
enterprise_logging_event 
enterprise_logging_event_changes 
index_event 
index_process_event 
report_event 
report_viewed_product_index 
dataflow_batch_export 
dataflow_batch_import" 

# Get random file name - some secret link for downloading from magento instance :) 
MD5=`echo \`date\` $RANDOM | md5sum | cut -d ' ' -f 1` 
DATETIME=`date -u +"%Y%m%d%H%M"` 
CODEFILENAME="$OUTPUTPATH$MD5.$DATETIME.tar.gz" 
DBFILENAME="$OUTPUTPATH$MD5.$DATETIME.sql.gz" 

# Create code dump 
DISTRNAMES= 
for ARCHPART in $DISTR; do 
    if [ -r "$MAGENTOROOT$ARCHPART" ]; then 
     DISTRNAMES="$DISTRNAMES $MAGENTOROOT$ARCHPART" 
    fi 
done 
if [ -n "$DISTRNAMES" ]; then 
    echo nice -n 15 tar -czhf $CODEFILENAME $DISTRNAMES 
    nice -n 15 tar -czhf $CODEFILENAME $DISTRNAMES 
fi 

# Get mysql credentials from local.xml 
getLocalValue() { 
    PARAMVALUE=`sed -n "/<resources>/,/<\/resources>/p" $LOCALXMLPATH | sed -n -e "s/.*<$PARAMNAME><!\[CDATA\[\(.*\)\]\]><\/$PARAMNAME>.*/\1/p" | head -n 1` 
} 

if [ -z "$DBHOST" ]; then 
    PARAMNAME=host 
    getLocalValue 
    DBHOST=$PARAMVALUE 
fi 
if [ -z "$DBUSER" ]; then 
    PARAMNAME=username 
    getLocalValue 
    DBUSER=$PARAMVALUE 
fi 
if [ -z "$DBNAME" ]; then 
    PARAMNAME=dbname 
    getLocalValue 
    DBNAME=$PARAMVALUE 
fi 
if [ -z "$TBLPRF" ]; then 
    PARAMNAME=table_prefix 
    getLocalValue 
    TBLPRF=$PARAMVALUE 
fi 

if [ -z "$DBHOST" -o -z "$DBUSER" -o -z "$DBNAME" ]; then 
    echo "Skip DB dumping due lack of parameters host=$DBHOST; username=$DBUSER; dbname=$DBNAME;"; 
    exit 
fi 
CONNECTIONPARAMS=" -u$DBUSER -h$DBHOST -p $DBNAME --single-transaction --opt --skip-lock-tables" 

# Create DB dump 
IGN_SCH= 
IGN_IGN= 
if [ -n "$SKIPLOGS" ] ; then 
    for TABLENAME in $IGNOREDTABLES; do 
     IGN_SCH="$IGN_SCH $TBLPRF$TABLENAME" 
     IGN_IGN="$IGN_IGN --ignore-table='$DBNAME'.'$TBLPRF$TABLENAME'" 
    done 
fi 

if [ -z "$IGN_IGN" ]; then 
    CODEDUMPCMD="nice -n 15 mysqldump $CONNECTIONPARAMS" 
else 
    CODEDUMPCMD="(nice -n 15 mysqldump $CONNECTIONPARAMS $IGN_IGN ; nice -n 15 mysqldump --no-data $CONNECTIONPARAMS $IGN_SCH)" 
fi 

CODEDUMPCMD="$CODEDUMPCMD | nice -n 15 gzip > $DBFILENAME" 
echo $CODEDUMPCMD 
eval "$CODEDUMPCMD" 
` 

請幫助.. 。

感謝..

回答

1

嘗試:

$output=shell_exec('/path/to/sh /var/www/html/backup.sh'); 
echo "<pre>$output</pre>"; 

通常,/path/to/sh/bin/sh

此外,你可以在你backup.sh腳本的末尾添加這一行:

echo "backup.sh has been executed" >> /var/www/html/backupstatus.txt 

這樣一來,如果你的腳本已成功執行,將創建一個名爲backupstatus.txt文件,其內容充滿了"backup.sh has been executed"

+0

感謝您的幫助,我運行腳本,看起來像現在正在運行,但它不創建任何文件,不是轉儲文件,不是backupstatus.txt文件,任何想法? –

+0

@NewBee我不是bash專家,但是如果你沒有看到創建的'backupstatus.txt',這意味着這一點從來沒有達到過。試着把這一行放在'.sh'文件中。如果該文件被執行,並且如果php對'/ var/www/html /'具有寫權限,那麼該文件應該被創建。必須滿足這兩個條件。你有一個SSH訪問服務器?如果是的話,事情將非常簡單的調試。 – Jean