Linux FTP upload: "No such file or directory", but file exists

15,365

Solution 1

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.

Solution 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
Share:
15,365
i.amniels
Author by

i.amniels

Updated on September 18, 2022

Comments

  • i.amniels
    i.amniels almost 2 years

    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?

    • cyberx86
      cyberx86 almost 13 years
      Try (Line 1): MEDIAFILE="/var/somedir/somefile.encrypted"
    • i.amniels
      i.amniels almost 13 years
      I already tried that and it didn't make a difference. Thanks anyway.
    • cyberx86
      cyberx86 almost 13 years
      What shell and what flavour of Linux? (Might not be needed, but I am used to bash/CentOS).
    • i.amniels
      i.amniels almost 13 years
      @cyberx86 Debian and I use /bin/sh
    • cyberx86
      cyberx86 almost 13 years
      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
      cyberx86 almost 13 years
      Does pastebin.com/siMYKf0R work (just a basic if - echo)? (Quotes aren't necessary - but no dollar sign, and no spaces around the equal).
    • i.amniels
      i.amniels almost 13 years
      @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
    i.amniels almost 13 years
    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
    i.amniels almost 13 years
    Thanks a lot for all the effort you have put in this solution! :)