I’ve been trying to upload files to my web server form my local computer. I wanted to write a shell script to automate this (it also ties in with resizing images and writing .txt files to be uploaded).
I found scp to be not very useful for this. The problem is the resulting ownership of the files. With scp, you log in to your remote server via ssh. I have my ssh pretty securely (I hope!) locked down, so only a single user can log in, and that is only with authenticated keys. This means that when I upload using scp the files are owned by the user that uploaded them. This in turn means that when my web server wants to access them, it can’t because it doesn’t own the files. You can’t change ownership of files unless you are root.
My solution to this was to find a method of using ftp over ssh. The advantage of ftp is that it authenticates as a user that has read and write access to the web files. Any files uploaded by the ftp user, are automatically accessible to the web server, at least on my set-up. So here is how my eventual script looked;
#!/bin/bash #Upload via lftp ftpUser="fptUser" ftpHost="ftpHost" ftpPass="ftpPassword" remoteFolder="path/to/remote/folder" #I'll copy the files to my server from this folder cd "/path/to/current/local/directory" #The EOF bit passes input to the command prompt once logged in # mput * copies all the files in the current working folder. lftp -du $ftpUser,$ftpPass $ftpHost <<EOF cd web/images/stories mkdir "$rname1-$rname2" cd "$rname1-$rname2" mput * bye EOF #End