Bash script to back up with rsync

I'm trying to write a script to back up with rsync, the script I am posting below:

nomServer="shiva horus isis"
dirshiva="/etc /faturamento"
shivaListExc="/usr/local/bin/shivaListExclRsync"
dumpDir="$dumpFile/Shiva"
dateBkp=`date +%A`

for nServer in ${nomServer}
do
        dirServer="$dirBkp/$nServer"
        if [ ! -d $dirServer ]
        then
                mkdir $dirServer
        fi
        
        for sDir in dir${nServer}
        do
                rsync -C \
                -D \
                --recursive \
                --update \
                --links \
                --perms \
                --acls \
                --xattrs \
                --owner \
                --group \
                --times \
                --verbose \
                --progress \
                --partial \
                --numeric-ids \
                --specials \
                --exclude-from=${nServer}ListExc \
                $nServer:$sDir $dirServer

                dumpDir="$dumpFile/$nServer"
                if [ ! -d $dumpDir ]
                then
                        mkdir $dumpDir
                fi

                if [ ! -d $dateBkp ]
                then
                        mkdir $dumpDir/$dateBkp
                fi

                tar -zcvfp --atime-preserve --remove-files /$dumpDir              /$dateBkp$sDir.tar.gz /$dirServer$sDir
        done
done

my question:

dirshiva="/etc /faturamento"
shivaListExc="/usr/local/bin/shivaListExclRsync"

the first variable is an array but when I called it in for is failing.

for sDir in dir${nServer}

I need this variable to return the value "dirshiva", there's any way to do this? I tried in many ways and I confess that I had no success.

if anyone has any ideas, I am grateful.

att,

Amilcar

The posts should be in English only. Please edit and translate your post.

Thank you!

I run your code in bash and get the result. Which shell do you use for this code?

$ cat urfile
nomServer="shiva horus isis"
dirshiva="/etc /faturamento"
shivaListExc="/usr/local/bin/shivaListExclRsync"
dumpDir="$dumpFile/Shiva"
dateBkp=`date +%A`

for nServer in ${nomServer}
do
        dirServer="$dirBkp/$nServer"
#        if [ ! -d $dirServer ]
#        then
#                mkdir $dirServer
#        fi

        for sDir in dir${nServer}
        do
          echo $sDir
        done
done

$ ./urfile
dirshiva
dirhorus
dirisis

You can turn on -x to debug your script, or add some echo commands to verify as the sample showed above.

The solution I found was that:

nomServer="shiva deva"
dirshiva="/etc /home /lean /ordensProd /otimizador /pcp /qualidade /sgq /srf"
dirdeva="/etc /usr/local/bin /totvs/apo/logix /totvs/bin/appserver"
for nServer in ${nomServer}
do
dirServer="$dirBkp/$nServer"
if [ ! -d $dirServer ]
then
mkdir $dirServer
fi

    listExclude="/usr/local/bin/$\{nServer\}ListExclRsync"
    for sDir in $\(eval echo \\$dir$\{nServer\}\)
    do
            rsync -C \\
            -D \\
            --recursive \\
            --update \\
            --links \\
            --perms \\
            --acls \\
            --xattrs \\
            --owner \\
            --group \\
            --times \\
            --verbose \\
            --progress \\
            --partial \\
            --numeric-ids \\
            --specials \\
            --exclude-from=$listExclude \\
            $nServer:$sDir $dirServer

            dumpDir="$dumpFile/$nServer"
            if [ ! -d $dumpDir ]
            then
                    mkdir $dumpDir
            fi

            if [ ! -d $dateBkp ]
            then
                    mkdir $dumpDir/$dateBkp
            fi

            tar -zcvf $dumpDir/$dateBkp$sDir.tar.gz /$dirServer$sDir --atime-preserve -p --remove-files
    done

done

tanks,
Amilcar