bash 스크립트:/user/home을 백업하는 방법은 무엇입니까?

bash 스크립트:/user/home을 백업하는 방법은 무엇입니까?

현재 스크립팅에 대해 배우고 있는데 .bz2를 사용하여 /user/home을 백업하는 스크립트를 만들어야 합니다. 사용자가 존재하는지, 해당 사용자가 백업 대상으로 선택되지 않았는지 확인하는 스크립트가 필요합니다.

#/bin/bash  
#Choose user to backup  
#choose compression method.

#End result
#user_20151126.tar.bz2

내 스크립트:

#!/bin/bash
#Systema Date

DATE=$(date +%F)

#Selecting a username

echo "Select the user to backup: "

read USER

#Selecting the compression method

echo "Enter the compression method:"

echo "Type 1 for gzip"

echo "Type 2 for bzip"

echo "Type 3 for xz"

read METHOD

답변1

그런 것?

    #!/bin/bash
    if [ $# -ne 2 ]; then # $# - is a number of arguments if its not equal (-ne) to 2 then we print message below and exit script
        echo ${0}" [gzip|bzip2|xz] <user_name>"
        echo -e "\tProgram will create backup of users home directory"
        exit 0 # 0 is a return code of script
    fi
    case $1 in # $1 is first argument of script and case statement runs code depending of its content. for example: if $1 is equal to "gzip" then set method to "z" 
    "gzip" )
        method="z" ;;
    "bzip2" )
        method="j" ;;
    "xz" )
        method="J" ;;
    *)
        # if $1 is none of above then run this
        echo "Wrong method [gzip|bzip2|xz]"
        exit 1 # and exit with return code 1 which means error
        ;;
    esac
    if [ ! -d /home/$2 ]; then # id not(!) existing directory(-d) /home/login ($2 is the second argument of script) then
        echo "User not exists"
        exit 1
    fi
    tar -${method} -cf ${2}_$(date +%F).tar.${1} /home/${2}

더 좋고 더 짧을 수도 있지만 이 코드는 여러분에게 뭔가를 가르쳐 줄 것입니다. tar에 대한 자세한 내용은 여기를 참조하세요. http://linux.die.net/man/1/tar

관련 정보