#!/bin/ksh #******************************************************************************* # KSH PROGRAM #******************************************************************************* # # PROGRAM ID : syncer.ksh # #D DESCRIPTION : Syncronize two folders (textfiles) # # INPUT PARAMETERS : none # # OUTPUT PARAMETERS : none # # # INPUT FILES : none # # OUTPUT FILES : none # # # SPECIAL LOGIC NOTES : none # #******************************************************************************* # MODIFICATION LOG # # # DATE SE # DESCRIPTION # --------------------------------------------------------------------------- # Fri Sep 9 16:23:06 CEST 2016 (Hajo) # initial version # --------------------------------------------------------------------------- # #******************************************************************************* #******************************************************************************* # INITIALIZATION #******************************************************************************* s_parmcount=$# s_myname=`basename $0` s_mynamebase=$(echo ${s_myname} | sed -e's/\.ksh//') s_parmcount_expected=0 s_dryrun=n if [[ $s_parmcount -ne $s_parmcount_expected ]] then print -u2 "${s_myname}: USAGE: ${s_myname}" exit 1 else s_dir_master=/app/istp/dump/syncmaster s_dir_local=/home/oracle/synclocal mkdir -p ${s_dir_master}/LOG/ s_logfile=${s_dir_master}/LOG/${s_mynamebase}_`date +'%Y%m%d%H%M%S'`.log fi #******************************************************************************* # ALIASSES #******************************************************************************* alias awk='/bin/awk' alias cp='/bin/cp' alias grep='/bin/grep' alias ll='/bin/ls -alv' alias ls='/bin/ls' alias mkdir='/bin/mkdir' alias sed='/bin/sed' alias sort='/bin/sort' alias stat='/usr/bin/stat' #******************************************************************************* # FUNCTIONS #******************************************************************************* #---------------------------------------- fun_error () { #---------------------------------------- echo "${s_myname}: ERROR in module <$*>!" exit 2 } # end fun_error #---------------------------------------- fun_action () { #---------------------------------------- s_module=fun_action s_action_cmd=$* if [ "${s_dryrun}" = "y" ] then echo ${s_action_cmd} else echo ${s_action_cmd} >> ${s_logfile} $(${s_action_cmd}) fi } # end fun_action #---------------------------------------- fun_no_action () { #---------------------------------------- s_na="" } # end fun_no_action #---------------------------------------- fun_backup () { #---------------------------------------- s_module=fun_backup s_dir=$1 s_file=$2 s_timestamp=$3 mkdir -p ${s_dir}/ARCHIVE s_action_cmd="cp -p ${s_dir}/${s_file} ${s_dir}/ARCHIVE/${s_file}.${s_timestamp}" fun_action ${s_action_cmd} } # end fun_backup #---------------------------------------- fun_copy () { #---------------------------------------- s_module=fun_copy s_file=$1 s_action=$2 if [ "${s_action}" = "m2l" ] then s_action_cmd="cp -p ${s_dir_master}/${s_file} ${s_dir_local}/${s_file}" else s_action_cmd="cp -p ${s_dir_local}/${s_file} ${s_dir_master}/${s_file}" fi fun_action ${s_action_cmd} } # end fun_copy #---------------------------------------- fun_get_checksum () { #---------------------------------------- s_module=fun_get_checksum s_file2chk=$1 # s_file_size=$(wc -c ${s_file2chk} | awk '{print $1}') # s_file_lines=$(wc -l ${s_file2chk} | awk '{print $1}') # s_file_words=$(wc -w ${s_file2chk} | awk '{print $1}') i_file_timestamp=$(stat ${s_file2chk} | grep '^Modify' | awk '{print $2, $3}' | sed -e's/\-/ /g' -e's/\:/ /g' -e's/\./ /g' | awk '{print $1 $2 $3 $4 $5 $6}') i_checksum=${i_file_timestamp} } # end fun_get_checksum #---------------------------------------- fun_overwrite () { #---------------------------------------- s_module=fun_overwrite s_file=$1 s_file_master=${s_dir_master}/${s_file} s_file_local=${s_dir_local}/${s_file} s_is_equal=$(sdiff -s ${s_file_master} ${s_file_local} 1>/dev/null 2>&1;echo $?) if [[ ${s_is_equal} -eq 0 ]] then s_action_cmd="fun_no_action" fun_action ${s_action_cmd} else fun_get_checksum ${s_file_master} i_chksum_master=${i_checksum} fun_get_checksum ${s_file_local} i_chksum_local=${i_checksum} if [[ ${i_chksum_master} -gt ${i_chksum_local} ]] then fun_backup ${s_dir_local} ${s_file} ${i_chksum_local} fun_copy ${s_file} m2l else fun_backup ${s_dir_master} ${s_file} ${i_chksum_master} fun_copy ${s_file} l2m fi fi } # end fun_overwrite #---------------------------------------- fun_sync () { #---------------------------------------- s_module=fun_sync s_check_file=$1 # echo ${s_check_file} i_master_exists=$([ -f ${s_dir_master}/${s_check_file} ];echo $?) i_local_exists=$([ -f ${s_dir_local}/${s_check_file} ];echo $?) case ${i_master_exists}${i_local_exists} in 00) fun_overwrite ${s_check_file};; 01) fun_copy ${s_check_file} m2l;; 10) fun_copy ${s_check_file} l2m;; *) fun_error ${s_module};; esac } # end fun_sync #******************************************************************************* # MAINLINE #******************************************************************************* typeset -i i_cnt i_cnt=0 #---------- # gather all files on master side into one variable #---------- ll ${s_dir_master} | grep -e'^-' | awk '{print $9}' | while read s_file_string do let i_cnt=$i_cnt+1 s_all_files="${s_all_files};${s_file_string}" done #---------- # ...and complete it whith the files on local side #---------- ll ${s_dir_local} | grep -e'^-' | awk '{print $9}' | while read s_file_string do let i_cnt=$i_cnt+1 s_all_files="${s_all_files};${s_file_string}" done #---------- # add all gathered file names distincted and in sorted order to an array #---------- i_cnt=0 echo ${s_all_files} | tr ';' '\012' | grep -v '^$' | sort -u | while read s_file_string do let i_cnt=$i_cnt+1 a_all_files[$i_cnt]="${s_file_string}" done #---------- # run thru file name array and syncronize #---------- typeset -i i_cnt i_cnt=0 while [[ ${i_cnt} -lt ${#a_all_files[*]} ]] do let i_cnt=$i_cnt+1 fun_sync ${a_all_files[$i_cnt]} done #******************************************************************************* # FINE #******************************************************************************* exit