Motivation !

코드와 서버의 코드를 동기화하는 작업의 세 번째 글이다.

이전 단계까지는 미리 정의한 디렉토리들을 제외하고 파일들을 전송하는 단계까지 진행이 되었다.

이번에는 파일이 변경되었는지를 체크하고 파일을 옮기는 작업을 해보려고 한다.

 

  1. 효율성
    파일이 변경되어 있던 되어있지 않던 무조건 덮어쓰기를 하고 있는 문제가 있다.

  2. 속도
    하나의 파일마다 연결을 열고 파일을 전송하다 보니 너무 느리다.

 


 

todo !

파일이 변경되었는지는 hash값을 이용해서 판단하려고 한다.

 

source -> target

전송 -> 수신

 

우선 전송하는 디렉토리의 파일 리스트를 얻는 단계에서 ssh를 이용해 수신하는 쪽 파일들의 hash값을 얻어올 생각이다.

그 후 hash값을 비교하고 hash값이 다르면 배열에 넣어 scp 명령어를 실행하려고 한다.

 

  1. 전송 측 파일 hash(source)
    hashCommand filePath

  2. 수신 측 파일 hash(target)
    ssh targetUser@targeIp hashCommand filePath

 

function mac_ssh_hash() {
    hash=`ssh $1@$2 $3 $4 | awk '{ print $4 }'`
    echo $hash
}
function mac_hash() {
    hash=`$1 $2 | awk '{ print $4 }'`
    echo $hash
}
function ubuntu_ssh_hash() {
    hash=`ssh $1@$2 $3 $4 | awk '{ print $1 }'`
    echo $hash
}
function ubuntu_hash() {
    hash=`$1 $2 | awk '{ print $1 }'`
    echo $hash
}

if [[ "$OSTYPE" == "linux-gnu"* ]]; then
    # ubuntu
    sourceIp=$(hostname -i | awk '{print $1}')
    targetIp=192.168.0.0
    user=serverUser
    targetPath='ubuntuPath'
elif [[ "$OSTYPE" == "darwin"* ]]; then
    # mac
    sourceIp=$(ifconfig | grep "inet " | grep -v 127.0.0.1 | cut -d\  -f2)
    targetIp=192.168.0.0
    user=macUser
    targetPath='macPath'
fi

targetFileArray=()
sourceFileArray=()

for filePath in $(find $PWD -maxdepth 60 -type d \( -path $PWD/node_modules -o -path $PWD/dist -o -path $PWD/python_env -o -path $PWD/logs -o -path $PWD/.git \) -prune -o -name '*' -a -type f)
do
    if [[ "$filePath" == "$PWD/node_modules/"* ]]; then
        continue
    elif [[ "$filePath" == "$PWD/python_env/"* ]]; then
        continue
    elif [[ "$filePath" == "$PWD/dist/"* ]]; then
        continue
    elif [[ "$filePath" == "$PWD/.git"* ]]; then
        continue
    fi
    fileName=$(basename ${filePath})
    rPath="${filePath#$PWD}"
    targetFilePath="$targetPath$rPath"
    if [[ "$OSTYPE" == "linux-gnu"* ]]; then
        # ubuntu
        sourceFileHash=$(ubuntu_hash md5 $filePath )
        targetFileHash=$(mac_ssh_hash md5sum $targetFilePath)
    elif [[ "$OSTYPE" == "darwin"* ]]; then
        # mac
        sourceFileHash=$(mac_hash md5 $filePath )
        targetFileHash=$(ubuntu_ssh_hash $user $targetIp md5sum $targetFilePath)
    fi

    if [[ $sourceFileHash == $targetFileHash ]]; then
        echo $fileName same
    else
        targetFileArray[${#targetFileArray[@]}]="$targetFilePath"
        sourceFileArray[${#sourceFileArray[@]}]="$filePath"
        echo $fileName diff
    fi
    
done

targetFileArrayLength="${#targetFileArray[@]}"

cnt=0
for value in "${targetFileArray[@]}"
do
    scp ${sourceFileArray[$cnt]} ${user}@${targetIp}:${targetFileArray[$cnt]}
    echo $(($cnt + 1)), $value
    cnt=$(( $cnt + 1 ))
done

 


 

conclusion !

hash 값을 얻어오는 것과, script에서 함수의 결과 값을 얻어오는 것이 힘들었지만, 일단 잘 동작하는 것을 확인했다.

 

hash값을 비교하면서 옮기는 작업을 추가하면서 속도적인 측면에서 더욱 좋지 않아 졌다.

target의 hash값을 얻어오는데 파일 하나마다 각각 연결을 열고 닫으면서 기존의 실행시간보다 느려졌다.

기존에는 (전체 파일의 개수) 만큼이었다면 이번 수정으로 인해 (전체 파일 개수 + 변경된 파일 개수)로 연결을 생성하는 개수가 늘었다.

 

이 문제를 해결하기 위해서는 한쪽(보내는 쪽)에서만 작동한다고 되는 게 아닌 것 같다.

일단 이 프로젝트는 여기까지 진행하기로 하였고, 해당 기능은 rsync을 사용하기로 했다.

 

https://linux.die.net/man/1/rsync