LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-12-2020, 04:26 PM   #1
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Rep: Reputation: 12
BASH Select - when in browser SELECT cmd sends output to Apche2 error.log


I can run pizdataRest.php which calls restoreBakup.sh which contains the subject "select" command from the command line in a terminal with no problems; it works!. Running the same from the "firefox" browser does not work!; the screen is blank except for the last line; "echo 'Continuing in 10 seconds.... "

After finding the below article on the internet I checked the /val/log/apache2/error.log and found that all the selections are there just as you want to find them on the screen.
Quote:
.
.
60) /home/rick/DBases2/MySql-bakups/2020Sep18-13:31:43_pizzidata.sql
61) /home/rick/DBases2/MySql-bakups/2020Sep18-13:31:43_polly.sql.gz
62) /home/rick/DBases2/MySql-bakups/2020Sep18-13:31:43_rick.sql.gz
63) /home/rick/DBases2/MySql-bakups/2020Sep18-13:31:43_sagle.sql.gz
64) /home/rick/DBases2/MySql-bakups/2020Sep18-13:31:44_states.sql.gz
.
.
Can this problem be fixed somehow??

Thanks in advance for your help.

R

Quote:
FROM https://linuxconfig.org/

Linux Tutorials - Learn Linux Configuration

How to create a selection menu using the select statement in Bash shell

........ The words obtained from the expansion of the variable are printed on the stderr (standard error). Each one is preceded and associated with a number, which is what the user will use to reference it. After all the elements are displayed, the user is prompted to enter its choice. What is displayed is the PS3 prompt ....

Code:
<?php

//Wed Nov 11, 2020 15:42
// File - pizdataRest.php
header( "refresh:10; url=PizziFrontEnd.php" );

exec("./restoreBakup.sh user passwd", $output, $return_var);

print_r($output);
echo $return_var;

echo 'Continuing in 10 seconds. Return now - click<a href="PizziFrontEnd.php"> here</a>.'; 

?>
Code:
#!/bin/bash

  # File - restoreBakup.sh
  #Sun 24 Sep 2017 18:23:28 
  #Tue 13 Oct 2020 09:18:45
  #Sun 08 Nov 2020 14:15
  
  # This is the companion script to dbaseBakup.sh
  # restoreBackup.sh  will restore the files dumped by that program

#folder="/home/rick/DBases/MySql_bakups" 
folder="/home/rick/DBases2/MySql-bakups"

