crazycoder Posted October 6, 2015 Share Posted October 6, 2015 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" Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/ Share on other sites More sharing options...
seandisanti Posted October 6, 2015 Share Posted October 6, 2015 Is it generating an error? is anything going into the event log? There's also the issue of your forward slashes in the path you've listed Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522444 Share on other sites More sharing options...
crazycoder Posted October 6, 2015 Author Share Posted October 6, 2015 There is no error. If I use a retun variable the it is having a value of '1'; system("cmd /c D:\wamp\www\pentium\protected\data\backup\cron.bat",$return_var); echo $return_var; I have tried escaping the slashes, but was of no use. Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522446 Share on other sites More sharing options...
seandisanti Posted October 6, 2015 Share Posted October 6, 2015 pg_dump -i -U "postgres" -F c -b -v -f "D:\wamp\www\pentium\protected/data/backup/pentium.backup" "pentium" Your operating system may not like how you use backslashes through the word protected, and then switch it up to forward slashes. Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522447 Share on other sites More sharing options...
crazycoder Posted October 6, 2015 Author Share Posted October 6, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522448 Share on other sites More sharing options...
seandisanti Posted October 6, 2015 Share Posted October 6, 2015 Windows path is typically backslashes Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522451 Share on other sites More sharing options...
crazycoder Posted October 6, 2015 Author Share Posted October 6, 2015 Is there any other way I can run the pg_dump command using php. I have tried with exec() but that also was not working. Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522455 Share on other sites More sharing options...
seandisanti Posted October 6, 2015 Share Posted October 6, 2015 It's typically inadvisable to have your php executing external programs when it's avoidable. For regular maintenance tasks it's usually a better alternative to use a cron job, or a scheduled task in windows. Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522493 Share on other sites More sharing options...
crazycoder Posted October 7, 2015 Author Share Posted October 7, 2015 If I execute the PHP file through command line then it successfully executes and the back up file is generated. Problem is when I access through browser. Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522540 Share on other sites More sharing options...
seandisanti Posted October 10, 2015 Share Posted October 10, 2015 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, Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1522862 Share on other sites More sharing options...
crazycoder Posted October 15, 2015 Author Share Posted October 15, 2015 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"); Quote Link to comment https://forums.phpfreaks.com/topic/298452-take-the-backup-of-postgresql-database-using-php/#findComment-1523317 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.