Solved

ftp files and backup

Posted on 2011-03-15
7
460 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
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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

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…
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.
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 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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now