Something wrong ? using backtics to back up data base

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Something wrong ? using backtics to back up data base

Post by pizzipie »

Hi,

I cannot get the following to work when calling through $.ajax().

Code: Select all

`mysqldump  -urick  -prick $database | gzip > $filename`
The file bakup.php when run like: php -f bakup.php works fine and produces the desired data DUMP.

If I run DatabaseBakup.html which calls DatabaseBakup.php via $.ajax() the DUMP is not successful. I use the exact same code in DatabaseBakup.php as I do in bakup.php, in fact the code is copied from bakup.php to DatabaseBakup.php .

This makes no sense to me, I hope someone can explain what I am doing wrogn

Thanks in advance for any help, Rick P.

DatabaseBakup.html:

Code: Select all


<script  type="text/javascript" >

	var database=unescape(getUrlParametersVals()['db']);
 	
	function bakitup(database) {

	var str='db='+encodeURIComponent(database);
	
	$.ajax({
  		url: 'DatabaseBakup.php',
  		data: str,
  		success: function(data) {
    					$('#showMessage').html(data);
  				}
	});
}	
	
</script>

DatabaseBakup.php

Code: Select all

?php

set_include_path( 'include' );
error_reporting (E_ALL ^ E_NOTICE);

$dir="/home/rick/DB-Web";
$database="contacts";

$format="m.d.y_G.i.s";  // month, day, year _ hr, min, sec

$filename=$dir."/$database". "/".strtoupper ($database)."_DUMP_". date($format).".sql.gz";

`mysqldump -urick -prick $database | gzip > $filename`;
   
echo "File is stored in: ". $filename;

?>

bakup.php

Code: Select all

<?php

set_include_path( 'include' );
error_reporting (E_ALL ^ E_NOTICE);

$dir="/home/rick/DB-Web";
$database="contacts";

$format="m.d.y_G.i.s";  // month, day, year _ hr, min, sec

$filename=$dir."/$database". "/".strtoupper ($database)."_DUMP_". date($format).".sql.gz";

`mysqldump -urick -prick $database | gzip > $filename`;
   
echo "File is stored in: ". $filename;

?>


User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Something wrong ? using backtics to back up data base

Post by Robert07 »

How exactly are you "running" the .html file? Are you viewing it in a browser? If so, does the .php file work when you view it in a browser?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Re: Something wrong ? using backtics to back up data base

Post by pickle »

Calling code with php -f on the command line runs the file as your user. Calling it via a web browser runs it as the web server's user. Permissions might be a problem.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

Thanks Robert07,

The .html file is run in Firefox.

The PHP code works fine when run by itself from the command line.


?>

Code: Select all

<script  type="text/javascript" >

        var database=unescape(getUrlParametersVals()['db']);
       
        function bakitup(database) {

        var str='db='+encodeURIComponent(database);

HERE IS THE CALL TO -- DatabaseBakup.php --

Code: Select all

        $.ajax({
                url: 'DatabaseBakup.php',
                data: str,
                success: function(data) {
                                        $('#showMessage').html(data);
                                }
        });
}      
       
</script>
HERE IS THE PHP

Code: Select all

?php

set_include_path( 'include' );
error_reporting (E_ALL ^ E_NOTICE);

$dir="/home/rick/DB-Web";
$database="contacts";

$format="m.d.y_G.i.s";  // month, day, year _ hr, min, sec

$filename=$dir."/$database". "/".strtoupper ($database)."_DUMP_". date($format).".sql.gz";

`mysqldump -urick -prick $database | gzip > $filename`;
   
echo "File is stored in: ". $filename;

I suspect Pickle may have the answer If I can get the permissions corrected.

Thanks,

R
Last edited by pizzipie on Sat Apr 21, 2012 12:07 pm, edited 1 time in total.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

Thanks Pickle,

How would I change permissions in the browser?? In order to look at the settings in PHP I've tried to run phpinfo() in firefox and all I get is repeating tabs. I've tried to fix this but the published fix(s) doesn't work. I'm stuck. Any help here would be great.

