LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Automating FTP upload via a shell script? (https://www.linuxquestions.org/questions/programming-9/automating-ftp-upload-via-a-shell-script-403805/)

Spitty 01-15-2006 10:07 PM

Automating FTP upload via a shell script?
 
Hey all-

I'm trying to write a shell script that performs regular backups of a directory to a FTP server. I googled around a bit, and I found the following script in a howto:

#!/bin/sh
HOST='ftp server address'
USER='ftp username'
PASSWD='ftp password'
FILE='filename'

ftp -n $HOST <<END_SCRIPT
quote USER $USER
quote PASS $PASSWD
put $FILE
quit
END_SCRIPT
exit 0

Now, this is a good start, but I need this script to upload an entire directory, not just one file. So, my questions are:

-Using the basic ftp program for linux, is it possible to tell the ftp program to simply upload a directory? If so, is it possible for the script to go through the local directory and only upload those files that are newer than those on the server, or completely new?

-If it isn't, how do you script a shell script that recurses through a directory and just uploads every file it comes across via ftp, while mirroring the local file structure on the ftp?

Also, if anyone has a simpler method of doing this than with a shell script and ftp, that would be awesome too.

Thanks!

halvy 01-16-2006 03:12 PM

welcome spitty; why don't you look at how sbackup (simple backup ) does it,and maybe even try it ;)

good luck.

BrianK 01-16-2006 05:57 PM

Is there any reason you have to use a basic ftp program? Life will be much easier if you can install ncftpput (part of the ncftp suite). Uploading a directory is pretty simply - I do my backups that way as well. The meat of the upload bit looks something like this:

ncftpput -S .tmp -DD -V -m -r $BACKUP_HOST backups $BACKUP_DIR

which says:

upload all of $BACKUP_DIR (because of the -r) to a directory on $BACKUP_HOST called "backups"
delete local files when done uploading (because of the -DD - you probably won't want this)
no output because we're running in a script (from -V)
don't overwrite destination files until the upload is done (from -S .tmp)
make the directory "backups" on $BACKUP_HOST if it doesn't already exist (from -m)

http://www.ncftp.com/

there's a lot of other useful ftp programs -
list the contents of an ftp dir and echo to stdout
get single file or directory (good for scripts as well)
and a nice interactive ftp client.

halvy 01-16-2006 06:40 PM

i forgot, since you are looking for 'regular' backups, sbackup has plenty of configurations for time/day, regular intervals, etc.

it even has ssh.. and the most important part is, IT WORKS!

i tried it.

it's origins are interesting as well.. it was developed last summer as some sort of google competition.. and it was so impresive that it made it's way right into the debian repositorys :)

Spitty 01-16-2006 07:34 PM

Excellent! ncftpput seems to be exactly what I was looking for- my requirement of the normal ftp program was due to my ignorance of the existence of better things. sbackup would be good if the computer that is going to be running it had a gui, but alas, it's a console-only headless server- ncftpput seems like the better choice for that. That being said, I may very well get sbackup on my personal machine, as it seems like an excellent program.

Thanks for the help, BrianK and halvy- this time last night, I was thinking I would have to write some sort of nightmare recursive shell script- this is definitely much easier!

halvy 01-16-2006 10:28 PM

coool, backups are another thing lin needs to bone up on. i'v tried alota different backup progs and was awefully glad to stumble upon simpe-backup. i tried to setup bacula a coupla times, and i think they need to change the name to bitchula ;)

hopefully you will be able to tear ncftpput and/or sbackup apart and see how it ticks so you can advance your scripting knowledge.


All times are GMT -5. The time now is 08:23 AM.