ftp, archive, email and delete from shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting ftp, archive, email and delete from shell script
# 1  
Old 07-10-2009
Java ftp, archive, email and delete from shell script

Hello, I found the forum’s information very helpful and informative. I am relatively new to UNIX programming. I am researching how to write a UNIX shell script to ftp all files from specific host folder to a client server. Also need to archive all transferred files, send a message (email) if any file was not transferred successfully and save (email) a log after the script's execution.
Where can find additional information or script’s examples?
Thank you in advance

Last edited by Lenora2009; 07-21-2009 at 06:28 PM..
# 2  
Old 07-10-2009
hint

man ftp
man sendmail
man bash
# 3  
Old 07-10-2009
First, consider reading Classic Shell Scripting book for learning shell scripting...
# 4  
Old 07-13-2009
How can save the ftp transferred details in log ?

Last edited by Lenora2009; 07-21-2009 at 06:29 PM..
# 5  
Old 07-17-2009
Email sending through a shell script

Hi everyone
I have a shell script to ftp (2 .gz files) and send some emails with attachments.
Expert opinions welcome! ... Any comments or suggestions would be most welcome.

Here is the script:
#!/bin/bash
# ## use "arc-$(date '+%y%m%d'.%H%M%S)" for prod
##################################################################################
DATE=`date +%Y%m%d`
TIME=`date +%H%M%S`
LOGFILE="process$(date '+%y%m%d')"
MESSAGE1="BACKUP: OK"
MESSAGE2="BACKUP: Fail"
MESSAGE3="GZIP: OK"
MESSAGE4="GZIP: Fail"
vFName="arc-$(date '+%y%m%d')"
mFile="/name1/name2/name3/"

##################################################################################
# ## Create a list of files - filename.txt
##################################################################################
echo "List of files in "$mFile":" >> $LOGFILE.log
ls -1 $mFile >> $LOGFILE.log

