Solved

FTP scripting - Automate download of files and move files to an archive directory

Posted on 2012-03-30
6
1,800 Views
Last Modified: 2012-06-27
One of my clients has a connection to an Unix environment where he starts some applications wich he uses in a Windows environment.

There is also a directory wich we can access by use a NFSclient, but after 2 days we have to reboot the server, to get the connection up and running again.

There is an other option, wich is downloading the files to a local directory by using FTP.

Now i am looking for a way to download the files and i have the following:

open <IP-address>
<username>
<password>
cd ..
cd ..
cd ..
cd ..
cd ..
cd var
cd appl
cd <application name>
prompt
lcd N:\<localdirectory>
mget *.aaa
mget *.AAA
mget *.bbb
mget *.BBB
bye

Open in new window


This works when i run it, but the files are still on the remote FTP location.

What i would like to do is putting these files in the Remote FTP directory ARCHIVE for an example.

I have found a sample code on a unix / linux form
#!/bin/sh
clear
# change local directory
cd [local-directory]

#collect file names
ftp -ni ftp.abccompany.com <<EOF
user [user] [password]
cd /OUT
mls W945*.* list.txt 
quit
EOF

# create ftp action list
echo >>todo.lst user [user] [password]
while read N
do
    echo >>todo.lst cd /OUT
    echo >>todo.lst get $N
    echo >>todo.lst rename $N ARCHIVE/$N
done <list.txt

echo >>todo.lst quit

# ftp transfer process
ftp -nv ftp.abccompany.com <todo.lst

# cleanup
rm todo.lst

Open in new window


But these commands doesn't work.

Can someone help me to get this working?
0
Comment
Question by:RemcoVi
6 Comments
 
LVL 13

Expert Comment

by:Sandy
ID: 37793229
Please use pure-ftp having upload and run after scripts.
0
 
LVL 20

Accepted Solution

by:
carlmd earned 500 total points
ID: 37795727
The following script works... use it all the time.

#!/bin/ksh
#
    HOST='remotehostname'
    USERN='anonymous'
    PASS='mm'
    cd /usr/sys/inst.images/update
    echo `ftp -inv ${HOST} >/home/ftp.como <<EndFTP
    user "${USERN}" "${PASS}"
    cd /remote/path
    binary
    mget *
    quit
    EndFTP`

NOTE that the marks before the "ftp" and after"EndFTP" are back tics. You MUST use these. I suggest you cut and past this script into vi or other editor on unix and edit it there.

remotehostname -> the path/url to the remote site you want to get files from
USERN=  -> the login name you use at the remote site
PASS=  -> the password you use at the remote site
cd /usr/....  -> this is a "cd" on you local system to where you want to put the downloaded files
echo   -> this creates a file in the path /home/ftp.como (change as you wish) that contains all that goes by in the download,
                  this is valuable if you have any problems or just want to see what happens
user   -> this logs you in on the remote system (don't change this)
cd /remote/path  -> change this to be the path on the remote site where you want to get the files from
binary -> this indicates the download is a binary file, if files are ascii you can omit this line, but it does not hurt anyway
mget * -> get all the files in the directory you cd'ed to. If you wanted all files that started with "text" then make this mget text*

Save this script with your changes and make sure it is executable. (chmod a+x script).

You can run this script without using a terminal session by using the "at" command
For example:
        at now today /path/myscript             will run the script in the background now
        at 8 pm today /path/myscript           will run the script at 8PM today
for other option see man pages for at
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 37797289
To get the files and then move them to another directory, try this:
#!/bin/ksh
#
SRCHOST=remoteHost
USER=MySelf
PASS=WhoCares
SRCDIR=/remote/file/dir
ARCDIR=/remote/file/archive
TGTDIR=/local/file/path

cd $TGTDIR

#get list of file names to transfer
ftpcmd=`
ftp -ni $SRCHOST <<EOF | grep -v ^ftp | xargs -i? echo "get "?"\nrename "?" $ARCDIR/"?
user $USER pass $PASS
cd $SRCDIR
ls -1  *.aaa
ls -1  *.AAA
ls -1  *.bbb
ls -1  *.BBB
bye
EOF`

# Execute the file transfer
ftp -ni $SRCHOST <<EOF
user $USER pass $PASS
cd $SRCDIR
$ftpcmd
bye
EOF

Open in new window

:p
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Author Comment

by:RemcoVi
ID: 37799775
Problem is that these scripts are for running on a unix environment, but i am connecting to a Unix environment, not running one.

I need to start the command on a Windows 2008 server, wich then connects to the ftp running on a Unix environment.

I tried the above anyway, but i can't get it to work.
0
 
LVL 20

Assisted Solution

by:carlmd
carlmd earned 500 total points
ID: 37800129
Sorry, the fact that you wanted to run this from Windows was not clear.

Take a look at:

http://support.microsoft.com/kb/96269

and

http://support.microsoft.com/kb/555976

and

http://www.howtogeek.com/howto/windows/how-to-automate-ftp-uploads-from-the-windows-command-line/

one of these should answer your question
0
 

Author Closing Comment

by:RemcoVi
ID: 37979291
I just bought a software license for a specific product wich makes scripting easier on FTP and it also has the option to do the things i want.
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Suggested Solutions

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

856 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