0

I'm going to backup my database using a php script which executes a command using the system() function. Unfortunately, my shared hosting has disabled it. I got this warning when I ran it:

Warning: system() has been disabled for security reasons in /path-to-my-file on line 162

Here is my piece of code:

$filename = 'backup/mybackupfile.sql';
$command = "mysqldump -u myuser -pmypass mydatabase > ". $filename ."";
system($command);

It is working fine on my local computer with xampp, but it happens to be a problem on my shared hosting.

So, I need another way to run the mysqldump command instead of using system(). I try to avoid using cron or SELECT * OUTFILE things.

John Conde
  • 217,595
  • 99
  • 455
  • 496
Abaij
  • 853
  • 2
  • 18
  • 34
  • 1
    May I ask *why* you want to avoid using cron? That would be a proper solution. – Matt Aug 10 '12 at 14:44
  • 1
    `mysqldump` *is* a system command. If you can not fire system commands, what would you do? Rewrite that in PHP? – hakre Aug 10 '12 at 14:44
  • @hakra, decerée - is there some renaming meme going on that I'm not privy to? :) – Pekka Aug 10 '12 at 14:45
  • @Matt > my user doesn't have access to cron, that's why I'm trying to avoid it – Abaij Aug 10 '12 at 14:46
  • Then how are you running your backups on a schedule? – Matt Aug 10 '12 at 14:47
  • @hakra you mean, I should choose a more masculine user name? Pekkor? Pekkar? Hmm. I'll have to think about it. – Pekka Aug 10 '12 at 14:49
  • @pekka, so there is no way out to run mysqldump in my host. so, can you share another solution to backup my database through php script? – Abaij Aug 10 '12 at 14:51
  • @Asep I wrote up what I know below – Pekka Aug 10 '12 at 14:51
  • @Matt, I do backup schedule with cron but my user what to do backups also anytime he want. So I need to provide a tool for him to do it :) Funny, he? – Abaij Aug 10 '12 at 14:56
  • 1
    Shell script is probably the best way to go about it then. This way the user just has to run the file. – Matt Aug 10 '12 at 15:03
  • @Matt, well I'll think about it. Thanks – Abaij Aug 10 '12 at 15:05

1 Answers1

1

As Mrs. Hakra already said, you will not be able to run mysqldump at all if your host has disabled the execution of system commands.

You will have to ask them to enable system commands, or do a dump using a PHP script. Be very careful with the PHP dump scripts around, however: my experience has shown that many create broken or incomplete dumps.

Some Googling reveals small tools such as this one. But as said, be careful and do some tests to verify accuracy.

The best PHP-based export tool I know that is doing the job properly is that of PhpMyAdmin. Maybe that can help. In theory, it should even be possible to isolate its exporting library, but as far as I know it doesn't expose it in any easy way for use in other scripts.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
  • yes, `phpmyadmin`is surely the best tool I know, but my client is not allowed to use it due to procedural matter. I think I will think about using more frequent schedule in cron instead. Thanks for your sharing ide :) – Abaij Aug 10 '12 at 15:04