6

I want to upload backup archives from one server to another server using ftp. In my backup cronjob I use this script to upload files:

MEDIAFILE=/var/somedir/somefile.encrypted
if [ -r $MEDIAFILE ]
# File seems to exist and is readable
then
ftp -n $FTPHOST <<END_SCRIPT
quote USER $FTPUSER
quote PASS $FTPPASS
cd backups
put $MEDIAFILE
quit
END_SCRIPT
fi

This script returns:/var/somedir/somefile.encrypted: No such file or directory. But the file exists and the user executing the script has rights to read the file.

What is causing this error?

i.amniels
  • 325
  • 1
  • 4
  • 9
  • Try (Line 1): MEDIAFILE="/var/somedir/somefile.encrypted" – cyberx86 Jul 05 '11 at 19:34
  • I already tried that and it didn't make a difference. Thanks anyway. – i.amniels Jul 05 '11 at 19:41
  • What shell and what flavour of Linux? (Might not be needed, but I am used to bash/CentOS). – cyberx86 Jul 05 '11 at 19:44
  • @cyberx86 Debian and I use /bin/sh – i.amniels Jul 05 '11 at 19:48
  • No spaces beside your equal sign, no dollar sign for variable assignment, and quoted string - and I don't get the error (on sh). Without quotes I get the error, and other errors for the other scenarios. I might suggest trying it as a basic if then ... else .. fi statement and seeing if that works - put an echo in each section. – cyberx86 Jul 05 '11 at 19:55
  • Does http://pastebin.com/siMYKf0R work (just a basic if - echo)? (Quotes aren't necessary - but no dollar sign, and no spaces around the equal). – cyberx86 Jul 05 '11 at 19:59
  • @cyberx86 It is the same test as in my script, but without the FTP code. I have tested your script and as expected it echos "readable". The file not found error in my question is definitly caused by the FTP code which means the file readability test in my script succeeds. – i.amniels Jul 05 '11 at 20:30

2 Answers2

8

Alright, I should have done this to start with:

FTPHOST="domain.com"
FTPUSER="xxxxxx"
FTPPASS="xxxxxxxxx"
MEDIAFILE=/path/to/something.enc
if [ -r $MEDIAFILE ]
# File seems to exist and is readable
then
ftp -n $FTPHOST <<END_SCRIPT
quote USER $FTPUSER
quote PASS $FTPPASS
cd backups
bin
put $MEDIAFILE something.enc
quit
END_SCRIPT
fi

I added the remote filename to put, and the bin command - tested and works - hopefully it helps.

Edit: I should explain - the put command will assume that the remote path is the same as the local path if a remote path is not specified (second parameter) - so without the remote path, the file was not found on the remote server.

cyberx86
  • 20,805
  • 1
  • 62
  • 81
2

Dollar sign shouldn't be used when assigning value to a variable. So try this code:

MEDIAFILE="/var/somedir/somefile.encrypted"
if [ -r $MEDIAFILE ]; # File seems to exist and is readable
then
ftp -n $FTPHOST <<END_SCRIPT
quote USER $FTPUSER
quote PASS $FTPPASS
cd backups
put $MEDIAFILE
quit
END_SCRIPT
fi
Vladimir Blaskov
  • 6,183
  • 1
  • 27
  • 22
  • Oops, the dollar is a copy/paste error and is not in the real script, I edited my original message. Tnx for pointing out and for all the effort. – i.amniels Jul 05 '11 at 20:22