I'm getting an error with a very simple script. The script should run pg_dumpall and write the output to a file but I'm getting an error that the resulting file doesn't exist. I know it doesn't exist, thats why I want to creat it! :-/

#remove previous day's dump and create new
rm -f /home/user/db_dump_*.txt;
pg_dumpall -U postgres -w > /home/user/db_dump_`date '+%d/%m/%Y_%H:%M:%S'`.txt;

The result is:

db_dump.sh: line 4: /home/user/db_dump_12/02/2017_14:30:15.txt: No such file or directory 

The date command is working fine and the filename comes out perfect but instead of the file being created, I get the error saying that it doesn't exist. I'm running the script as root so there should be no issues with permissions. I can write to the user's home directory without any problems.

Any ideas?

  • 1
    Since your filename contains slashes, try pre-creating the directories involved before trying to write to a file inside them. In this instance, mkdir -p /home/user/db_dump_12/02. On the other hand, I urge you to reconsider your directory structure as it doesn't seem logical to, say, keep all of the files written on the 12th of any month in a given directory. Perhaps if your date format were big-endian instead oflittle-endian it might make more sense. – Celada Feb 12 '17 at 20:19

Check that generated filename again:


It tries to write into a subdirectory called 02. If that directory doesn't exist, you'll get "No such file or directory".

I suggest using another date format:

dumpfile_date=$( date +"%F-%T" ) # or %Y%m%d-%H%M%S or something similar
pg_dumpall -U postgres -w > "/home/user/db_dump_$dumpfile_date.txt"
  • derp... Yeah, that'll do it... Slashes in the date are gonna be read as a path... – rijo79 Feb 12 '17 at 20:22
  • I just replaced the slashes with hyphens in the date parameters and it works the way I want. Thanks! – rijo79 Feb 12 '17 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.