Need help in deleting old files from a ftp server


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help in deleting old files from a ftp server
# 15  
Old 06-24-2009
FYI, My job has the following 2 components:
  1. remoteFtpServerCleanUp.ksh: Goes through the local archive directory and creates a list of all the old files that need to be deleted. Then it internally calls 'remoteFtpServerCleanUp.pl' for eac file that needs to be deleted.
  2. remoteFtpServerCleanUp.pl: Connects to the remote ftp server and deletes the requested file.
You can customize the scripts as per your requirements.

USAGE:
Code:
remoteFtpServerCleanUp.ksh -f myremoteserver.net -u myuser -p mypswd -l /data/mylocaldirectory -m "myfilepattern*.txt" -r 2
-f Remote FTP server name
-u Remote FTP server user name
-p Remote FTP server password
-l Local archive directory
-m File pattern that needs to be deleted
-r Retention period

remoteFtpServerCleanUp.ksh:

Code:
#!/bin/ksh
#
####################################################################################
#                                                                                  #
#  Script Name : remoteFtpServerCleanUp.ksh                                        #
#  Date        : 26-Jan-2009                                                       #
#  Developer   : Arun Kumar                                                        #
#  Description :                                                                   #
#                                                                                  #
####################################################################################

# Initialize all the variables
. /datapool/mlbdba/mlbdba_crontab_env

# Defined constant values
typeset UTIL_FILE_DIR="${HOME}/files/utilities"
typeset TEMP_DIR="${HOME}/remoteFtpServerCleanUp"
typeset PROXY_SERVER="my-proxy-vip.abc.com"
typeset DB_MAIL_GROUP="arun.kumar@abc.com"
typeset LOG_FILE=""

PGM_NAME=`basename $0`
BASENAME=`echo $PGM_NAME|cut -f1 -d "."`
LOG_FILE=${TEMP_DIR}/${BASENAME}.`date '+%m%d%Y'`.log.$$
OLD_LOG_FILE=${LOG_FILE}
>>${LOG_FILE}



USAGE="remoteFtpServerCleanUp.ksh -f <Remote FTP Server> -u <FTP User> -p <FTP password> -d [Remote FTP directory] -l <Local directory> -m <Local file match pattern> -r <Retention period in days>"

#Variable declaration
typeset -i exitStatus=0
typeset remoteFtpServer=""
typeset ftpUserNm=""
typeset ftpPassword=""
typeset remoteFtpDirectory=""
typeset localDirectory=""
typeset fileMatchPattern=""
typeset -i retentionPeriod=0
typeset -i cutOffPeriod=30

#********************************************************#
# Main program starts here
#********************************************************#

echo "Starting ${PGM_NAME}..." >> ${LOG_FILE}


#********************************************************#
# Initial Level Validations start here
#********************************************************#

echo "Starting initial level validations..." >> ${LOG_FILE}

# Check to make sure all the 5 mandatory options/arguments
# are passed in the command-line
if [[ $# -lt 5 ]]; then
   echo "Incorrect number of arguments passed in the command-line."
   echo "USAGE : $USAGE"
   echo "Incorrect number of arguments passed in the command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   exit 1;
fi

# Read the option argument values from the commandline.
while getopts :f:u:p:d:l:p:m:r: optVal
do      case "$optVal" in
        f)      remoteFtpServer="$OPTARG";;
        d)      remoteFtpDirectory="$OPTARG";;
        p)      ftpPassword="$OPTARG";;
        u)      ftpUserNm="$OPTARG";;
        l)      localDirectory="$OPTARG";;
        m)      fileMatchPattern="$OPTARG";;
        r)      retentionPeriod="$OPTARG";;
        esac
done


#Verify if the remote ftp server name was passed in the command-line
if [[ -z ${remoteFtpServer} ]];
then
   echo "Remote FTP server name was not provided in command-line."
   echo "USAGE : $USAGE"
   echo "Exiting process"
   echo "Remote FTP server name was not provided in command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   exit 1;
fi

BASE_DIR=${TEMP_DIR}/${remoteFtpServer}
if [[ ! -d ${BASE_DIR} ]]; then
   mkdir -p ${BASE_DIR}
fi

export LOG_FILE=${BASE_DIR}/${BASENAME}.`date '+%m%d%Y'`.log.$$
cat ${OLD_LOG_FILE} >> ${LOG_FILE}
rm -f ${OLD_LOG_FILE}

#Verify if the ftp user name was passed in the command-line
if [[ -z ${ftpUserNm} ]];
then
   echo "FTP user name was not provided in command-line."
   echo "USAGE : $USAGE"
   echo "Exiting process"
   echo "FTP user name was not provided in command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   exit 1;
fi

#Verify if the ftp password was passed in the command-line
if [[ -z ${ftpPassword} ]];
then
   echo "FTP password was not provided in command-line."
   echo "USAGE : $USAGE"
   echo "Exiting process"
   echo "FTP password was not provided in command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   exit 1;
fi

#Verify if the database name was passed in the command-line
if [[ -z ${remoteFtpDirectory} ]];
then
   echo "FTP server directory name was not provided in command-line."
   echo "Default FTP server directory name to /"
   remoteFtpDirectory="/"