user=$1      # command line parameters
passwd=$2

   echo "-- Start"
   echo 
   
   PS3='Pick a Database to Restore ...  '
  
    select bakup in $(ls -d "$folder"/* ) 
    do
	   break;
    done
  
ext=$(echo $bakup | awk -F "." '{print $2, $3}') # $2 gets sql - $3 gets gz extension
.
.
.
.
.
--- DONE ---

Last edited by pizzipie; 11-12-2020 at 04:31 PM.
 
Old 11-12-2020, 05:21 PM   #2
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Are you expecting to see the PS3 prompt and make a file selection from that bash script within the browser? It won't work like that, as you have found out! Everything sent to stderr will end up in the error file when running under apache, but even if you sent it to the screen you would have no means of making a selection from it as you do in a terminal - the script is not interactive through the browser!

I would begin by asking why you are using a shell script at all? Instead of using php as a wrapper to try to run the shell script, is there any reason to not do it much more simply by writing it all in php?

I would guess the answer may be that the ...DBases2/ path is not under your web server document root path (i.e. not accessible to apache or other permission problem), or the backups themselves cannot be easily executed via php, so you may be attempting this as a work around - either way a classic X-Y problem.

Rather than spend time trying to get the script to work through a browser, let's define the actual problem being solved and work on that instead.

* What are the backup files, possibly created by mysqldump?
* Why are you actually using the shell script? (You may have reasons not obvious to me!)
* Related, why did you not simply write this in php? (Did you try? If so what were the results and why did you turn to this method?)
* Are the backup files accessible to the web server? (Could you show us the apache document root path please)

Last edited by astrogeek; 11-12-2020 at 05:33 PM.
 
1 members found this post helpful.
Old 11-12-2020, 06:00 PM   #3
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,781

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
Continuation of a previous thread...
https://www.linuxquestions.org/quest...27-4175683614/

Using PHP would be the preferred method.

However, the web server runs as an unprivileged user i.e apache, www-data etc and therefore does not have permissions to read your home directory.

Last edited by michaelk; 11-12-2020 at 06:18 PM. Reason: caveat
 
Old 11-12-2020, 08:38 PM   #4
pizzipie
Member
 
Registered: Jun 2005
Location: Hayden, ID
Distribution: Ubuntu 20.04
Posts: 441

Original Poster
Rep: Reputation: 12
Quote:
I would begin by asking why you are using a shell script at all? Instead of using php as a wrapper to try to run the shell script, is there any reason to not do it much more simply by writing it all in php?
I discovered "select" in bash and it is really a perfect way to select files (in this case) to manipulate, at least outside the browser.

Quote:
I would guess the answer may be that the ...DBases2/ path is not under your web server document root path (i.e. not accessible to apache or other permission problem),
Actually DBase2.com is a virtual host and all files for the program, including the "backup files" are easily accessed from here.

Quote:
* What are the backup files, possibly created by mysqldump?
MySql database files gzipped from mysqldump

Quote:
* Why are you actually using the shell script? (You may have reasons not obvious to me!)
I thought it a slick and easy way to select the files to manipulate.

Quote:
* Related, why did you not simply write this in php? (Did you try? If so what were the results and why did you turn to this method?)
I'm lazy!. I thought it was going to be easier to use the shell script.

Quote:
* Are the backup files accessible to the web server? (Could you show us the apache document root path please)
DocumentRoot /var/www/html/DBases2.com
lrwxrwxrwx 1 root root 18 Nov 6 15:33 DBases2.com -> /home/rick/DBases2/

At this point I guess I should write this in PHP and access it thru the browser with AJAX. Wouldn't that be the best solution.

If so what would your suggestion be as a way to substitute the BASH "select" command?
I'm thinking something with php dir functions.

Last edited by pizzipie; 11-12-2020 at 08:52 PM.
 
Old 11-12-2020, 08:52 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,781

Rep: Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936Reputation: 5936
One way would be to use the glob function which creates an array of files of the directory and create a dynamic form using whatever html form you prefer i.e. select, check boxes, data list etc.

Last edited by michaelk; 11-12-2020 at 08:54 PM.
 
2 members found this post helpful.
Old 11-12-2020, 09:10 PM   #6
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,269
Blog Entries: 24

Rep: Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206Reputation: 4206
Quote:
Originally Posted by pizzipie View Post
I discovered "select" in bash and it is really a perfect way to select files (in this case) to manipulate, at least outside the browser.
Yes, shell scripts cannot be used interactively through a browser - now you know!

Quote:
Originally Posted by pizzipie View Post
Actually DBase2.com is a virtual host and all files for the program, including the "backup files" are easily accessed from here.
Good, that should eliminate path and permission problems, assuming nothing outside apache alters file ownership or permission.

Quote:
Originally Posted by pizzipie View Post
I'm lazy!. I thought it was going to be easier to use the shell script.
Only in a shell, at least if you need it to be interactive.

Quote:
Originally Posted by pizzipie View Post
At this point I guess I should write this in PHP and access it thru the browser with AJAX. Wouldn't that be the best solution.

If so what would your suggestion be as a way to substitute the BASH "select" command?
I'm thinking something with php readfile().
AJAX would be one possibility, but a simple select list in a web page would work as well without all the javascript.

If I understand correctly you need two things:

* Some way to list and select the backup file name
* A method of passing the selected filename or its contents to MySQL for execution

The first could be as simple as using PHP's glob() function, or opendir()/readdir() to generate a list of files.

Once you have the list you can encode it into an HTML SELECT element, or simply make a list of hyperlinks that you can click on. For simplicity I'd suggest a simple list of links that submit back onto the same page, with code that detects the encoded filename in the $_GET[] array (if I remember my PHP correctly) and performs the necessary action to execute the queries it contains.

That "execute" action might be done by passing the selection to a shell script, although I would recommend keeping it all in PHP with no exec()s if possible.

AJAX would be basically the same thing but with would require a little javascript and a target for the AJAX request... your choice.

One other thing to consider is that with any kind of one-click restore from backup you create a potential garden path to data loss... if your live DB has had changes since the last backup and you "restore" it, all those changes are gone - poof! You might consider some mechanism to prevent that being too easy to do!

See how far you can get with that!

* michaelk types faster and says more with fewer words than I do!

Last edited by astrogeek; 11-12-2020 at 09:12 PM. Reason: michaelk note - thanks!
 
1 members found this post helpful.
Old 11-13-2020, 12:03 AM   #7
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,880
Blog Entries: 1

Rep: Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871Reputation: 1871
You shouldn't perform long operations (such as database backup) via httpd+php (or httpd+cgi). That's what shell-access is good for. Also there is 'crontab' for repeated operations.
 
2 members found this post helpful.
Old 11-13-2020, 12:20 AM   #8
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,744

Rep: Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222Reputation: 2222
Quote:
Originally Posted by NevemTeve View Post
You shouldn't perform long operations (such as database backup) via httpd+php (or httpd+cgi). That's what shell-access is good for. Also there is 'crontab' for repeated operations.
Yes! If you have a shell script that will restore a database from the command line that works, just use that…length is not a factor...(one can create a php/cgi script that’s not dependent upon the connection), but absent outstanding coding and security (phpMySQL comes to mind), one should not do this kind of work in a browser.

Last edited by scasey; 11-13-2020 at 12:24 AM.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
after installation of mysql and php my apche2 and svn in not working, shivanyamtabad Linux - Networking 1 12-23-2013 01:22 PM
Issue sg_modes cmd at cmd line, want to see the cmd in binary form NuUser Linux - Newbie 1 03-28-2012 08:08 AM
convert LAN IP address to Host Name when I give cmd tail -f /var/log/squid/access.log rs15 Linux - Networking 6 01-22-2012 01:45 AM
apche2 help with mod_rewrite sir-lancealot Linux - Server 1 04-30-2009 12:55 PM
CSH: "cmd >& file" or "cmd </dev/null >& file" stefanlasiewski Programming 1 09-08-2003 04:19 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:45 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration