Solved

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

Posted on 2002-06-27
7
256 Views
Last Modified: 2010-04-21
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
0
Comment
Question by:lphillips120898
7 Comments
 
LVL 40

Expert Comment

by:jlevie
ID: 7114531
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.
0
 
LVL 6

Expert Comment

by:bira
ID: 7114553
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.
 
0
 
LVL 51

Accepted Solution

by:
ahoffmann earned 200 total points
ID: 7115844
beside the "expect" suggestion, did you ftp server allow to rename files?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 1

Expert Comment

by:petereilering
ID: 7116144
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
0
 

Author Comment

by:lphillips120898
ID: 7116158
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
0
 
LVL 51

Expert Comment

by:ahoffmann
ID: 7117047
Could you please explain the grading?

If SSH is enabled, you simply can do:
   ssh user@web-server mv oldfile newfile
0
 

Expert Comment

by:aabedi
ID: 11013690
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.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In tuning file systems on the Solaris Operating System, changing some parameters of a file system usually destroys the data on it. For instance, changing the cache segment block size in the volume of a T3 requires that you delete the existing volu…
A metadevice consists of one or more devices (slices). It can be expanded by adding slices. Then, it can be grown to fill a larger space while the file system is in use. However, not all UNIX file systems (UFS) can be expanded this way. The conca…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now