简体   繁体   English

从ftp或php运行Bash文件(.sh)

[英]Running Bash file (.sh) from ftp or php

let me admit, I never ran a bash file from php...and I am unable to do this although I tried googling for it and using shell_exec & system() 让我承认,我从未从php运行过bash文件...尽管我尝试对其进行谷歌搜索并使用shell_execsystem()但我无法执行此操作

I got a bash script which takes back of magento db, file name is backu.sh . 我得到了一个bash脚本,该脚本带回了magento db,文件名为backu.sh the file is located at magento root /var/www/html/backup.sh , how can I run this file? 该文件位于magento根目录/var/www/html/backup.sh ,如何运行该文件?

I tried so far 我到目前为止尝试过

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

but it does not show anything, nor I can see the generated file ...my bash script looks like 但它什么也没显示,也看不到生成的文件...我的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"
`

please help... 请帮忙...

thanks.. 谢谢..

Try: 尝试:

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

Usually, /path/to/sh is /bin/sh . 通常, /path/to/sh/bin/sh

Also, you can add this line at the end of your backup.sh script: 另外,您可以在backup.sh脚本的末尾添加以下行:

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

That way, if your script has successfully been executed, a file named backupstatus.txt will be created and its content filled with "backup.sh has been executed" . 这样,如果脚本已成功执行,将创建一个名为backupstatus.txt的文件,其内容填充为"backup.sh has been executed"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM