이미지 동기화 설정
1. 환경
- hmsecft를 이용하여 이미지 동기화를 구현
2. script
sync_files_v3.sh
#!/bin/bash
function get_dir(){
# https://stackoverflow.com/questions/3236871/how-to-return-a-string-value-from-a-bash-function/38997681#38997681
declare -n ret=$1
# https://stackoverflow.com/questions/2437452/how-to-get-the-list-of-files-in-a-directory-in-a-shell-script/71345102#71345102
local dirlist=$(find $2 -mindepth 1 -maxdepth 1 -type d)
ret=$dirlist
}
function get_files(){
declare -n ret=$1
# https://stackoverflow.com/questions/2437452/how-to-get-the-list-of-files-in-a-directory-in-a-shell-script/71345102#71345102
local files=$(find $2 -maxdepth 1 -type f)
ret=$files
}
# @return './<version_directory>'
# './1.1.1'
function check_last_sync_ver(){
declare -n ret=$1
local fnameLastSyncVer=$2
if [ ! -f $fnameLastSyncVer ]
then
# generate a file with version '0'
echo "0" > $fnameLastSyncVer
fi
local lastSyncVer="./$( cat $fnameLastSyncVer )"
ret=$lastSyncVer
}
# This checks the app config value
# @return '' | 'true' | 'false'
#
function check_capture_image(){
declare -n ret=$1
local configUrl=$2
local configs=$( curl -s $configUrl )
local captureImageFlag=$( echo $configs | grep -oP 'image_capture.*?:\s*?(true|false)' | awk -F'[:]' '{print $2}' )
if [[ "$captureImageFlag" == "false" && "$captureImageFlag" == "" ]]
then
ret="false"
return
fi
ret=$captureImageFlag
}
#
# This generates a file with current date
#
function write_enddate_now(){
declare -n ret=$1
local fnameEndDate=$2
local nowYYYYMMDD=$( date +"%Y-%m-%d" )
# generate a file with current date
echo $nowYYYYMMDD > $fnameEndDate
}
#
# This generates a file with '9999-12-31'
#
function write_enddate_infinite(){
declare -n ret=$1
local fnameEndDate=$2
# generate a file with '9999-12-31'
echo "9999-12-31" > $fnameEndDate
}
# --------------------
# main
#
# Assumes that this script would be executed once a day at 23:00
# --------------------
SCRIPT_DIR=$(cd "$(dirname "${BASH_SOURCE[0]}")" &> /dev/null && (pwd -W 2> /dev/null || pwd))
DEBUG=0
if [ $# -ne 3 ]
then
echo "Usage: <porg>.sh [hostname] [absoulte_path] [appkey]"
exit $WRONG_ARGS
fi
host=$1
dstPath=$2
appKey=$3
USER_ID="mceadm"
ftpCmd=hmcft
URL_CONFIG_API="http://localhost:8080/v3/app/$appKey/config"
FNAME_END_DATE="_uh_end_date_$host"
FILENAME_LASTSYNCVER="_uh_sync_last_ver_$host"
# ----------------
# check if the capturing-image flag is turned on
# ----------------
echo "config-api : $URL_CONFIG_API"
check_capture_image result $URL_CONFIG_API
needSync=$result
if [ "$needSync" == "false" ]
then
echo "image_capture flag is off"
write_enddate_now result $FNAME_END_DATE
else
echo "image_capture flag is on"
write_enddate_infinite result $FNAME_END_DATE
fi
captureEndDate=$( cat $FNAME_END_DATE )
nowYYYYMMDD=$( date +"%Y-%m-%d" )
if [[ "$captureEndDate" < "$nowYYYYMMDD" ]]
then
echo "no need to sync, capture-end-date: $captureEndDate"
exit 0
fi
# ----------------
# check the last sync version directory
# ----------------
check_last_sync_ver result $FNAME_LASTSYNCVER
lastSyncVer=$result
echo "last sync version directory: $lastSyncVer"
# --------------------
# check the $dstPath exists on remote target
# --------------------
commands="cd $dstPath"
cd $dstPath
if [ $DEBUG -eq 0 ]
then
echo "$commands" | $ftpCmd ${USER_ID}@$1
if [ $? -ne 0 ]
then
echo "$dstPath does NOT exist"
exit 1
fi
fi
# --------------------
# put all files to $dstPath
# --------------------
get_dir result .
dirlist=$result
# --------------------
# mkdir directories
# --------------------
for dir in $dirlist
do
cmdAdded="mkdir $dir"
printf -v commands "$commands\n$cmdAdded"
done
# --------------------
# put files of directories
# --------------------
echo "wait 20s or more"
for dir in $dirlist
do
if [[ $dir < $lastSyncVer ]]
then
echo "skip : $dir"
continue
else
echo ${dir:2:50} > "$SCRIPT_DIR/FNAME_LASTSYNCVER"
fi
cd $dir
printf -v commands "$commands\ncd $dir"
get_files result .
files=$result
count=0
for file in $files
do
# up to 6000, it is ok
# if [ $count -eq 100 ]
# then
# printf -v commands "Scommands\nbye"
# echo "$commands"
# exit 0
# fi
cmdAdded="put $file"
printf -v commands "$commands\n$cmdAdded"
count=$(( $count + 1 ))
done
cd ..
done
# https://stackoverflow.com/questions/3005963/how-can-i-have-a-newline-in-a-string-in-sh
# without quote, newline does not work
printf -v commands "$commands\nbye"
if [ $DEBUG -eq 1 ]
then
echo "$commands"
else
echo "$commands" | $ftpCmd ${USER_ID}@$1
fi
3. crontab 등록
su - mceadm
crontab -e
corontab
# crontab
30 22 * * * /mceadm/apps/script/sync_files.sh 10.227.171.97 /mceadm/apps/polaris/resources/static/attach/000000000000000000000004 <app_key>
35 22 * * * /mceadm/apps/script/sync_files.sh 10.227.171.97 /mceadm/apps/polaris/resources/static/attach/000000000000000000000003 <app_key>
35 22 * * * echo "run" > /mceadm/apps/script/res97.log
40 22 * * * /mceadm/apps/script/sync_files.sh 10.227.171.98 /mceadm/apps/polaris/resources/static/attach/000000000000000000000004 <app_key>
45 22 * * * /mceadm/apps/script/sync_files.sh 10.227.171.98 /mceadm/apps/polaris/resources/static/attach/000000000000000000000003 <app_key>
45 22 * * * echo "run" > /mceadm/apps/script/res97.log