#!/usr/bin/ksh # # gbo_ua_canmag_mirror.ksh - Using rsync, mirror the # Ground Magnetometer (GMAG) files that UAlberta # collects from the CARISMA and NRCan magnetomter # chains. # # Usage: gbo_ua_canmag_mirror.ksh [year] [month] [day] [duration] # # Arguments: # $1 = year # $2 = month # $3 = day # $4 = duration # # # Creation Date: # # 22 march 2006 TimQuinn # # ## Set the environment variables # if [[ -z $THMSOC ]] then THMSOC=/disks/socware/thmsoc_dp_current export THMSOC fi #. ${THMSOC}/src/config/soc_it_to_me.config # . ${THMSOC}/src/config/gbo_ua_canmag_mirror.config . /home/thmsoc/gaia2_test/soc_it_to_me.config . /home/thmsoc/gaia2_test/gbo_ua_canmag_mirror.config ## Take input arguments and build mirror directories # year=$1 month=$2 day=$3 args_dir_path=${year}/${month}/${day}/ if [[ -z $month && -z $day ]] then args_dir_path=${args_dir_path%%//} elif [[ -z $day ]] then args_dir_path=${args_dir_path%/} fi mirror_start_dir="$UCB_MIRROR_HOME/${args_dir_path}" mkdir -p $mirror_start_dir cd $mirror_start_dir ## Run rsync # echo "$$:" >> $LOGFILE echo "$$:----------- Starting gbo_ua_canmag_mirror at $(date) ---------" >> $LOGFILE ## Check for lock file # start_time=$(date -u '+%Y-%m-%d %T') filenum=0 RSYNC_LOGFILE=${RSLOGFILE}_pid$$_${filenum} if [[ -n $5 ]] then count=$5 while (( $count > 0 )) do echo "Starting rsync at directory level ${args_dir_path}" >> $LOGFILE echo "Starting rsync at directory level ${args_dir_path}" >> $RSYNC_LOGFILE # DEBUG echo "Remote server name ${REMOTE_SERVER_NAME}" >> $RSYNC_LOGFILE # Run rsync /usr/local/bin/rsync $RSYNC_OPTIONS ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $RSYNC_LOGFILE echo "RSYNC_STATUS = $?" >> $RSYNC_LOGFILE # Now increment date dateseconds=$(date --date "${year}-${month}-${day}" +%s) nextseconds=$(($dateseconds+60*60*24)) nextday=$(date --date "1970-01-01 ${nextseconds} sec GMT" +%Y/%m/%d) #deltadate doesn't work anymore # nextday=$(/usr/bin/ssh juneau /disks/juneau/home/teq/ws/src/fastlib/deltaDate \ # ${month}/${day}/${year} 1) month=$(echo $nextday |awk -F/ '{ printf("%02s",$2) }') day=$(echo $nextday |awk -F/ '{ printf("%02s",$3) }') year=$(echo $nextday |awk -F/ '{ printf("%s",$1) }') args_dir_path=$(echo $nextday |awk -F/ '{ printf("%s/%02s/%02s/",$1,$2,$3) }') args_dir_path=${school}/${args_dir_path} mirror_start_dir="$UCB_MIRROR_HOME/${args_dir_path}" mkdir -p $mirror_start_dir d $mirror_start_dir count=$(( $count - 1 )) filenum=$(( $filenum + 1 )) RSYNC_LOGFILE=${RSLOGFILE}_pid$$_${filenum} done else echo "Starting rsync at directory level ${args_dir_path}" >> $LOGFILE echo "Starting rsync at directory level ${args_dir_path}" >> $RSYNC_LOGFILE which rsync >> $LOGFILE # Run rsync /usr/local/bin/rsync $RSYNC_OPTIONS ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $RSYNC_LOGFILE rsynccode=$? echo "RSYNC_STATUS = $rsynccode" >> $RSYNC_LOGFILE if (( $rsynccode != 0 )) then echo "WARNING RSYNC PROBLEM: Return code = $rsynccode" >> $LOGFILE ##Disabled on 25Sep09 - too many messages #printf "%s\n" "script: $0" \ #"Warning: UAlberta RSYNC Problem:code $rsynccode: path ${args_dir_path}" | #mailx -s "UAlberta RSYNC Problem" $SOC_EMAIL fi fi end_time=$(date -u '+%Y-%m-%d %T') ## Process log files created by rsync run. ## Check rsync results by seeing if any files were ## recovered or updated # logfilelist=$(ls -1 ${RSLOGFILE}_pid$$_*) for logfile in $logfilelist do # Search logfile for new or updated image files # and process into database if found egrep '(gz)' $logfile > /tmp/GBO_CHECK$$ ##while [[ -s /tmp/GBO_CHECK$$ ]] while read line do ##line=$(head -1 /tmp/GBO_CHECK$$) processCode=$(echo $line |awk -F\| '{print substr($1,3,8)}') path=$(echo $line |awk -F\| '{print $2}') processTime=$(echo $line |awk -F\| '{print $3}' | sed 's/\//-/g') fileSize=$(echo $line |awk -F\| '{print $4}'| sed 's/"//g') fileName=${path##*/} gboName=$(echo $fileName |awk -F. '{print substr($1,9,4)}') year=$(echo $fileName | awk -F. '{print substr($1,1,4)}') month=$(echo $fileName | awk -F. '{print substr($1,5,2)}') day=$(echo $fileName | awk -F_ '{print substr($1,7,2)}') dataTime="${year}-${month}-${day} 00:00:00" mysql_table_name=gbo_${gboName}_canmagfiles echo "$$: Updating MySQL database table $mysql_table_name......." >> $LOGFILE echo "$$: NOT RUNNING gbo_ua_canmag_mirror.php $mysql_table_name $dataTime $fileName \ $processTime $processCode $fileSize" >> $LOGFILE #gbo_ua_canmag_mirror.php $mysql_table_name \ #$dataTime $fileName $processTime $processCode $fileSize echo "$$: ...........update complete. " >> $LOGFILE done < /tmp/GBO_CHECK$$ ## Create file for later use by CDF processing software # for line in $(cat /tmp/GBO_CHECK$$) do if [[ -n $(echo $line |grep gz) ]] then fullfile=$(echo $line | awk -F\| '{print $2}') echo $fullfile >> $GMAGMIRROR/tmp_CAN_ASCfiles$$ fi done mv $GMAGMIRROR/tmp_CAN_ASCfiles$$ $GMAGMIRROR/CAN_ASCfiles$$ rm -f /tmp/GBO_CHECK$$ ## Process rsync statistics into database # rsync_status=$(grep RSYNC_STATUS $logfile|awk '{print $3}') if (( $rsync_status == 0 )) then rsync_dir=$(grep rsync $logfile | awk '{print $6}') num_files=$(grep "Number of files transferred" $logfile | awk '{print $5}') tot_size=$(grep "Total file size" $logfile | awk '{print $4}') xfer_rate=$(grep "bytes/sec" $logfile | awk '{print $7}') #gbo_ua_canmag_mirror_stats.php \ # $rsync_dir $start_time $end_time $num_files $tot_size $xfer_rate else echo "$$:" >> $LOGFILE echo "$$:WARNING: rsync did not finish properly" >> $LOGFILE echo "$$:WARNING: rsync code = $rsync_status" >> $LOGFILE ##Disabled on 25Sep09 - too many messages #printf "Subject: %s\n%s\n" "RSYNC Failure" \ #"WARNING: rsync code = $rsync_status. Check $LOGFILE" | mail $SOC_EMAIL fi done echo "$$:" >> $LOGFILE echo "$$:----------- Ending gbo_ua_canmag_mirror at $(date) ---------" >> $LOGFILE # ## Cleanup and exit # exit 0