Jump to content

Take the backup of PostgreSQL database using php


crazycoder

Recommended Posts

I am trying to take the backup of PostgreSQL database using php. I have written the below code to run a batch file which has the command to take the backup. I am using Wampserver for development and the below coed works perfectly. But the problem is that it is not working on the server which is Windows Server 2008 R2. Please help.

 

putenv("PGPASSWORD=postgres");
system("cmd /c D:\wamp\www\pentium\protected\data\backup\cron.bat");

 

 

Below is the content of cron.bat

 

pg_dump -i -U "postgres" -F c -b -v -f "D:\wamp\www\pentium\protected/data/backup/pentium.backup" "pentium"

Link to comment
Share on other sites

 

pg_dump -i -U "postgres" -F c -b -v -f "D:\wamp\www\pentium\protected/data/backup/pentium.backup" "pentium"

 

Seems there is no error here because if I double click to run the cron.bat file then the backup file is generated. Now I have tried changing the path to forward slashes in system() function,but it is not working.

Link to comment
Share on other sites

Then it sounds like a permission issue, like your apache user may not have write access where you're trying to put the backup. Again I'd like to point out that regular maintenance tasks like backups are typically delegated to cron jobs or scheduled tasks, which would not typically require browser interaction,

Link to comment
Share on other sites

Finally I wrote the command in php and executed the php file using windows task scheduler.

$path = Yii::app()->basePath . '/data/backup/';
$file=$path."pentium.backup";
unlink($file);

putenv("PGPASSWORD=postgres");
$dumpcmd = array("pg_dump", "-i", "-U", escapeshellarg("postgres"), "-F", "c", "-b", "-v", "-f", escapeshellarg($file), escapeshellarg("pentium"));
exec( join(' ', $dumpcmd), $cmdout, $cmdresult );
putenv("PGPASSWORD");

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.