๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Mac

[Mac] Shell Script๋กœ ์›๊ฒฉ์„œ๋ฒ„ ์ ‘์† ๋ฐ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

๐Ÿ–‹ ์ด ํฌ์ŠคํŒ…์—์„œ๋Š”

1. ์‹คํ–‰ํŒŒ์ผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

2. ssh, scp๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํŒŒ์ผ ์—…๋ฐ์ดํŠธ

์œ„์˜ 2๊ฐ€์ง€ ๋‚ด์šฉ์„ ๋ฐฐ์›Œ๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 


< ๋ณธ๋ฌธ ์š”์•ฝ >

1. ์‹คํ–‰ํŒŒ์ผ ๋งŒ๋“ค๊ธฐ

cd ~/.ssh
vi config

 

 Host TEST 
     HostName 192.192.192.191
     User ubuntu
     IdentityFile ~/.ssh/{pemํ‚ค ์žˆ๋Š” ๊ฒฝ๋กœ}

 

touch test.sh
chmod +x test.sh 

 

2. SSH, SCP๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํŒŒ์ผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

# test.sh

# ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ๋กœ ์ ‘๊ทผํ•ด์„œ && ๋นŒ๋“œ
cd {PATH} && npm run build || yarn build

# ์›๊ฒฉ ์„œ๋ฒ„์— ์ ‘์†ํ•˜๊ธฐ
# 1. pemํ‚ค๋ฅผ ํ™œ์šฉํ•˜๊ธฐ
ssh TEST rm -rf ${REMOTE_PATH}/* 
&& scp -r ${LOCAL_PATH}/build/*
ubuntu@TEST:${REMOTE_PATH}

# 2. sshpass๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ
sshpass -p "password" ssh TEST rm -rf ${REMOTE_PATH}/* 
&& sshpass -p "password"  scp -r ${LOCAL_PATH}/build/*
ubuntu@TEST:${REMOTE_PATH}

 

 


< ์ž์„ธํžˆ >

Shell Script๋กœ ์›๊ฒฉ ์„œ๋ฒ„ ์ ‘์† ๋ฐ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

1. ์‹คํ–‰ํŒŒ์ผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•

ํŒŒ์ผ ์ƒ์„ฑ ๋ฐ ๊ถŒํ•œ ๋ถ€์—ฌ

touch test.sh
chmod +x test.sh

 

๋งŒ๋“  ์Šคํฌ๋ฆฝํŠธ๋ฅผ ์‹คํ–‰์‹œํ‚ค๊ธฐ ์œ„ํ•ด์„œ ํŒŒ์ผ์— ์‹คํ–‰ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

chmod +x test.sh ๋ฅผ ํ†ตํ•ด์„œ ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋Š” ๋ฐฉ๋ฒ•์€ ์—ฌ๋Ÿฌ ๊ฐ€์ง€๊ฐ€ ์žˆ๋Š”๋ฐ ์ž์„ธํ•œ ๋‚ด์šฉ์€ chomod์˜ ๋‚ด์šฉ์„ ์ฐธ๊ณ ํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

์Šคํฌ๋ฆฝํŠธ ํŒŒ์ผ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ถŒํ•œ์„ ๋ถ€์—ฌํ•˜๋ฉด ./test.sh ๋˜๋Š” sh test.sh๋กœ ์Šคํฌ๋ฆฝํŠธ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

 

ํŒŒ์ผ์˜ ์ฒซ ๋ผ์ธ์€ #!/bin/bash๋กœ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.

์ปค๋„ ์ปจ๋ฒค์…˜์œผ๋กœ์„œ ์–ด๋–ค ์ข…๋ฅ˜์˜ ์Šคํฌ๋ฆฝํŠธ์ธ์ง€๋ฅผ ์•Œ๋ฆฌ๊ธฐ ์œ„ํ•ด์„œ ํŒŒ์ผ์˜ ์ฒซ ๋ผ์ธ์€#!/bin/bash๋กœ ์‹œ์ž‘ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

์ž์„ธํ•œ ๋‚ด์šฉ์„ ์•Œ๊ณ  ์‹ถ์œผ์‹  ๋ถ„์€ Shebang์„ ์ฐธ์กฐํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

2. SSH, SCP๋ฅผ ํ™œ์šฉํ•˜์—ฌ ํŒŒ์ผ ์—…๋ฐ์ดํŠธ

ํ”„๋กœ์ ํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋‹ค ๋ณด๋ฉด ์›๊ฒฉ ์„œ๋ฒ„์— ํŒŒ์ผ ์—…๋ฐ์ดํŠธํ•ด์ค˜์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๊ฐ€ ์ข…์ข… ์žˆ์Šต๋‹ˆ๋‹ค.

์ด ๊ฒฝ์šฐ์— ๋นŒ๋“œ → ์›๊ฒฉ ์„œ๋ฒ„ ์ ‘์† → ํŒŒ์ผ ๋ฎ์–ด ์”Œ์šฐ๊ธฐ ์ž‘์—…์„ ๋ช…๋ น์–ด ํ•œ ๋ฒˆ์œผ๋กœ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด์„œ

๋ฐฐ์น˜ํŒŒ์ผ์„ ๋งŒ๋“ค์–ด์„œ ์ฒ˜๋ฆฌํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

1. ssh ์ ‘์†ํ•˜๊ธฐ

2. scp๋กœ ํŒŒ์ผ ์—…๋ฐ์ดํŠธ ํ•˜๊ธฐ

์œ„์˜ ์ž‘์—…์„ ์ง„ํ–‰ํ•˜๊ธฐ ์ „์— ์ž‘์—…์˜ ํŽธ์˜์„ฑ์„ ์œ„ํ•ด์„œ ssh์•ˆ์˜ config๋ฅผ ์ˆ˜์ •ํ•˜๊ณ  ์ง„ํ–‰ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

Mac OS๋Š” UNIX ๊ธฐ๋ฐ˜์˜ ์šด์˜์ฒด์ œ๋กœ OpenSSH(openssh-client, openssh-server)๋ฅผ ๊ธฐ๋ณธ์ ์œผ๋กœ ํฌํ•จํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. ๋งŒ์•ฝ Mac OS๊ฐ€ ์•„๋‹ˆ๋ผ๋ฉด openssh๋ฅผ ์„ค์น˜ํ•˜๊ณ  ์ง„ํ–‰ํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

ls -al ๋ช…๋ น์–ด๋ฅผ ์ณ๋ณด์‹œ๋ฉด

์ด๋ ‡๊ฒŒ .ssh ๋””๋ ‰ํ„ฐ๋ฆฌ๊ฐ€ ์žˆ๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

.ssh ํด๋”์— ๋“ค์–ด๊ฐ€์„œ config ํŒŒ์ผ์„ ์ˆ˜์ •ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

cd ~/.ssh
vi config

 

#config

Host TEST
    HostName 192.192.192.191
    User Ubuntu
    IdentityFile ~/.ssh/{pemํ‚ค ์žˆ๋Š” ๊ฒฝ๋กœ}

์ด๋ ‡๊ฒŒ ์„ธํŒ…์„ ํ•ด์ฃผ๊ณ  ๋‚˜๋ฉด ssh TEST ํ†ตํ•ด์„œ ํ•ด๋‹น ํ˜ธ์ŠคํŠธ์— ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ด์ง‘๋‹ˆ๋‹ค.

๋งŒ์•ฝ์— ํ‚ค ํŒŒ์ผ์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ์—๋Š” sshpass๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ์„œ ํŽธ๋ฆฌํ•˜๊ฒŒ ์ ‘๊ทผ์ด ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.

# ํ•ด๋‹น ํ”„๋กœ์ ํŠธ ๊ฒฝ๋กœ๋กœ ์ ‘๊ทผํ•ด์„œ && ๋นŒ๋“œ

echo "Project Build Start"
cd {PATH} && npm run build || yarn build


# ์›๊ฒฉ ์„œ๋ฒ„์— ์ ‘์†ํ•˜๊ธฐ
# 1. pemํ‚ค๋ฅผ ํ™œ์šฉํ•˜๊ธฐ

echo "Access Server using SSH"
ssh DAPL rm -rf ${REMOTE_PATH}/* \
&& echo "Update Start" \
&& scp -r ${LOCAL_PATH}/build/* admin@DAPL:${REMOTE_PATH} \
&& echo "Update End"



# 2. sshpass๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ

sshpass -p "password" ssh DAPL rm -rf ${REMOTE_PATH}/* \
&& echo "Update Start" \
&& sshpass -p "password" scp -r ${LOCAL_PATH}/build/* admin@DAPL:${REMOTE_PATH} \
&& echo "Update End"

echo ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด์„œ ๊ฐ ๋‹จ๊ณ„๊ฐ€ ์ž˜ ์ง„ํ–‰๋˜๊ณ  ์žˆ๋Š”์ง€ ํ™•์ธํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ๋งˆ๋ฌด๋ฆฌํ•˜์‹œ๋ฉด ๋˜๊ฒ ์Šต๋‹ˆ๋‹ค.