简体   繁体   中英

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()

I got a bash script which takes back of magento db, file name is backu.sh . the file is located at magento root /var/www/html/backup.sh , how can I run this file?

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

`#!/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 .

Also, you can add this line at the end of your backup.sh script:

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" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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