mysqldump via bash script failing with "No such file or directory"

This one's driving me NUTS. Any help appreciated!

$MYSQLDUMP --login-path=$HOST $DATABASE > "$BACKUP_DIR/${HOST}__${DATABASE}__${NOW}.sql";
  • I've confirmed that the binary $MYSQLDUMP (within the script) has a valid full path
  • The command is complete (confirmed via echo)
  • $NOW is set early on in the script, and not here...so the time isn't changing on us or anything
  • This is not being run via cron
  • The user executing the script via command line, also has access/permissions to the actual directory
  • Tried dos2unix to ensure no weird characters. I'm on a Mac, but hey...why not
  • Attempted to escape the > like \>, which instead produces a mysqldump: Couldn't find table: ">" error

This one's driving me crazy. Not sure what I'm missing here? We're just directing output, and I feel like it's something ridiculously obvious I'm overlooking.

UPDATE

Added some more tests to debug and assist here.

vrb "\$BACKUP_DIR: $BACKUP_DIR"
vrb "whoami $(whoami)"
vrb "ls -ld \"$BACKUP_DIR/\" $(ls -ld \"$BACKUP_DIR/\")"
vrb "absolute path of user's home dir: $(cd ~/; pwd)"
vrb "absolute path of \$BACKUP_DIR: $(cd $BACKUP_DIR/; pwd)"

...produces...

20160713T210808Z: $BACKUP_DIR: ~/www/__MYSQL/backup
20160713T210808Z: whoami william
ls: "~/www/__MYSQL/backup/": No such file or directory
20160713T210808Z: ls -ld "~/www/__MYSQL/backup/" 
20160713T210808Z: absolute path of user's home dir: /Users/william
./mygration.sh: line 383: cd: ~/www/__MYSQL/backup/: No such file or directory
20160713T210808Z: absolute path of $BACKUP_DIR: >> ERROR: 1

vrb is just another function that processes verbose output in the script, so don't be alarmed by that. It's only for outputting the debugged info.

If I manually list the contents in the same terminal to ~/www/__MYSQL/backup/, I can see the following:

$ ls -la ~/www/__MYSQL/backup/
total 0
drwxr-xr-x  2 william  staff   68 Jul 13 20:55 .
drwxr-xr-x  6 william  staff  204 Jul 13 20:55 ..

It's weird. Almost like Bash doesn't have access, but my regular user (the one running the bash script in the first place) does.

ANOTHER UPDATE

echo $(whoami);
echo ~;
exit;

...produces...

william
/Users/william

Solution 1:

The problem is that you've put the tilde inside double quotes. When you do this, it is not expanded to the path of your home directory.

Consider:

MacBook-Pro:~ error$ cat x.sh
#!/bin/sh
echo ~
echo "~"
ls ~
ls "~"
MacBook-Pro:~ error$ ./x.sh
/Users/error
~
Calibre Library Downloads   Music       bin
Desktop         Library     Pictures    synergy
Documents       Movies      Public      x.sh
ls: ~: No such file or directory
MacBook-Pro:~ error$ 

If you wish to continue using double quotes, use the $HOME variable instead of the tilde.

MacBook-Pro:~ error$ echo $HOME
/Users/error
MacBook-Pro:~ error$ echo "$HOME"
/Users/error
MacBook-Pro:~ error$