Thanks,
R
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Something wrong ? using backtics to back up data base

Post by Robert07 »

Hi R,
What happens when you open bakup.php in a browser window? If you don't see anything add the following to the top of that file to help display whatever error you have:

#Show errors
error_reporting(E_ALL);
ini_set('display_errors', true);
ini_set('html_errors', true);

Regards,
Robert
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

Thanks Robert,

What is happening right now is that Firefox spits out un-ending tabs like: tab tab tab tab tab etc. There is a supposed fix on the internet but it doesn't work for me. SO ... This is completely screwing up my attempt to fix my PHP problem. Before this Firefox problem showed up bakup.php did work from the terminal like: php -f bakup.php. Not sure if it worked by calling the file with Firefox. As you can see above pickle alluded to a file path problem with PHP or APACHE2. To me solving that is a big problem too. I'll give your "#show errors" a try and see what happens when the backup php code is called from within the .html program.

Thanks again,
R
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Something wrong ? using backtics to back up data base

Post by Robert07 »

Pickle and I are talking about the same thing, I'm just trying to help you get more info about the issue.
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

I tried the error_reporting and ini_set code and nothing happened.
The error log from Apache2 says:

mysqldump: Got errno 32 on write
sh: cannot create /home/rick/DB-Web/contacts/CONTACTS_DUMP_04.24.12_10.11.19.sql.gz: Permission denied
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Something wrong ? using backtics to back up data base

Post by Robert07 »

