ftp files and backup

Posted on 2011-03-15
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
        open ${SERVER}
        mkdir $BKPFILES
        lcd bkp_remote_files
        mget *.zip
        cd $BKPFILES
        mput *.zip
        cd ..
        lcd ..
        mput *.zip

Open in new window

Question by:cutie_smily
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
LVL 16

Assisted Solution

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 "" /user=myUserID /pw=secret
FTPMAKEDIR "temp_folder"

FTPGETFILE "*.zip"  ;; find next zip file in root
SET new_filename = "/temp_folder/" + %sitefile
FTPRENAME %sitefile new_filename  ;; move remote file
GOTO top_of_loop

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

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.

Accepted Solution

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.
LVL 16

Expert Comment

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?
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 19

Expert Comment

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 backup_dir1/

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

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

# Set up the start of the FTP script to rename and copy
cat > $TMPF.ftpcmd <<EOF
open $SERVER

# 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

# 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

LVL 19

Expert Comment

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.

Expert Comment

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

Featured Post

[Webinar] Code, Load, and Grow

Managing multiple websites, servers, applications, and security on a daily basis? Join us for a webinar on May 25th to learn how to simplify administration and management of virtual hosts for IT admins, create a secure environment, and deploy code more effectively and frequently.

Question has a verified solution.

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

Suggested Solutions

Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap ( Version 1.2 2.      Jpcap( Version 0.6 Prerequisite: 1.      GCC …
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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

734 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