Link to home
Start Free TrialLog in
Avatar of lphillips120898
lphillips120898

asked on

script to FTP a file to another server - making backup of the current one first

I need a script that will FTP a file to another UNIX server.  In addition to this I will need to have the file that is about to be over written to be copied to a timestamped backup.

Any suggestions/examples on how to accomplish this?

Thanks,

Lisa
Avatar of jlevie
jlevie

A scripted transfer of the file with ftp (and a .netrc file) or, even easier, with ncftpput isn't much of a problem. The wrinkle is the need to rename the existing file. For that I'd think that an expect script would be the best choice, as I'd imagine that checking to see that the rename succeeded before uploading the file is important. I'm not where I can write/test a sample expect script right now, but I'll post one to this question later today or tomorrow.
Hi
   An example here

 echo "Enter filename"
 read file
 rsh RemoteServer mv $file $file.bkp.`date +%d%m`
 ftp -n RemoteServer  <<endftp
 user username password
 prompt
 put $file
endftp

  Note that  RemoteServer must be included in /etc/hosts.
 
ASKER CERTIFIED SOLUTION
Avatar of ahoffmann
ahoffmann
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use expect
see working example:

#!/usr/bin/expect
set timeout 1
cd /var/named
exec cp /etc/named.conf /var/named/
system tar cf - * | gzip -9 > /home/backup/ns1.db.files-`date +%d.%m.%y`.tgz

# begin met de ftp sessie

spawn ftp backup
expect "Name"
send "backup\r"
expect "Password:"
send "ZH0p1k\r"
expect "logged"
send "bi\r"
expect "I"
send "prompt off\r"
expect "off"
send "lcd /home/backup\r"
expect "now"
send "cd ns1\r"
expect "successful"
send "mput *tgz*\r"
expect eof
expect "ftp"
send "bye\r"
system rm -f /home/backup/*
exit
Avatar of lphillips120898

ASKER

There has been an added twist to moving/renaming a file to another server, which I'm going to post a question about.  Bira, since the script you provided the info I needed to complete the process.  The FTP part worked, but RSH gave a permission denied error, I'm assuming there is something that needs to be done in UNIX to enable this.

Thanks to all - and hopefully someone will have some insight on the real problem, which will be having to SSH to a web server (FTP is not enabled) to mv the file and send the file over from a remote box.

Lisa
Could you please explain the grading?

If SSH is enabled, you simply can do:
   ssh user@web-server mv oldfile newfile
Can I suggest you use autoexpect. This basically allows you to continue working as normal inside an expect shell and then writes what you type and the response you get back to an expect script, which you then execute as normal.

Command: autoexpect -f <script_name>
expect1.1> ftp hostname
...ftp conversation...
quit
expect1.1>ssh hostname
...ssh conversation...
exit
expect1.1> exit

I find this easier than playing around with the script from the start. If you do use this message, don't forget to set your conservative parameter to 1 (as opposed to 0) to allow for a 1/10 second delay. More info available by doing man autoexpect.