Motivation !
코드와 서버의 코드를 동기화하는 작업의 두 번째 글이다.
이전 단계에서는 원하는 기능은 이루었지만, 3개의 문제를 발견했다.
이번 에는 1번 문제를 해결해 보려고 한다.
- 필요 없는 데이터
패키지와 git 정보 같은 데이터들도 같이 넘어가는 문제가 있다. - 효율성
파일이 변경되어 있던 되어있지 않던 무조건 덮어쓰기를 하고 있는 문제가 있다. - 속도
하나의 파일마다 연결을 열고 파일을 전송하다 보니 너무 느리다. 
todo !
우선 특정 경로의 파일들을 제외하고 파일을 옮기기 위해 2가지 방법을 사용해 보았다.
- bash script
스크립트에서 해당 경로를 if 문으로 제외하는 방법이다. - find 명령어 filter
파일들의 목록을 불러오는 find명령어에서 걸러내는 방법이다. 
우선 첫 번째 방법으로 시도해 보았다.
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 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"
    targetFileArray[${#targetFileArray[@]}]="$targetFilePath"
    sourceFileArray[${#sourceFileArray[@]}]="$filePath"
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
두 번째 방법으로 테스트를 시도해 보았다.
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
    fileName=$(basename ${filePath})
    rPath="${filePath#$PWD}"
    targetFilePath="$targetPath$rPath"
    targetFileArray[${#targetFileArray[@]}]="$targetFilePath"
    sourceFileArray[${#sourceFileArray[@]}]="$filePath"
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 !
두 방법에서 불필요한 폴더들을 제외하고 전송하는 데 성공하였다.
여기서 두 방식의 차이점을 발견하였는데 바로 속도이다.
첫 번째 방법의 경우 find 명령어로 현재 디렉토리의 전체 하위 파일들을 순회하면서, if 문으로 예외를 처리하므로 속도가 조금 느렸다.
두 번째 방법의 경우 find 명령어로 불필요한 디렉토리들을 미리 제외하고 순회하기 때문에 초기 실행속도가 더 빠른 것을 확인했다.
두 번째 방법을 사용하도록 하고 다음 문제들을 해결하도록 하려고 한다.
Issue !
- 효율성
파일이 변경되어 있던 되어있지 않던 무조건 덮어쓰기를 하고 있는 문제가 있다. - 속도
하나의 파일마다 연결을 열고 파일을 전송하다 보니 너무 느리다. 
'프로젝트 > 로컬서버코드동기화' 카테고리의 다른 글
| [bash] 로컬의 코드와 서버의 코드를 동기화 해보자. <3> (0) | 2022.08.31 | 
|---|---|
| [bash] 로컬의 코드와 서버의 코드를 동기화 해보자. <1> (0) | 2022.08.25 |