#!/usr/bin/ksh # # gbo_ualaska_mirror.ksh - Using rsync, mirror the # Ground Magnetometer (GMAG) files that UAlaska # collects from the GIMA ground magnetometer chain. # # Usage: gbo_ualaska_mirror.ksh [station] [year] # # Arguments: # $1 = station # $2 = year # # # Creation Date: # # 13 August 2006 TimQuinn # # ## Set the environment variables # . $HOME/software_dev/config/gbo_ualaska_mirror.config . $HOME/software_dev/config/soc_it_to_me.config ## Test Settings # ## Function Definitions # ## Take input arguments and build mirror directories # station=$1 year=$2 args_dir_path=${station}/${year}/ if [[ -z $station || -z $year ]] then echo Must set station - FtYukon or Gakona echo and year exit 1 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_ualaska_mirror at $(date) ---------" >> $LOGFILE ## Check for lock file # if [[ -a $LOCKFILE ]] ; then echo "$$:" >> $LOGFILE echo "$$: A Mirror Process is currently active " >> $LOGFILE echo "$$: Quittin..................." >> $LOGFILE exit 0 fi echo $$ > $LOCKFILE 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 # Run rsync /usr/local/bin/rsync $RSYNC_OPTIONS_A ${REMOTE_SERVER_NAME}/${args_dir_path} . >> $RSYNC_LOGFILE echo "RSYNC_STATUS = $?" >> $RSYNC_LOGFILE # Now increment date nextday=$(/usr/bin/rsh 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 cd $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_A ${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 printf "%s\n" "script: $0" \ "Warning: UAlaska RSYNC Problem:code $rsynccode: path ${args_dir_path}" | mailx -s "UAlaska 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 grep nc\| $logfile > /tmp/GBO_CHECK$$ while [[ -s /tmp/GBO_CHECK$$ ]] 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##*/} year=$(echo $fileName | awk -F. '{print substr($1,1,4)}') ## The UAlaska file naming convention is non-uniform and ## needs to be evaluated # nameCheck=$(echo $fileName | awk -F. '{print substr($1,5,1)}') if [[ $nameCheck = "F" ]] then gboName=$(echo $fileName |awk -F. '{print substr($1,5,7)}') doy=$(echo $fileName |awk -F. '{print substr($1,12,3)}') hr=$(echo $fileName |awk -F. '{print substr($1,16,2)}') elif [[ $nameCheck = "G" ]] then gboName=$(echo $fileName |awk -F. '{print substr($1,5,6)}') doy=$(echo $fileName |awk -F. '{print substr($1,11,3)}') hr=$(echo $fileName |awk -F. '{print substr($1,15,2)}') else echo Filename $fileName is incorrect rm -f $LOCKFILE exit 1 fi ## Create and fill file with year doy gboName ## to be used for downstream processing into CDF # echo ${year}_${doy}_${gboName} >> $WORKDIR/GIMA_Prep$$ ## Now determine dataTime # date=$(/home/thmsoc/software_dev/ksh/doy2date $year $doy) month=$(echo $date |awk -F/ '{print $2}') day=$(echo $date |awk -F/ '{print $3}') dataTime=$(echo $year $month $day $hr | awk '{printf("%s-%02s-%02s %02s:00:00",$1,$2,$3,$4)}') ## Construct database table name # mysql_table_name=gbo_${gboName}_magfiles ## Now insert file information into database # echo "$$: Updating MySQL database table $mysql_table_name......." >> $LOGFILE echo "$$: gbo_ualaska_mirror.php $mysql_table_name $dataTime $fileName \ $processTime $processCode $fileSize" >> $LOGFILE /home/thmsoc/software_dev/php/gbo_ualaska_mirror.php $mysql_table_name \ $dataTime $fileName $processTime $processCode $fileSize echo "$$: ...........update complete. " >> $LOGFILE ## Move to next line # sed '1d' /tmp/GBO_CHECK$$ > /tmp/gbo_check$$ mv /tmp/gbo_check$$ /tmp/GBO_CHECK$$ done rm -f /tmp/GBO_CHECK$$ ## Edit out duplicates in CDF processing file ## and create file to be used by gima_netcdf2all.ksh ## to convert netCDF to CDF and other formats # if [[ -f $WORKDIR/GIMA_Prep$$ ]] then previous=NaN for line in $(cat $WORKDIR/GIMA_Prep$$) do if [[ $line != $previous ]] then echo $line >> $WORKDIR/tmp_GIMA_NC2ALL$$ previous=${line} fi done mv $WORKDIR/tmp_GIMA_NC2ALL$$ $WORKDIR/GIMA_NC2ALL$$ rm -f $WORKDIR/GIMA_Prep$$ fi ## 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}') /home/thmsoc/software_dev/php/gbo_ualaska_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 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_ualaska_mirror at $(date) ---------" >> $LOGFILE rm -f $LOCKFILE exit 0