Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

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

Posted on 2002-06-27
7
260 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
AIX Server 10 88
Problem Imaging Computers With Clonezilla 2 95
Tracking which app / process changes permission of a file in SOlaris 10 4 124
centos commands 6 69
Hello fellow BSD lovers, I've created a patch process for patching openjdk6 for BSD (FreeBSD specifically), although I tried to keep all BSD versions in mind when creating my patch. Welcome to OpenJDK6 on BSD First let me start with a little …
I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
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.

829 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