Jump to content

Automatically generate backup of db


son

Recommended Posts

I would like to run a scheduled task via server (hosted) that generates a backup. I uploaded for this reason a file called backup.php to root directory and it has the following code:

/usr/bin/php5mysqldump -h localhost -p[PASSWORD] -u [USERNAME] [DATABASENAME] > testdump.sql

I cannot get it work and wondered where am I going wrong... Son

Link to comment
Share on other sites

I had it within php tags, but not within function. However, still not working as

<?php/usr/bin/php5function exec(){mysqldump -h localhost -p[pass] -u [user] [db] > testdump.sql}exec();?>

Where am I going wrong? Son

Link to comment
Share on other sites

Remove the /usr/bin/php5 line, or move it outside of the PHP block. I don't use that line in my cron scripts. I meant this exec: http://www.php.net/manual/en/function.exec.php Just writing any arbitrary thing in the PHP script doesn't cause PHP to send that as a command to the operating system, you need to use a function like exec, passthru, shell_exec, etc to have PHP send that command to the system to execute.

Link to comment
Share on other sites

Now I got you, but still not working. I have now:

/usr/bin/php5<?phpecho exec('mysqldump -h localhost -p[data] -u [data] [data] > testdump.sql');?>

Also tried shell_exec and path to PHP inside php tags, but same result.... Son

Link to comment
Share on other sites

Did you try to remove the first line? It shouldn't have an effect outside of the PHP tags, but it's worth a try. Otherwise, run the exact command you're trying to execute inside of a shell and see what happens, you may be getting errors. You can also use passthru instead of echo to send the output to the browser, but that will only verify that it works in a browser and not necessarily run as a cron job. To verify that this runs without errors you'll need to enable error logging for that script, capture the output of the command, and write the output to the error log.

Link to comment
Share on other sites

I have found one issue which was that the file did not have permission which I fixed. Now running the schedule it says:Your script returned the following:X-Powered-By: PHP/5.2.17Content-type: text/htmlwhich looked promising, but no backup generated. I tried to remove the path, but no difference and also tried passthru to see if it works when run in a browser. No luck with this either. You say I should run the script inside a shell... I have nver done this before and believe it is when you run something in MS DOS or similar. Is this correct and how would I do this? Also, in what general cases is this useful? Son

Link to comment
Share on other sites

You use a shell to run commands directly on the server. You'll need to figure out what type of server you're using and how to access it. If it is a Linux server then they may let you log in using SSH, but you'll need to contact the host to figure out how to run commands in a shell.

Link to comment
Share on other sites

They're basically saying that if you break the server it's your fault. You can pretty much do anything you want in a shell, so they're limiting their liability. If you break the server then you will need to pay them to do the work to bring it back online, they won't do it for free as part of their normal duties.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...