#!/bin/bash # --------------------- Settings # SOURCE_HOST=cloud21.cvtt.vpn # SOURCE_ROOT_DIR=/opt/store/cvtt/md_archive/crypto # DB_SOURCE=cloud28 # DB_SOURCE_BACKUP=cloud29 # OUTPUT_DIR=/tmp # DATE=20250516 # RSYNC_TARGETS="cvtt@hs01.cvtt.vpn:/works/cvtt/md_archive/crypto/sim/ cvtt@cloud21.cvtt.vpn:/opt/store/cvtt/md_archive/crypto/sim/" # --------------------- Settings if [ -z ${DATE} ] ; then DATE=$(date -d 'yesterday' +'%Y%m%d') fi if [ -z ${OUTPUT_DIR} ] ; then OUTPUT_DIR=. fi echo "DATE=${DATE} SOURCE_HOST=${SOURCE_HOST}" mkdir -p ${OUTPUT_DIR} year=$(date -d ${DATE} +"%Y") month=$(date -d ${DATE} +"%m") # read the file prepare_crypto_simdata.sh # implement the following logic. # 1. check SourceFilePath on on SURCE_HOST for both DB_SOURCE and DB_SOURCE_BACKUP # 2. if the file does not exist on either, exit like in line 47 # 3. if file exist on only one of them, use ti to rsync # 4. if the file exists on both, use the one that is larger in size SourceFile="${DATE}.mktdata.db.gz" SelectedSourceHost="" SelectedSourceFilePath="" SelectedSourceSize=0 for db_source_host in ${DB_SOURCE} ${DB_SOURCE_BACKUP}; do SourceDir="${SOURCE_ROOT_DIR}/${db_source_host}/${year}/${month}" echo "SourceDir=${SourceDir}" CandidatePath="${SourceDir}/${SourceFile}" remote_stat_cmd="if [ -f '${CandidatePath}' ]; then stat -c %s '${CandidatePath}'; else exit 1; fi" CandidateSize=$(ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null ${SOURCE_HOST} "${remote_stat_cmd}" 2>/dev/null) if [ $? -eq 0 ] && [ -n "${CandidateSize}" ]; then echo "Found ${SOURCE_HOST}:${CandidatePath} (${CandidateSize} bytes)" if [ -z "${SelectedSourceHost}" ] || [ "${CandidateSize}" -gt "${SelectedSourceSize}" ]; then SelectedSourceHost=${db_source_host} SelectedSourceFilePath=${CandidatePath} SelectedSourceSize=${CandidateSize} fi fi done if [ -z "${SelectedSourceHost}" ]; then echo "File ${SOURCE_HOST}:${SourceFile} NOT FOUND" exit fi echo "Using source ${SelectedSourceHost} with ${SelectedSourceFilePath} (${SelectedSourceSize} bytes)" Cmd="/usr/bin/rsync -ahv" Cmd+=" --mkpath" Cmd+=" -e 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null'" Cmd+=" ${SOURCE_HOST}:${SelectedSourceFilePath}" Cmd+=" $OUTPUT_DIR/" echo ${Cmd} eval ${Cmd} if [ ! -f ${OUTPUT_DIR}/${SourceFile} ] ; then echo "File ${OUTPUT_DIR}/${SourceFile} NOT FOUND" exit fi Cmd="(cd ${OUTPUT_DIR} && gunzip -f *.db.gz)" echo ${Cmd} eval ${Cmd} SourceDbFile="${OUTPUT_DIR}/${DATE}.mktdata.db" ResultDbFile="${OUTPUT_DIR}/${DATE}.crypto_sim_md.db" echo "SourceDbFile=${SourceDbFile}" echo "Creating Result Database File ${ResultDbFile}" cleanup() { rm ${SourceDbFile} } trap cleanup EXIT echo "Creating table md_trades ..." sqlite3 ${ResultDbFile} <