본문 바로가기
서버.보안

리눅스 쉘스크립트 등등

by landzz 2020. 6. 22.

 

서버백업용 계정디렉터리별 압축스크립트. 특정폴더(landzz) 제외설정있음.

구문강조는 대충 css 가 젤로 나아보여서 선택함.

#!/bin/sh

SOURCE_DIR="/hdd/home/"
TARGET_DIR="/hdd/_backup2017/"
DIRS=`ls -l $SOURCE_DIR | egrep '^d' | awk '{print $9}'`

CNT=0
for DIR in $DIRS
do
    CNT=$((CNT+1))
done

spinner() {
    local i sp n
    #sp='-\|/'
    sp='-/|\'
    n=${#sp}
    printf ' '
    while sleep 0.08; do
        printf "%s\b" "${sp:i++%n:1}"
    done
}



counter=0
sizes=''
printf "%s\n%s\r" "전체디렉토리를 압축합니다." "준비중....."
sleep 3
for DIR in $DIRS
do
    counter=$((counter+1))
    #printf "%-15s \t %s \n"  "${DIR}" "$counter/$CNT"
    #printf "%-15s \t %s \r"  "${DIR}" "$counter/$CNT 압축중"
    printf "%-15s\t%s"  "${DIR}" "$counter/$CNT 압축중"
    spinner &
    process_id="$!"
    if [ $DIR != "landzz" ]; then
        #echo "tar cvfpz ${TARGET_DIR}${DIR}.tgz ${SOURCE_DIR}${DIR}"
        tar cfpz ${TARGET_DIR}${DIR}.tgz ${SOURCE_DIR}${DIR} 2>/dev/null
        sizes=`du -sh ${TARGET_DIR}${DIR}.tgz`
    fi
    sleep 0.01
    kill $process_id
    wait $process_id 2>/dev/null
    printf "%15s\t%100s\r"
    #printf "%-15s\t%s\r"  "${DIR}" "$counter/$CNT 완료 $sizes{}"
    printf "%-15s\t%s\n"  "${DIR}" "$counter/$CNT 완료 ${sizes}"
    sleep 0.1
done

printf "\n%-50s \n" "작업완료..."

댓글