LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 10-28-2010, 02:04 AM   #1
thunyiwe
LQ Newbie
 
Registered: Nov 2006
Distribution: RHEL4,5,6&7,Debian sarge,etch & Fedora 18,19,20
Posts: 28

Rep: Reputation: 15
copy whole directory to ftp server


Hello

I have this scripts that is meant to create certain files and a directory by date and once its finished its meant to copy that direcory an the example ftp server. Can someone check the script for me and advise?



#!/bin/bash
# script that FTPs the given file to a host

FTPHOST="example.example.com"
DATE=`date +%d%m%Y`
DATESTRING=`date +%d/%m/%Y\ %H:%M:%S`
LOG="/data/logs/ftpScript.log"
FTPINPUT="/data/printData/ftpCommandFile"
FTPOUTPUT="/data/printData/ftpOutputFile"
DATADIR="/data/printData"
TIMESTRING=`date +%H%M%S`

# do we have an argument?
if [ -z "$1" ]; then
echo $DATESTRING" - ERROR: no argument supplied" >> $LOG
exit
fi

# is it a file?
if [ ! -e "$1" ]; then
echo $DATESTRING" - ERROR: supplied argument does not exist" >> $LOG
exit
fi

# does the date folder exist in the data dir?
if [ ! -d $DATADIR/$DATE ]; then
mkdir $DATADIR/$DATE
# add a line to make the directory on the FTP host
EXTRALINE="mkdir $DATE"
else
EXTRALINE=""
fi

# what is just the filename portion?
FILENAME=`basename "$1"`

# does the target file already exist in the target folder?
if [ -e "$DATADIR/$DATE/$FILENAME" ]; then
NEWFILENAME="$FILENAME.$TIMESTRING"
else
NEWFILENAME="$FILENAME"
fi

# copy the argument there
cp "$1" "$DATADIR/$DATE/$NEWFILENAME"

# check that it copied fine
if [ ! -e "$DATADIR/$DATE/$NEWFILENAME" ]; then
echo $DATESTRING" - ERROR: error copying file" >> $LOG
exit
fi
DIFFSTRING=`diff "$1" "$DATADIR/$DATE/$NEWFILENAME" 2>&1`
if [ ! -z "$DIFFSTRING" ]; then
echo $DATESTRING" - ERROR: copied files differ" >> $LOG
exit
fi

# make the FTP command script
echo "user oper oper" > $FTPINPUT
echo "cd moblas6" >> $FTPINPUT
echo $EXTRALINE >> $FTPINPUT
echo "cd $DATE" >> $FTPINPUT
echo "bin" >> $FTPINPUT
echo "lcd $DATADIR/$DATE" >> $FTPINPUT
echo "put \"$NEWFILENAME\"" >> $FTPINPUT
echo "bye" >> $FTPINPUT

# execute the script
ftp -nvi $FTPHOST < $FTPINPUT > $FTPOUTPUT 2> /dev/null

# log the transmission
echo $DATESTRING" - '$1' sent" >> $LOG

# and exit
exit
 
Old 10-31-2010, 09:00 PM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
It might be quicker if you just add 'set -x' and post the output

Code:
#!/bin/bash
# script that FTPs the given file to a host

# uncomment during debugging
set -x

FTPHOST="example.example.com"
DATE=`date +%d%m%Y`
...
cheers
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ftp user can't create a directory on ftp server bharatlalgupta Red Hat 4 01-27-2010 01:58 PM
own directory share in ftp server. rahuldevalone Linux - Server 5 10-14-2008 04:49 AM
is there the x directory in an ftp server? kpachopoulos Programming 1 07-24-2005 03:44 PM
Using wget to copy entire ftp directory stuartmunro Linux - Newbie 7 06-17-2005 07:06 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:03 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration