0

I am using this batch file to backup my database:

    @echo off
   for /f "tokens=1-4 delims=/ " %%i in ("%date%") do (
     set dow=%%i
     set month=%%j
     set day=%%k
     set year=%%l
   )
   for /f "tokens=1-2 delims=/:" %%a in ('time /t') do (
     set time=%%a%%b
   )
   set datestr=%month%_%day%_%year%_%time%
   echo datestr is %datestr%

   set BACKUP_FILE=C:\database_files\db_backup\DailyBackup_%datestr%.backup
   echo backup file name is %BACKUP_FILE%
   SET PGPASSFILE=C:\Users\Administrator\AppData\Roaming\postgresql\pgpass.conf
   echo on
   "C:\Program Files\PostgreSQL\9.0\bin\pg_dump" -i -h localhost -p 5432 -U postgres -F c -b -v -f %BACKUP_FILE% databasename

When I run it I get the error mentioned in the title. If i remove the part where I set the name of the file to include the time, it works.

Azar_Javed
  • 75
  • 1
  • 2
  • 9
  • 1
    Are there spaces in the filename? Try to quote your args. –  Jun 25 '14 at 18:13
  • @Tibo What do you mean quote args? – Azar_Javed Jun 25 '14 at 18:18
  • 1
    Use quotes around your arguments, like so: ``pg_dump -p foo "%BACKUP_FILE%"`` otherwise the shell will expand your filename (with its spaces) and ``pg_dump`` will see mutiple arguments. –  Jun 25 '14 at 19:44
  • @Tibo now it tells me: `"C:\>"C:\Program Files\PostgreSQL\9.0\bin\pg_dump" -i -h localhost -p 5432 -U postgres -F c -b -v -f "C:\database_files\db_backup\DailyBackup_06_27_2 014_0746 AM.backup" databasename pg_dump: [custom archiver] could not open output file "C:\database_files\db_back up\DailyBackup_06_27_2014_0746 AM.backup": No such file or directory` – Azar_Javed Jun 27 '14 at 14:48
  • @Tibo, Disregard my last post, that actually fixed it. Thanks – Azar_Javed Jun 27 '14 at 14:53
  • Not sure why this question got a downvote so I gave it an upvote. It got me looking in the right direction when I had the same problem recently - I have added a little extra to ensure that the date and time functions don't add unwanted spaces to my pg_dump command. This answer to a similar question gave me the trick for removing spaces: http://stackoverflow.com/a/23558738/341180 – DavidHyogo Apr 30 '17 at 04:52

1 Answers1

0

I repeat the right answer of Tibo to get this question removed from the list of unanswered questions.

The command time /t executed on computer of the questioner returns a time string with  AM or  PM at end. Please pay attention to the space character before AM / PM.

This means that also the string values of the environment variables time, datestr and finally BACKUP_FILE contain a space character.

Therefore "%BACKUP_FILE%" must be used on last line to get the entire file name with path containing a space character read as 1 parameter and not as 2 parameters because of the space character left of AM / PM.

Note: Format of time string output by command time /t depends on time format defined in Windows regional and language settings. For example with using 24 hour format just 17:45 is output instead of 05:45 PM.

Community
  • 1
  • 1
Mofi
  • 46,139
  • 17
  • 80
  • 143