##################################################################################
# ## TAR - create a collection of files within a single file uncompressed
# ## check the command execution and save a message (BACKUP: Fail / BACKUP: OK)
# ## exit if the TAR command failed
##################################################################################
tar -cvvf "/name1/name2/name4/$vFName.tar" / name1/name2/name3/*.txt

if [ $? -gt 0 ]; then
echo "BACKUP: Fail"
printf "%-8s %-10s %-6s %-80s\n" $vFName.tar $DATE $TIME "$MESSAGE2" >> $LOGFILE.log
mailx -s "BACKUP: Fail" "ipeleva@zenithadmin.com" < "$LOGFILE.log"
exit
else
echo "BACKUP: OK"
echo " " >> $LOGFILE.log
echo "TAR file SIZE DATE TIME MESSAGE" >> $LOGFILE.log
F1SIZE=`ls -l /impact/conv/arc/$vFName.tar|tr -s " "|cut -d" " -f5`
printf "%-8s %-11s %-10s %-6s %-80s\n" $vFName.tar $F1SIZE $DATE $TIME "$MESSAGE1" >> $LOGFILE.log
fi

###################################################################################
# ## GZIP - compresses the file .tar, making it .tar.gz.
# ## -f force to overwrite if there is a file aready
# ## When doing this the original file will no longer exist on the drive.
# ## check the command execution and save a message (GZIP: Fail / GZIP: OK)
# ## exit if the GZIP command failed
###################################################################################
gzip -f " /name1/name2/name4/$vFName".tar

if [ $? -gt 0 ]; then
echo "GZIP: Fail"
printf "%-8s %-10s %-6s %-80s\n" $vFName.tar.gz $DATE $TIME "$MESSAGE4" >> $LOGFILE.log
mailx -s "GZIP: Fail" "ipeleva@zenithadmin.com" < "$LOGFILE.log"
else
echo "ZIP: OK"
echo " " >> $LOGFILE.log
echo "TAR.GZ file SIZE DATE TIME MESSAGE" >> $LOGFILE.log
F2SIZE=`ls -l /impact/conv/arc/$vFName.tar.gz|tr -s " "|cut -d" " -f5`
printf "%-8s %-8s %-10s %-6s %-80s\n" $vFName.tar.gz $F2SIZE $DATE $TIME "$MESSAGE3" >> $LOGFILE.log
fi

FILE1IN="/name1/name2/name4/$vFName.tar.gz"
FILE1OUT="$vFName.tar.gz"
FILEIN1SIZE=`ls -l $FILE1IN|tr -s " "|cut -d" " -f5`

FILE2IN="/name1/name2/name4/$vFName.tar.gz"
FILE2OUT="$vFName.tar.gz"
FILEIN2SIZE=`ls -l $FILE2IN|tr -s " "|cut -d" " -f5`

###################################################################################
# User and host info
###################################################################################
HOST=host.com'
FTPUSER='user'
PASSWD='password'
HOSTDIR='/nameh1/nameh2'

###################################################################################
# Upload to ftp
# ftp.log - all ftp messages
###################################################################################
ftp -v -n $HOST > / name1/name2/name4/ftp.log <<EOF
quote user $FTPUSER
quote pass $PASSWD
cd “/nameh1/nameh2'"
bin
put $FILE1IN $FILE1OUT
put $FILE2IN $FILE2OUT
EOF

###################################################################################
# Check the ftp status and file
# ## wc -l tells the number of lines (accounts)
###################################################################################
ftp_ctr=`cat / name1/name2/name4/ftp.log | grep 226 | wc -l`

if [ "$ftp_ctr" = 2 ]; then
echo "No Error"
echo "List of transferred files:" >> ./transfer.txt
printf "System:Impact\tRemote:$HOST\tRemote Folder:$HOSTDIR\tFile Name:$FILE1OUT\tFile Size:$FILEIN1SIZE\n" >> ./transfer.txt
printf "System:Impact\tRemote:$HOST\tRemote Folder:$HOSTDIR\tFile Name:$FILE2OUT\tFile Size:$FILEIN2SIZE\n" >> ./transfer.txt
FILENAME="transfer.txt"
else
echo "Error has occured"
echo "List of transferred files:" >> ./error.txt
printf "System:Impact\tRemote:$HOST\tRemote Folder:$HOSTDIR\tFile Name:$FILE1OUT\tFile Size:$FILEIN1SIZE\n" >> ./error.txt
printf "System:Impact\tRemote:$HOST\tRemote Folder:$HOSTDIR\tFile Name:$FILE2OUT\tFile Size:$FILEIN2SIZE\n" >> ./error.txt
FILENAME="error.txt"
fi

mailx -s "FTP log" "name@somewhere.com" < "$LOGFILE.log"
email_dest=$(grep $FILENAME filelist.txt|cut -d ";" -f 2Smilie
mailx -s "FTP " "$email_dest" < "$FILENAME"

Last edited by Lenora2009; 07-21-2009 at 06:24 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to archive logs and sftp to another archive server

Requirement: Under fuse application we have placeholders called containers; Every container has their logs under: <container1>/data/log/fuse.log <container1>/data/log/fuse.log.1 <container1>/data/log/fuse.log.XX <container2>/data/log/fuse.log... (6 Replies)
Discussion started by: Arjun Goswami
6 Replies

2. Shell Programming and Scripting

Need help in FTP script to archive

Hi Guys, I am unable to archive the FTP files from FTP source path to FTP archive path.Currently i am running this script from the Local server. Can someone help me here. Code: #!/bin/ksh set -x HOST='xxxx' USER='xxxx' PASSWD='xxxx' PORTNO='9999' FILE_NAME=$1 FTP_SRC_PATH=$2... (2 Replies)
Discussion started by: spidy
2 Replies

3. Shell Programming and Scripting

Shell script to call Oracle archive backup script when file system reaches threshold value

Hello All, I need immediate help in creating shell script to call archivebkup.ksh script when archive file system capacity reaches threshold value or 60% Need to identify the unique file system that reaches threshold value. ex: capacity ... (4 Replies)
Discussion started by: sasikanthdba
4 Replies

4. Shell Programming and Scripting

Script to FTP,clean up and email

Hi i need a unix script to do the following tasks. My folder structure is /home/MSTR/test and will have the following folder within it Cache Lookup Source Target 1. On the Source & Target folder i have to take a copy of files older than 5 days and move(FTP) it into local machines C:\Backup... (1 Reply)
Discussion started by: Codesearcher
1 Replies

5. Shell Programming and Scripting

how to delete 3 days old files using ftp in a shell script (Solaris OS)

I need help in writng a script to delete last three days files from a remote server using only FTP / SFTP. I was using find command which is not working and i cant use ssh Immediate response is highly appreciated . Thank in advance! ---------- Post updated 12-05-10 at 09:16 PM ----------... (5 Replies)
Discussion started by: Olivia
5 Replies

6. Shell Programming and Scripting

Shell Script for moving 3 days old file to Archive Folder

Hi Experts, I have a "Source" folder which may contain some files. I need a shell script which should move all files which are older than 3 days to "Archive" folder. Thanks in Advance... (4 Replies)
Discussion started by: phani333
4 Replies

7. Shell Programming and Scripting

Archive shell script

Hi, I'm new to Unix shell scripting and my first task is proving to be tricky. Please help where you can with pointers, tips, or examples of script. I'm self learning from the internet. I need to archive off files to another write protected directory according to the file names. ex.... (0 Replies)
Discussion started by: gorses
0 Replies

8. Shell Programming and Scripting

shell script for primary and standby DB archive log check

Hi All, OS:AIX 5.3 64 bits I would like the below script to send alert mail with the message - "Standby logs falling behind Primary" to xyz@yahoo.com Script ===== #!/usr/bin/ksh #----------------------------------------------------------------------------- # Use SQL*Plus to query... (1 Reply)
Discussion started by: a1_win
1 Replies

9. Shell Programming and Scripting

shell script for archive purge

I am writing a shell script for Archive Purge for the table having rows < 1 year. The shell script has to extract the rows from the table and write those extracted rows to a text file. Then from the text file, each rows will be read and deleted by means of delete query one by one. The fields will... (5 Replies)
Discussion started by: regnumber
5 Replies

10. Shell Programming and Scripting

Generic Shell Script to Archive a file

Would appreciate if any one can paste a generic schell script to archive a file with date stamp by passing the file with fullpath as parameter For Eg. /apps/scripts/Archive_File.sh /data_home/project_home/file.txt this should place the file in the following directory ... (8 Replies)
Discussion started by: mak1600
8 Replies
Login or Register to Ask a Question