Solved

ftp files and backup

Posted on 2011-03-15
7
490 Views
Last Modified: 2012-06-21
I want to ftp some local files to remote server but I want to backup the remote files before ftp.

My Requirement in detail:
----------------------------
Login to ftp account,
create a temp directory,
mover all the *.zip files that are in ftp home directory to this temp directory,
then copy the local machine files to this remote home directory.

I read somewhere that we cannot move the contents in the remote directory to another remote directory so I created a local directory and got all the files from remote machine and did a mput to move all the files back to the new directory. Somehow the script is not executing any commands after the mget. Below is the script.

cd bkp_files
rm -rf bkp_remote_files
mkdir bkp_remote_files

ftp -in  << EOF >> $LOG
        verbose
        open ${SERVER}
        $LOGON
        mkdir $BKPFILES
        binary
        prompt
        lcd bkp_remote_files
        mget *.zip
        cd $BKPFILES
        mput *.zip
        cd ..
        lcd ..
        mput *.zip
        close
        bye
      EOF

Open in new window

0
Comment
Question by:cutie_smily
[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 16

Assisted Solution

by:AlexPace
AlexPace earned 200 total points
ID: 35143139
You can "move" files on an FTP site by renaming them with the path in the name.

I'm not sure about the unix syntax but this is how you could script it in the Robo-FTP scriptable windows FTP client:
FTPLOGON "127.0.0.1" /user=myUserID /pw=secret
IFERROR!= $ERROR_SUCCESS GOTO halt
FTPMAKEDIR "temp_folder"

:top_of_loop
FTPGETFILE "*.zip"  ;; find next zip file in root
IFERROR!= $ERROR_SUCCESS GOTO upload_files
SET new_filename = "/temp_folder/" + %sitefile
FTPRENAME %sitefile new_filename  ;; move remote file
IFERROR!= $ERROR_SUCCESS GOTO halt
GOTO top_of_loop

:upload_files
WORKINGDIR "bkp_files"  ;; set local working folder
SENDFILE "*.zip"  ;; upload all zip files

:halt
FTPLOGOFF
STOP   ;; use EXIT here if running w/o GUI

Open in new window

Notice it does a loop to get the name of each zip file and then rename it into the /temp_folder sub-directory.  I know the exact syntax above won't work for you but this is an FTP client so if it can do it you should be able to use the same idea.
0
 

Accepted Solution

by:
cutie_smily earned 0 total points
ID: 35143721
I have run this in UNIX (Kshell). I am not sure if I will be able to run this script.
0
 
LVL 16

Expert Comment

by:AlexPace
ID: 35146485
I'm sorry I didnt mean for you to use the Robo-FTP script in UNIX it is a windows-only program.  I was just showing the script as an example of how to move files on an FTP server by renaming them into another folder...  changing the name of the parent folder effectively MOVES the file.  See?
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 19

Expert Comment

by:simon3270
ID: 35399740
I object to the closure - I have a proposed solution.

I have tested this on a Linux FTP client and an HPUX FTP server (the only ones I can get to at the moment!), and you can move a file on the remote server with a RENAME command - you just specify the new directory, but keep the filename the same - for example:
  ren file1.zip backup_dir1/file1.zip

The script below connects to the FTP server to get a list of files in the home directory.  It takes the .zip file names from this and for each one, sets up the rename command.  The script then logs in to the FTP server again, creates the new backup directory, moves the files from the root directory to the backup directory, then moves all zip files from the current directory to the ftp server home directory.

One thing to note - the "EOF" should be at the start of the line it is on for "here" documents - there is a way (use "<<-EOF" with a hyphen instead of "<<EOF") to get tabs before the EOF ignored, but it doesn't work with spaces, so could easily be broken after an edit.  I also usually put the contents of the here document at the start of the line too, just to avoid any possible errors.
LOGON="user my_user my_passwd"
BKPFILES=ftpback_$(date '+%Y%m%d_%H%M%S')
LOG=ftpback.$(date '+%Y%m%d_%H%M%S').log
SERVER=remotesrv2
TMPF=/tmp/ftpback$$

# Get a full directory listing of FTP home directory into $TMPF.filelst
ftp -in <<EOF > $TMPF.filelst
open $SERVER
$LOGON
dir
quit
EOF

# Set up the start of the FTP script to rename and copy
cat > $TMPF.ftpcmd <<EOF
verbose
open $SERVER
$LOGON
mkdir $BKPFILES
binary
prompt
EOF

# Now find any names ending ".zip", and use them
# to set up the rename commands
awk -v bkp=$BKPFILES '/\.zip$/{printf "ren %s %s/%s\n", $NF, bkp, $NF}' $TMPF.filelst >> $TMPF.ftpcmd

# and add the trailing commands
cat >> $TMPF.ftpcmd <<EOF
mput *.zip
close
bye
EOF

# Now run the FTP script to move the old zip files,
# and copy the new ones
echo Backup files in $BKPFILES on $SERVER > $LOG
ftp -in < $TMPF.ftpcmd >>$LOG

echo Log in $LOG

rm -f $TMPF.*

Open in new window

0
 
LVL 19

Expert Comment

by:simon3270
ID: 35399804
Forgot to say, you run the above script in the directory where the *.zip files are to be copied to the FTP server.  And there must be no spaces in the zip filenames.
0
 
LVL 1

Expert Comment

by:modus_operandi
ID: 35473609
Restarting auto-close process on cutie_smily's behalf.
 
modus_operandi
EE Admin
0

Featured Post

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

Question has a verified solution.

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

Hello to you all, I hear of many people congratulate AWS (Amazon Web Services) on how easy it is to spin up and create new EC2 (Elastic Compute Cloud) instances, but then fail and struggle to connect to them using simple tools such as SSH (Secure…
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 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.:
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…
Suggested Courses

626 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