3

I'm using a Bash script to backup my webfiles via FTP. As the title says, I have an Ubuntu webserver and am backing up to a Windows machine. I have an ssh program and FileZilla Server on the Windows machine, and can SSH and SFTP into it. The core of the script looks like this:

SRCDIR="C:\\Users\\Tech1\\testserverbackup"
DATAIN="/var/www/html/"
FILENAME="-r *"

sshpass -e sftp -oBatchMode=no -b - ${USER}@${LANHOST} << EOF
cd  ${SRCDIR}
lcd ${DATAIN}
mkdir $(date -I)
cd $(date -I)
put ${FILENAME}
bye
echo made it
EOF

The others vars are a bit sensitive, so I don't want to post them, but the credentials have been working for me so far.

The error I'm getting looks like this:

sftp>       cd  C:\Users\Tech1\testserverbackup
Couldn't stat remote file: No such file or directory

I've ssh'd into the folder and sftp'd, so I'm not really sure what the issue is. AFAIK, cd is the native windows command, not just the FTP one.

Any ideas what's going wrong? Thank you.

jfa
  • 1,047
  • 3
  • 13
  • 39
  • 1
    FTP hosts do not have the concept of mounted drives, as Windows systems do. As the error says, there is no file/directory with `C:` in the path while connecting via FTP. – admdrew Jul 16 '14 at 20:34
  • It means that when you're connected via FTP (ie, on the `sftp>` prompt), there isn't a `C:` drive to connect to, so trying to `cd` to that path simply won't work. – admdrew Jul 16 '14 at 20:36
  • `cd` isn't Windows-specific, that's not the problem. Using `C:` as part of the file/folder path **is** Windows-specific, and has no meaning when on an FTP host. – admdrew Jul 16 '14 at 20:37
  • You can still use absolute paths, but when connecting to an FTP host, it likely uses the Linux/Unix convention, so it likely starts with `/` (like the path in your `$DATAIN` variable). – admdrew Jul 16 '14 at 20:39
  • Replacing `C:\ ` with `/` returns the same error. – jfa Jul 16 '14 at 20:43
  • 2
    Yes, the entire path is probably incorrect. What's your output when you type just `pwd` while logged into FTP? – admdrew Jul 16 '14 at 20:45
  • 1
    Ah, good point. It's /C/Users/Tech1, so that's what I'll use. Thank you for your help. – jfa Jul 16 '14 at 20:48
  • @admdrew Interesting, so that means that `/` is above `C`, and going there, I can view all mounted drives. – jfa Jul 16 '14 at 20:55
  • 1
    Yes, `/` is the top-level/root folder. In your environment, the FTP administrator mapped a Windows `C:` drive to a regular folder called `C` within the FTP server (and mapped all the other mounted drives to folders). – admdrew Jul 16 '14 at 20:57
  • 1
    @jfa, you should answer your own question and accept it, so that others who have the same question don't have to pore through the comments. – glenn jackman Jul 16 '14 at 21:04

2 Answers2

1
sftp>       cd  C:\Users\Tech1\testserverbackup
Couldn't stat remote file: No such file or directory

This is due to FTP servers not having the concept of drive letters for mounts, as Windows systems do.

You can use pwd to while connected to FTP to determine the directory you're currently in:

sftp> pwd
/C/Users/Tech1

...which shows that in your environment, the FTP administrator mapped a Windows C: drive to a regular folder called C within the FTP server (and mapped all the other mounted drives to folders).

admdrew
  • 3,790
  • 4
  • 27
  • 39
0

You make change the ownership of this directory in the Linux:

$ sudo chown $USER /path/directory

I found this solution here.

James Risner
  • 5,451
  • 11
  • 25
  • 47
  • Andwer not relevant (though I won't downvote as you are new here) The remote machine is Windows, `chown` won't work as that is a Unix command. The reason the person has an issue is that often FTP servers on Windows run in a sandbox. Hence you can only send and receive files in that sandbox tree. Accessing files outside that tree is forbidden unless explicitly configured on the server. This default is used as sharing the entire filesystem in Windows is a bad idea as security in Windows is (or was) rather relaxed by default. – Jay M Apr 06 '23 at 14:09