There you go, that's something useful! Is that path within your webroot? Apache won't be able to write to a file outside the webroot. You can use htaccess to secure it so others can't download your backups, but if you are using ajax for your backups then you'll need to write to files under your webroot (the path to where your domain's php files are).
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

The way I have this set up is to have the following link in /var/www.
lrwxrwxrwx 1 root root 17 2011-12-23 17:38 DB-Web -> /home/rick/DB-Web
/home/rick/DB-Web is where all my files originate. This has worked for all my scripts until now.

This is what the default virtual host has.

rick@rhino:/etc/apache2/sites-enabled$ cat 000-default
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www ===== I'm thinking of making a different virtual host out of DB-Web
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

:( Does Not Work. :(

I call DatabaseBakup.php in Firefox and it does not run. I call DatabaseBakup.php directly on the command line and it works fine.

Here is what I'm running now.

Code: Select all

$command ="mysqldump --host='localhost' --user='rick' --password='rick'  ".$database." | gzip >". $filename;
exec($command);
x_mutatis_mutandis_x
Forum Contributor
Posts: 160
Joined: Tue Apr 17, 2012 12:57 pm

Re: Something wrong ? using backtics to back up data base

Post by x_mutatis_mutandis_x »

pizzipie wrote:The way I have this set up is to have the following link in /var/www.
lrwxrwxrwx 1 root root 17 2011-12-23 17:38 DB-Web -> /home/rick/DB-Web
/home/rick/DB-Web is where all my files originate. This has worked for all my scripts until now.

This is what the default virtual host has.

rick@rhino:/etc/apache2/sites-enabled$ cat 000-default
<VirtualHost *:80>
ServerAdmin webmaster@localhost

DocumentRoot /var/www ===== I'm thinking of making a different virtual host out of DB-Web
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
</Directory>
What are your permissions on /home/rick/DB-Web directory? What user is apache running as? Make sure the /home/rick/DB-Web is writable by user that apache is running as.

Also, you do not need to make another virtual host for DB-Web. I think you can do something like this:

Code: Select all

<VirtualHost *:80>
	ServerAdmin webmaster@localhost

	DocumentRoot /var/www 
	
                <Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /var/www/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

                <Directory /var/www/DB-Web>
                               Options Indexes FollowSymLinks MultiViews
                               AllowOverride None
                               Order allow,deny
                               deny from all
                </Directory>

pizzipie
Forum Commoner
Posts: 87
Joined: Wed Feb 10, 2010 10:59 pm
Location: Hayden. ID

Re: Something wrong ? using backtics to back up data base

Post by pizzipie »

Thank you x_mutatis_mutandis_x,

/home/rick/DB-Web has 777 permissions.

I think I'm now running off the apache2 VirtualHost DB-Web as I used a2dissite on sites-available/default and deleted the link to /home/rick/DB-Web in /var/www.

Here is the sites-available DB-Web file.

Code: Select all

<VirtualHost *:80>
	ServerAdmin webmaster@localhost
	ServerName DB-Web

	DocumentRoot /home/rick/
	<Directory />
		Options FollowSymLinks
		AllowOverride None
	</Directory>
	<Directory /home/rick/>
		Options Indexes FollowSymLinks MultiViews
		AllowOverride None
		Order allow,deny
		allow from all
	</Directory>

	ScriptAlias /cgi-bin/ /usr/lib/cgi-bin/
	<Directory "/usr/lib/cgi-bin">
		AllowOverride None
		Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
		Order allow,deny
		Allow from all
	</Directory>

	ErrorLog ${APACHE_LOG_DIR}/error.log

	# Possible values include: debug, info, notice, warn, error, crit,
	# alert, emerg.
	LogLevel warn

	CustomLog ${APACHE_LOG_DIR}/access.log combined

    Alias /doc/ "/usr/share/doc/"
    <Directory "/usr/share/doc/">
        Options Indexes MultiViews FollowSymLinks
        AllowOverride None
        Order deny,allow
        Deny from all
        Allow from 127.0.0.0/255.0.0.0 ::1/128
    </Directory>

</VirtualHost>
Here is apache2 user, I think.

rick@rhino:~$ ps -aux | grep apache2
Warning: bad ps syntax, perhaps a bogus '-'? See http://procps.sf.net/faq.html
root 1108 0.0 0.3 176260 12140 ? Ss 11:59 0:00 /usr/sbin/apache2 -k start
rick 3263 0.7 0.8 568328 35364 pts/1 Sl+ 14:01 0:21 gedit /var/log/apache2/error.log
www-data 3430 0.0 0.1 176780 7380 ? S 14:14 0:00 /usr/sbin/apache2 -k start
www-data 3431 0.0 0.2 176796 8508 ? S 14:14 0:00 /usr/sbin/apache2 -k start
www-data 3432 0.0 0.2 176904 8808 ? S 14:14 0:00 /usr/sbin/apache2 -k start
www-data 3433 0.0 0.1 176780 7244 ? S 14:14 0:00 /usr/sbin/apache2 -k start
www-data 3434 0.0 0.1 176780 7268 ? S 14:14 0:00 /usr/sbin/apache2 -k start
www-data 3439 0.0 0.2 176796 8280 ? S 14:15 0:00 /usr/sbin/apache2 -k start
www-data 3440 0.0 0.2 176916 8508 ? S 14:15 0:00 /usr/sbin/apache2 -k start
www-data 3445 0.0 0.2 176952 8624 ? S 14:15 0:00 /usr/sbin/apache2 -k start
www-data 3446 0.0 0.2 176796 8484 ? S 14:15 0:00 /usr/sbin/apache2 -k start
www-data 3494 0.0 0.2 176952 8648 ? S 14:24 0:00 /usr/sbin/apache2 -k start
rick 3682 0.0 0.0 9140 1048 pts/0 S+ 14:46 0:00 grep --color=auto apache2

It appears I was the apache2 user the last time I accessed my program.

Here is the error it produced.

sh: cannot create /home/rick/DB-Web/contacts/CONTACTS_DUMP_04.26.12_14.42.12.sql.gz: Permission denied
mysqldump: Got errno 32 on write

Driving me nuts !!!!!!!!!!!!!!!!!!!!!!!!!1
User avatar
Robert07
Forum Contributor
Posts: 113
Joined: Tue Jun 17, 2008 1:41 pm

Re: Something wrong ? using backtics to back up data base

Post by Robert07 »

What is the path to the ajax file that is trying to run the mysqldump command?
Post Reply