Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2002-06-27
7
Medium Priority
?
279 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Tech or Treat! - Giveaway

Submit an article about your scariest tech experience—and the solution—and you’ll be automatically entered to win one of 4 fantastic tech gadgets.

Question has a verified solution.

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

When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
FreeBSD on EC2 FreeBSD (https://www.freebsd.org) is a robust Unix-like operating system that has been around for many years. FreeBSD is available on Amazon EC2 through Amazon Machine Images (AMIs) provided by FreeBSD developer and security office…
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…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:
Suggested Courses

618 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