fi

#Verify if the ftp password was passed in the command-line
if [[ -z ${localDirectory} ]];
then
   echo "Local directory was not provided in command-line."
   echo "USAGE : $USAGE"
   echo "Exiting process"
   echo "Local directory was not provided in command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   exit 1;
fi

#Verify if the ftp password was passed in the command-line
if [[ -z ${fileMatchPattern} ]];
then
   echo "File matching pattern was not provided in command-line."
   echo "USAGE : $USAGE"
   echo "Exiting process"
   echo "File matching pattern was not provided in command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   exit 1;
fi

#Verify if the ftp password was passed in the command-line
if [[ -z ${retentionPeriod} || ${retentionPeriod} -le 0 ]];
then
   echo "Valid Retention period was not provided in command-line."
   echo "USAGE : $USAGE"
   echo "Exiting process"
   echo "Valid Retention period was not provided in command-line." >> ${LOG_FILE}
   echo "USAGE : $USAGE" >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   exit 1;
fi

echo "Finished initial level validations..." >> ${LOG_FILE}

echo "remoteFtpServer		:       ${remoteFtpServer}" >> ${LOG_FILE}
echo "ftpUserNm		:       ${ftpUserNm}" >> ${LOG_FILE}
#echo "ftpPassword		:       ${ftpPassword}" >> ${LOG_FILE}
echo "remoteFtpDirectory	:       ${remoteFtpDirectory}" >> ${LOG_FILE}
echo "localDirectory		:       ${localDirectory}" >> ${LOG_FILE}
echo "fileMatchPattern	:       ${fileMatchPattern}" >> ${LOG_FILE}
echo "retentionPeriod		:       ${retentionPeriod}" >> ${LOG_FILE}

#********************************************************#
# Initial Level Validations end here
#********************************************************#

oldFileNameList=${BASENAME}.`date '+%m%d%Y'`.lst.$$

# Create a list of all the sql
echo "find ${localDirectory} -name '${fileMatchPattern}' -type f -mtime +${retentionPeriod} -mtime -${cutOffPeriod} | grep -v '${localDirectory}/.*/' > ${BASE_DIR}/${oldFileNameList}.tmp" >> ${LOG_FILE}
eval "find ${localDirectory} -name '${fileMatchPattern}' -type f -mtime +${retentionPeriod} -mtime -${cutOffPeriod} | grep -v '${localDirectory}/.*/' > ${BASE_DIR}/${oldFileNameList}.tmp"
exitStatus=$?

if (( ${exitStatus} != 0 )); then
   echo "No files to be removed...." >> ${LOG_FILE}
   echo "Exiting process" >> ${LOG_FILE}
   rm -f ${BASE_DIR}/${oldFileNameList}.tmp 
   exit 0;
fi

# Create a new list file having only the filename and stripping the absolute path 
# based on the temporary list file created in the step above
>${oldFileNameList}

while read myAbsFileName
do
  basename ${myAbsFileName} >> ${BASE_DIR}/${oldFileNameList}
done < ${BASE_DIR}/${oldFileNameList}.tmp

cd ${BASE_DIR}

# Call the perl script to connect to the remote ftp server and delete the old files
# from the ftp server based on the list of files created above.
#echo "remoteFtpServerCleanUp.pl ${PROXY_SERVER} ${remoteFtpServer} ${ftpUserNm} ${ftpPassword} ${remoteFtpDirectory} ${oldFileNameList}" >> ${LOG_FILE}

${UTIL_FILE_DIR}/remoteFtpServerCleanUp.pl ${PROXY_SERVER} ${remoteFtpServer} ${ftpUserNm} ${ftpPassword} ${remoteFtpDirectory} ${oldFileNameList} >> ${LOG_FILE}
exitStatus=$?


case ${exitStatus} in
 1)
    echo "************************************************" >> ${LOG_FILE}
    echo "Cannot connect to ${remoteFtpServer}..." >> ${LOG_FILE}
    echo "Please check ${remoteFtpServer} availability!" >> ${LOG_FILE}
    exit 1;;
 2)
    echo "************************************************" >> ${LOG_FILE}
    echo "Cannot login to ${remoteFtpServer}..." >> ${LOG_FILE}
    echo "Please check the ftp user and password info." >> ${LOG_FILE}
    exit 1;;
 3)
    echo "************************************************" >> ${LOG_FILE}
    echo "Cannot access ${remoteFtpDirectory} on server ${remoteFtpServer}..." >> ${LOG_FILE}
    echo "Please check the ftp location name and permissions." >> ${LOG_FILE}
    exit 1;;
 *)
    echo "Old files successfully deleted from ${remoteFtpServer}." >> ${LOG_FILE};;
esac

rm -f ${BASE_DIR}/${oldFileNameList}.tmp ${BASE_DIR}/${oldFileNameList}

echo "${PGM_NAME} finished processing..."
echo "${PGM_NAME} finished processing..." >> ${LOG_FILE}

#********************************************************#
# Main program ends here
#********************************************************#
exit 0


