1

I'm trying a simple bash script to backup my source files from my website root directory to my /home/backups/files directory.

script2.sh

#!/bin/bash
#START
TIME=`date +%b-%d-%y`
FILENAME=backup-$TIME.tar.gz
SRCDIR="var/www/html/My_Site"
DESDIR="/home/backups/files"
tar cpzfP $DESDIR/$FILENAME $SRCDIR
#END

Error:

tar: /var/www/html/My_Site\r: Cannot stat: No such file or directory
tar: Exiting with failure status due to previous errors
  1. I cross checked for Whitespaces in each line of code, there are none.

  2. Cross checked the folder permission for both SRC and DES, its 0777.

I'm not getting any idea as to why I get Cannot stat: No such file or directory.

  • 1
    Where did you create this script? Windows? – muru Nov 28 '16 at 9:57
  • no in my Ubuntu VPN and running this script from the same terminal – Sushivam Nov 28 '16 at 9:57
  • Add the output of file script2.sh to your question, please. – muru Nov 28 '16 at 9:58
2

It looks to me like you script has Windows carriage returns in it, which are causing the failure. The clue to this is the \r at the end of the path in the SRCDIR variable, which is shown in the error code as /var/www/html/My_Site\r. Obviously that path does no exist with the added \r on the end.

Here's a sed command which you can run on your script to remove the carriage returns, it will make a backup of the original script, named script2.sh.bak.

sed -i.bak 's/\r//g' script2.sh

The script should work correctly now, you can delete the backup version once you've verified this.

Another method to get rid of those unwanted carriage returns would be to use tr -d '\r' script2.sh, but you'd need to direct the output to a new file, and copy that in place of the old one.

One other thing that would be useful would be to ensure that you quote your variables when you use them in your script. It's a good practice to get into.

| improve this answer | |
  • cool...i dono why sed dint come to my thought though...Thanks Arronical, it should work now! – Sushivam Nov 28 '16 at 11:31
2

Do you have rights to write into DESDIR folder?

Also, to avoid "tar: Removing leading `/' from member names" try this:

tar cpzfP $DESDIR/$FILENAME $SRCDIR

I don't know why, but this works for me.

| improve this answer | |
  • have updated the error list and code, yes DESDIR have full 0777 and also the SRCDIR...but still why is it not able to find the files... – Sushivam Nov 28 '16 at 9:32

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.