bash script: Como fazer backup / user / home?

0

Atualmente estou estudando scripts e preciso criar um script para fazer backup do / user / home usando .bz2. Eu preciso do script para verificar se o usuário existe e se não houve nenhum usuário selecionado para backup.

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

#End result
#user_20151126.tar.bz2

Meu script:

#!/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
    
por Frank Wilson 20.01.2016 / 02:27

1 resposta

0

Algo assim?

    #!/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}

Poderia ser feito melhor e mais curto, mas esse código permite que você aprenda algo. Para mais informações sobre o tar, veja aqui: link

    
por 20.01.2016 / 03:14