remoteFtpServerCleanUp.pl:
Code:
#!/usr/local/bin/perl
use File::Basename;
use Net::FTP;
my $verbose = 0;
my $proxy = @ARGV[0];
my $host  = @ARGV[1];
my $ftpuser = @ARGV[2];
my $ftppass = @ARGV[3];
my $remoteDir = @ARGV[4];
my $lstOfFiletoDelete = @ARGV[5];
open (MYFILE, $lstOfFiletoDelete);
my $ftp=Net::FTP->new($host,
           Debug => 0,
           Passive => 0,
           Hash => ($verbose ? \*STDOUT : undef),
           FirewallType => 1,
           Firewall => $proxy,
   ) or exit(1);
   $ftp->login($ftpuser,$ftppass) or exit(2);
chomp($remoteDir);
if ( $remoteDir ne "/" )
{
   $ftp->cwd($remoteDir) or exit(3);
   print $remoteDir;
}

print "Starting deletion of files from server $host ... \n";
open (MYFILE, $lstOfFiletoDelete);
while (<MYFILE>) 
{
   chomp;
   print "Deleting file: $_\n";
   $ftp->delete($_) or print "$_ not deleted or not found... \n";
   
}
close (MYFILE); 

$ftp->quit;
exit(0);


Last edited by rbatte1; 06-15-2017 at 09:36 AM.. Reason: LIST=1 tags
# 16  
Old 06-24-2009
As per your requirement . Probably you can try the below code

Code:
{
echo "user userid Passwd"  ( to provide the crendentials to login)
echo "cd locationofdirectorywherethe file recides"
echo "rm filname"
echo "bye"
} > pwd/filename  (where pwd is present working directory
{
ftp -n "ipaddress"  ( ip address  of which you want to login) 
sleep 3
} < pwd/filename


Last edited by rbatte1; 06-15-2017 at 09:36 AM.. Reason: Added CODE tags
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Deleting multiple files off an ftp server once they have been downloaded

Hello, I have a server that I have to ftp files off and they all start SGRD and are followed by 6 numbers. SGRD000001 SGRD000002 SGRD000003 The script I have will run every 10 mins to pick up files as new ones will be coming in all the time and what I want to do is delete the files I have... (7 Replies)
Discussion started by: sph90457
7 Replies

2. Shell Programming and Scripting

Deleting local server file from automated FTP script

Hi, I want to delete a file on the local server, while connected to remote server through FTP. I am using the below code for this $FTP_CMD -v -n $HOST <<*! >> $LOGFILE 2>&1 user $USER $PASSWORD cd $DIR ... (11 Replies)
Discussion started by: jhilmil
11 Replies

3. Solaris

FTP-ing files from Windows server to UNIX server

I need to transfer files from a Windows server to the Unix server and have to run some shell script on it to get the required output. Is it possible to transfer files from Windows server to unix server through any shell script? If so can you please help me with the details. Thanks in... (8 Replies)
Discussion started by: ssk250
8 Replies

4. Shell Programming and Scripting

FTP multiple files from one server to one server

Hi, I'm new to shell script..I have one requriement like - In one server have more than one files,I want to ftp those files to some otehr server.. Ex : test1.pdf test2.pdf Please suggest me how to do (3 Replies)
Discussion started by: venkaswa
3 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. UNIX for Advanced & Expert Users

why file automatically deleting in ftp server

Iam putting file in ftp server. iam doing ftp to transfer a file to ftp server but after sometime(10 sec) the file is automatically deleting in the ftp. Can i know why this happens. When my friend ftp the file to the same server , the file is not deleting aftersometime... it is there. Can... (1 Reply)
Discussion started by: nani1984
1 Replies

7. Shell Programming and Scripting

script for to take files from FTP server to UNIX server and Unzipped that files

script for to take files from FTP server to UNIX server and Unzipped that files (1 Reply)
Discussion started by: sunilamarnadh
1 Replies

8. Filesystems, Disks and Memory

Not able to FTP the files to a FTP server

Hi , We are facing a weird problem in our project. we need to send some xml & audio files to a remote FTP server from a Linux box, we are doing this in Perl script using Net::FTP->. Issue here is.. when FTPed the files using Perl scripts, only empty files ( 0 byte) are getting created on the... (2 Replies)
Discussion started by: kishorepotta
2 Replies

9. Shell Programming and Scripting

FTP multiple files from remote server to local server

Hi, I am facing a weired problem in my FTP script. I want to transfer multiple files from remote server to local server everyday, using mget * in my script. I also, want to send an email for successful or failed FTP. My script works for file transfer, but it don't send any mail. There is... (2 Replies)
Discussion started by: berlin_germany
2 Replies

10. Shell Programming and Scripting

deleting multiple files through ftp

Hi, I have a situation where I need to delete multiple files from a folder once I connect to FTP server. I am using ftp script to get the files, number of files always vary from 1 to 100. once I get the files I need to delete all the files downloaded I am making a list of all the files... (4 Replies)
Discussion started by: mgirinath
4 Replies
Login or Register to Ask a Question