?
Solved

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

Posted on 2002-06-27
7
Medium Priority
?
274 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
[X]
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
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 800 total points
ID: 7115844
beside the "expect" suggestion, did you ftp server allow to rename files?
0
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 
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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

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

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…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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