#!/bin/bash # MGET.SH (script by Jaky, 2010): FTP Folder Sync & Verify + Delete | op. logging in /var/log/mget.log hostname=ftp.site.com username=USER1 password=XXXXX SRC_DIR="Downloads" MOUNT_DEV="//192.168.111.222/codespring" MOUNT_POINT="/datastore" DEST_DIR="/datastore/Projects/USER1/Downloads/" LOGFILE="/var/log/mget.log" LSTFILE="/home/jaky/files.lst" set $(date) DATETIME="$2/$3/$6:$4" function mount_partition(){ if [ -d "${DEST_DIR}" ] ; then echo -e "* Mount '${MOUNT_POINT}' exists...Continuing...." >> $LOGFILE else mount $MOUNT_DEV RESULT=$? if [ $RESULT -ne 0 ] ; then #if result = 32 (allready exists) you can let go ... if [ $RESULT -ne 32 ] ; then echo -e "* Aborted actions: mount returned code: ${RESULT} for 'mount ${MOUNT_DEV} ${MOUNT_POINT}' *" >> $LOGFILE exit 1 fi fi echo -e "* Mounting: '${MOUNT_DEV}' -> '${MOUNT_POINT}' ..." >> $LOGFILE fi } function unmount_partition() { umount -l $MOUNT_POINT RESULT=$? if [ $RESULT -ne 0 ] ; then echo -e "* Warning! umount returned code: ${RESULT} for umount -l ${MOUNT_POINT} *" >> $LOGFILE #| mail -s "Warn:unmount" eamiladdr exit 1 fi echo -e "* Lazy Unmounting: '${MOUNT_DEV}' -> '${MOUNT_POINT}' ..." >> $LOGFILE return } echo -e "\nMaking a FILE-LIST (priv, user, size, fname..) from the FTP Source Folder..\n" ftp -n -i <<_EOF_ open $hostname user $username $password bin cd $SRC_DIR ls * $LSTFILE quit _EOF_ if test -f $LSTFILE ; then echo -e "\n* '${0}' connected to remote server '${hostname}' [ $DATETIME ].." >> $LOGFILE fi if test -s $LSTFILE ; then # if LSTFILE size > 0 --> we have at least one file on source echo "* File-list on remote server ( ${hostname}/$SRC_DIR ): *" >> $LOGFILE cat $LSTFILE >> $LOGFILE echo -e "There are files on FTP SRC_DIR --> MOUNT the DEST_DIR & MGET all the files there from SRC_DIR\n" mount_partition ftp -n -i <<_EOF_ open $hostname user $username $password bin cd $SRC_DIR lcd $DEST_DIR mget * quit _EOF_ echo -e "Checking if downloaded files size matches the fsize on the FTP..\n If Equal -> delete them from the FTP server\n" while read SLINE do fsize=`echo $SLINE | awk '{ print $5 }'` fname=`echo $SLINE | awk '{ print $9 }'` if test -f $DEST_DIR/$fname ; then lfsize=`ls -l $DEST_DIR/$fname | awk '{ print $5 }'` echo "local_file_size[${fname}]=${lfsize} bytes" if [ $fsize -eq $lfsize ] ; then ftp -n -i <<_EOF_ open $hostname user $username $password bin cd $SRC_DIR delete $fname quit _EOF_ else # if ${fsize} != ${lfsize} echo -e "* Warning: local_fsize[${fname}]=${lfsize}bytes <> remote_fsize[${fname}]=${fsize} bytes, Download restarted.. *" >> $LOGFILE ftp -n -i <<_EOF_ open $hostname user $username $password bin cd $SRC_DIR lcd $DEST_DIR get $fname quit _EOF_ fi fi echo -e "Remote_ftp_fsize[${fname}]=${fsize} bytes\n" done < $LSTFILE # DONE the While cycle checking set $(date) echo -e "* Files downloaded succesfuly [ $2/$3/$6:$4 ] ! *" >> $LOGFILE unmount_partition else echo -e "* Folder '${hostname}/$SRC_DIR' is empty ! *\n" >> $LOGFILE fi # if test -s files.lst ..