What I'm trying to write is a shell script that will back up a website and all the MySQL DBs that support it. I found examples all over the 'net, but when I run the script in bash in a jailed SSH session, it fails. If I type the commands straight into the SSH session, they work fine.


THEDATE=$(date +"%Y-%m-%d")

mysqldump -u $THEDBUSER -p$THEDBPW $DB1 > dbbackup_$DB1_$THEDATE.sql

tar -czf sitebackup_website_$THEDATE.tar /home/website/public_html
gzip sitebackup_website_$THEDATE.tar

This script lives on the web server, so for testing purposes, I SSH into the server, cd into the directory, and run bash backup.sh. mysqldump isn't connecting, seeming to fail to pass the password in (it returns an error reporting 'using password: NO'). Then the tar command fails, reporting that it can't stat /home/website/public_html with a 'no such file or directory'. tar then reports it is exiting with error status due to previous errors.

What am I missing here?

  • 1
    Can you explain what "a jailed SSH session" means. Are you running ssh foo chroot /bar bash backup.sh? Does it work without the chroot?
    – phemmer
    May 9, 2014 at 12:45
  • Do you still have problems if you quote your variables? Use "$var" or "${var}"
    – terdon
    May 9, 2014 at 13:58

4 Answers 4


Check the script for Windows line endings by logging in on the server and running

cat -v /path/to/script

If the line ends with ^M, that is the problem.

You can fix a file with broken line endings by running

dos2unix /path/to/script

If dos2unix doesn't exist on the server, you can instead use sed, like this:

sed -i 's/\r$//' /path/to/script
  • This was exactly it. Once I ran dos2unix on the script, it worked just fine. Thanks! May 11, 2014 at 4:56

You have assigned the username to variable THEUSER, but your mysqldump command is using the variable THEDBUSER.

  • 2
    That's true, but why would that give an error saying it can't start at all because of 'no such file of directory'?
    – Simkill
    May 9, 2014 at 9:32
  • Nice catch - I did have the same mistake in the file. At least I caught that, with your help, before I tried to run the script again. May 11, 2014 at 5:00
tardir=$PWD ; cd /home/website/public_html && 
    tar -czf "$tardir"/sitebackup_complete-office_"$THEDATE.tar"

or just:

tar -C/home/website/public_html -czf \

In general many tars will fail to work at all with absolute paths unless explicitly instructed to do so.

By default, GNU tar drops a leading / on input or output,and complains about file names containing a .. component. This option turns off this behavior.

It's entirely possible that ssh's commands are resolving paths beforehand depending on command delivery and quoting. The same is relevant to the expansion of the variables in your pathnames.


If I understand you description of the problem correctly, the key difference seems to be the jailed SSH session - processes running inside the jailed session cannot see outside of their jail directory (often their home). That's why you don't experience problems in non-jailed session. Yo might also have a look at the wikipedia Jail entry for more details.

  • It's not clear to me that "typing the command directly into the ssh session" refers to a non-jailed session - but it would be nice to have it verified.
    – Jenny D
    May 9, 2014 at 12:19
  • You're right, Jenny D - all SSH is jailed for me. May 11, 2014 at 4:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .