CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
System() call problem on Windows

 



razmot
New User

Jun 9, 2011, 6:21 AM

Post #1 of 5 (851 views)
System() call problem on Windows Can't Post

I try to start a postgres backup job from a perl script. The script should create the backupfilenames and zip them.

The command for that is pg_dump.exe.

Looks like the parameters of the command are not correctly interpreted. When run from the windows commandline, the generated command-line works fine. When run from the perl script it asks for a password!!


Code
my $tm = localtime(); 
my $backupfile = sprintf("%s/PostgresDBBackup_%s_%s_%04s%02s%02s%02s%02s%02s.bak", $BackupPath, $hostname,
$DBName, $tm->year+1900, $tm->mon+1, $tm->mday, $tm->hour, $tm->min, $tm->sec);
my $backupargs = sprintf("-f %s -F p --clean -U postgres -h localhost %s", $backupfile, $DBName);
my @args = ($pgbackup, $backupargs);
print "COMMAND: ".$pgbackup." ".$backupargs."\n";
my $status = system(@args);


And this is the output:

D:\Projects\Eclipse\workspace\DBBackups>perl PostgresBackup.pl //nas-tb/DBBackups misdwh E:\PostgreSQL\8.4SS\bin
COMMAND: E:\PostgreSQL\8.4SS\bin/pg_dump.exe -f //nas-tb/DBBackups/PostgresDBBackup_TB-WS_misdwh_20110609151147.bak -F p --clean -U postgres -h localhost misdwh
Passwort:
pg_dump: [Archivierer (DB)] Verbindung zur Datenbank »TB« fehlgeschlagen: fe_sendauth: no password supplied
256

What I do not understand is the error message saying that the connection to the database "TB" failed. There is of course no database "TB". The name of the database is the last parameter passed and in my case 'misdwh'. By the way I tried the paths with backslashes and forwardslashes. Alsways the same error! Any idea what the problem could be??

Just for information: If the system is setup correctly, you NEVER have to pass a password on the command line to connect to the Server. The -U parameter specifies the user, the password is looked up in a file. As said if I copy the command I printed (the string after COMMAND) this works as a charm.

Any idea what I am doing wrong in the perl script?

Many thanks


rovf
Veteran

Jun 10, 2011, 6:25 AM

Post #2 of 5 (832 views)
Re: [razmot] System() call problem on Windows [In reply to] Can't Post

I don't know about Postgres, but from your example, I find one thing worth investigating: Your variable $hostname appearantly contains 'TB'. Is it a coincidence, or is it really the name of the host? It is interesting that Postgres complains about a "database" TB, and the only place the string TB appears in your code, is in $hostname....


razmot
New User

Jun 10, 2011, 7:55 AM

Post #3 of 5 (829 views)
Re: [rovf] System() call problem on Windows [In reply to] Can't Post

Well I got the solution in the meantime. The correct Perl code to compose the parameters for pg_dump is:


Code
OK: my $backupargs = sprintf("-f \"%s\" -F p --clean -U postgres -h localhost %s", $backupfile, $DBName);  

NOK: my $backupargs = sprintf("-f %s -F p --clean -U postgres -h localhost %s", $backupfile, $DBName);


This means, that the fully qualified name of the backup file is within double quotes.

NOT working:
E:\PostgreSQL\8.4SS\bin/pg_dump.exe -f
//nas-tb/DBBackups/PostgresDBBackup_TB-WS_misdwh_20110609151147.bak -F p
--clean -U postgres -h localhost misdwh

Working when passed to system():
E:\PostgreSQL\8.4SS\bin/pg_dump.exe -f
"//nas-tb/DBBackups/PostgresDBBackup_TB-WS_misdwh_20110609151147.bak" -F p
--clean -U postgres -h localhost misdwh

This is only true when calling system() from within perl, double quotes on the command line will NOT work! Looks like there is a problem with the underscore in the filename.

Thomas


rovf
Veteran

Jun 10, 2011, 8:52 AM

Post #4 of 5 (826 views)
Re: [razmot] System() call problem on Windows [In reply to] Can't Post

Are you running on Cygwin? I wonder, because you are using a mixture of forward- and backward slashes.

Note also that passing quotes to system probably doesn't do what you expect, if you call system with more than one argument. In this case, the effect is to bypass the shell, and if your arguments are surrounded by quotes, the application will see the quotes, and it is quite likely that it doesn't know what to do with them.

If you decide to "bundle" your arguments by quoting, I suggest that you pass to system only one string, containing the whole invocation (command and arguments). If you decide to pass to system a list, each list element should have just one argument to the called program, i.e. something like


Code
system('pg_dump.exe','-f','//my/file/xxx.bak','-F','p',...)


Everything else depends on how much "intelligence" has been built in your postgres dump program....


razmot
New User

Jun 10, 2011, 1:27 PM

Post #5 of 5 (814 views)
Re: [rovf] System() call problem on Windows [In reply to] Can't Post

No, I am not on Cygwin, I use ActivePerl. The backslashes seem not to be a problem, it works with them, mixed with forwardslashes or forwardslashes only.

Thanks for the hint with the list. I will give that a try.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives