6

I need to upload the entire content of a directory /home/test to my ftp server, in a specific folder. I will then schedule the script hourly via cron. Any examples?

NB. consider that I'm on a Netgear ReadyNAS Duo (a debian box) and I can't install lftp or similars, only standard commands :)

Fabio B.
  • 299
  • 2
  • 6
  • 17
  • 1
    What do you mean "standard commands"? Bash has no "standard command" for ftp. – DerfK Jun 10 '11 at 14:38
  • Is the Netgear ReadyNAS Duo the machine you want to copy from or to? You are unable to install any sort of software or run scripts on it? Is there a possibility of initiating the connection on the FTP server and pulling down the files? – Clinton Blackmore Jun 10 '11 at 15:25

7 Answers7

12

Found this bash script online that has quality documentation:

#!/bin/bash

HOST=ftp.server.com  #This is the FTP servers host or IP address.
USER=ftpuser             #This is the FTP user that has access to the server.
PASS=password          #This is the password for the FTP user.

# Call 1. Uses the ftp command with the -inv switches. 
#-i turns off interactive prompting. 
#-n Restrains FTP from attempting the auto-login feature. 
#-v enables verbose and progress. 

ftp -inv $HOST << EOF

# Call 2. Here the login credentials are supplied by calling the variables.

user $USER $PASS

# Call 3. Here you will change to the directory where you want to put or get
cd /path/to/file

# Call4.  Here you will tell FTP to put or get the file.
put test.txt

# End FTP Connection
bye

EOF

After configuring and saving the .sh script, make it executable:

chmod +x ftpscript.sh

Lastly, configure your cronjob

Mitja
  • 404
  • 5
  • 11
Brendan
  • 411
  • 2
  • 3
  • this stores the password in plain text in your script. a better solution would be: `user $USER` ftp will then prompt for a password. – expz Dec 10 '14 at 16:13
  • 1
    @expz I'm not sure how having the terminal prompt for a password is compatible with scheduling it to run hourly in a cron job... – Michael Shaw Jun 22 '15 at 14:12
  • 1
    I didn't notice. In that case, `chmod a-rw+x ftpscript.sh` would be a tiny bit of security. A more involved solution would use the default SSH key for security: `$PASS=; echo user = $USER:$PASS | curl --disable-epsv --ftp-skip-pasv-ip --digest --anyauth --ftp-ssl -K - -T /path/to/file /path/to/remote/dir` – expz Jul 29 '15 at 16:58
8

command in one line:

ftp -in -u ftp://username:password@servername/path/to/ localfile
Chizhik
  • 81
  • 1
  • 1
8

curl is capable of uploading file(s) to FTP servers.

curl -T "$FILE(s)" -u $USERNAME:$PASSWORD $SERVER/$DIR/

You can also use a glob pattern for $FILE.

Anthony Geoghegan
  • 2,875
  • 1
  • 24
  • 34
Ray
  • 181
  • 1
  • 2
2

if you have 'curl', which is fairly standard, it can do unattended FTP uploads (see man page for the -T option)

0

If ssh is installed and configured to allow file transfers you could use scp.

If not netcat or rsync may be an option.

3dinfluence
  • 12,449
  • 2
  • 28
  • 41
0

In a bash script you should be able to do something like:

    #!/bin/bash
    echo "
    verbose
    open 1.2.3.4
    USER ftpuser ftppasswd
    put /path/to/stuff.tar.gz
    bye
    " > ftp -n > ftp_$$.log

Tarring your directory first would be a good idea and make it much simpler. Also, depending on the size of your directory and the speed of your network, you may want to consider doing differential tars.

sreimer
  • 2,218
  • 15
  • 17
0

If you are able to install or build programs, a utility called ftpsync may be just what you are looking for. [I've never tried it, and there seem to be several utilities either called that or something very similar.]

Clinton Blackmore
  • 3,520
  • 6
  • 36
  • 61