BASH Select - when in browser SELECT cmd sends output to Apche2 error.log
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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 ---
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)
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)
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.
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!
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.