0

I am trying to backup database through .sh file. Its working fine if I open the file manually. But I am trying to do it with PHP(laravel).

Here is my mysql_backup.sh file:

#!/bin/bash

#
#   Use this script to perform backups of one or more MySQL databases.
#

# Databases that you wish to be backed up by this script. You can have any number of databases specified; encapsilate each database name in single quotes and separate each database name by a space.
#
# Example:
# databases=( '__DATABASE_1__' '__DATABASE_2__' )
databases=('sasty')

# The host name of the MySQL database server; usually 'localhost'
db_host="127.0.0.1"

# The port number of the MySQL database server; usually '3306'
db_port="3306"

# The MySQL user to use when performing the database backup.
db_user="root"

# The password for the above MySQL user.
db_pass=""

# Directory to which backup files will be written. Should end with slash ("/").
backups_dir="C:/Users/Kapil/backup/"

backups_user=""

# Date/time included in the file names of the database backup files.
datetime=$(date +'%Y-%m-%dT%H:%M:%S')

for db_name in ${databases[@]}; do
        # Create database backup and compress using gzip.
        C:/xampp/mysql/bin/mysqldump -u $db_user -h $db_host -P $db_port --password=$db_pass $db_name | gzip -9 > $backups_dir$db_name--$datetime.sql.gz
done

# Set appropriate file permissions/owner.
chown $backups_user:$backups_user $backups_dir*--$datetime.sql.gz
chmod 0400 $backups_dir*--$datetime.sql.gz

I am using PHP code for this

shell_exec('/path/to/my/file');

But its not working. however shell_exec('ls') is working fine. How to solve my problem?

Lawrence Cherone
  • 41,907
  • 7
  • 51
  • 92
Kapil Paul
  • 416
  • 5
  • 20
  • What's the owner and permission of your `'/path/to/my/file'`? – Ataur Rahman Feb 06 '18 at 04:57
  • It looks like you are attempting to execute the path directly. Is the file (I assume a script), located at `/path/to/my/file` marked as executable? Is it timing out? What error messages do you see in your logs? Does the user running the PHP script (e.g., `www-data`) have the permissions to execute that script? – hunteke Feb 06 '18 at 04:57
  • `ls` on windows, that's new to me. – Lawrence Cherone Feb 06 '18 at 04:59
  • @AtaurRahman file permission is read & execute, read, write – Kapil Paul Feb 06 '18 at 05:01
  • @hunteke the file name is mysql_backup.sh . i did not get any error message. instead of using file I also tried with the codes . but no luck – Kapil Paul Feb 06 '18 at 05:05
  • @LawrenceCherone i am using vagrant on my machaine. but i want to use it xampp in windows – Kapil Paul Feb 06 '18 at 05:06
  • @KapilPaul And permissions of `mysql_backup.sh`? Is it executable? Does the Apache/PHP user have the correct permissions to execute it? Try showing [all errors](https://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display). – hunteke Feb 06 '18 at 05:17
  • @hunteke I execute this normally. I just open the file via git bash and its working. I don't know if the apache/php user have the correct permission for it. How to check the permissions? – Kapil Paul Feb 06 '18 at 05:20
  • You're running Apache/PHP on Windows, but are running the bash script through Git Bash. As @LawrenceCherone noted, `ls` on Windows (*not* through Git Bash) is confusing. My first thought is that you need to execute `sh` and _that_ needs to run the backup script. Something like: `/path/to/sh /path/to/mysql_backup.sh` – hunteke Feb 06 '18 at 05:26

1 Answers1

-1

php.ini

disable_functions =exec,passthru,shell_exec,system,proc_open,popen,curl_exec,curl_multi_exec,parse_ini_file,show_source

remove shell_exec

hoyeonUm
  • 39
  • 7