Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

ftp files and backup

Posted on 2011-03-15
7
Medium Priority
?
493 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 800 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
Independent Software Vendors: 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 20

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 20

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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Attention: This article will no longer be maintained. If you have any questions, please feel free to mail me. jgh@FreeBSD.org Please see http://www.freebsd.org/doc/en_US.ISO8859-1/articles/freebsd-update-server/ for the updated article. It is avail…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
Viewers will learn how to properly install and use Secure Shell (SSH) to work on projects or homework remotely. Download Secure Shell: Follow basic installation instructions: Open Secure Shell and use "Quick Connect" to enter credentials includi…
Suggested Courses

721 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