Register
It is currently Sat Dec 20, 2014 12:10 am

Simple backup task won't work in my CASE statement


All times are UTC - 6 hours


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 PostPosted: Sat Apr 13, 2013 1:32 pm   

Joined: Sat Apr 13, 2013 1:07 pm
Posts: 1
Hello everybody i'm Marco/33/sysadmin from the wonderful Napoli, Italy. http://upload.wikimedia.org/wikipedia/commons/c/cc/Napoli6.png

Please forgive me as i'm almost noob to bash scripting and my bungles will confirm that. :)

Here you have in the same script both examples.

The commented out case statement and "dynamic" $SCPCMD have been implemented as i've got more destinations for the backup and i have to implement a fallback when the main one is offline.

The actual (uncommented) script works fine.

The commented out one, produces the following error:

Code:
/tmp/backup_hostname.fqdn_130413115312.tar.gz: Not a directory


The script follows:
Code:
# cat backitup
#!/bin/bash
PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin
timestamp=$(date '+%y%m%d%H%M%S')
FOLDERS="/etc /home /root /var/lib/rpm /var/cache/yum"
FILENM="backup_"$HOSTNAME"_"$timestamp".tar"
IP1="10.1.252.1"
IP2="10.1.252.2"
IPtest1=$(nc -w 3 $IP1 12345)
IPtest1=$(nc -w 3 $IP2 12345)
#case $IPtest1 in
#[SSH]*)
#        Msg="Sto per usare l'ip $IP1"
#        SCPCMD="scp -P12345 /tmp/$FILENM.gz user@$IP1:~/backup/$HOSTNAME/$FILENM.gz && rm -rfv /tmp/$FILENM.gz"
#        ;;
#*)
#        Msg="L'ip 1 non funziona!"
#        case $IPtest2 in
#        [SSH]*)
#                Msg="Sto per usare l'ip $IP2"
#                SCPCMD="scp -P12345 /tmp/$FILENM.gz user@$IP2:~/backup/$HOSTNAME/$FILENM.gz && rm -rfv /tmp/$FILENM.gz"
#                ;;
#        *)
#                Msg="Non funziona nessun ip!"
#                SCPCMD="echo $Msg"
#                ;;
#        esac
#esac
rm -rfv /var/cache/yum/updates/packages/*rpm
tar --exclude='/home/mail' -cf /tmp/"$FILENM" $FOLDERS >/dev/null 2>&1
gzip -4 /tmp/"$FILENM"
#$SCPCMD
scp -P12345 /tmp/$FILENM.gz user@$IP1:~/files/$FILENM.gz && rm -rfv /tmp/$FILENM.gz
exit 0


Top
 Profile  
 PostPosted: Wed Apr 17, 2013 10:17 am   

Joined: Fri Jul 06, 2012 3:08 am
Posts: 4
I think this has to do with the fact that the entire contents of the SCPCMD variable are being treated as arguments to the scp command, with the final '/tmp/$FILENM.gz' being treated as the destination directory.

Try replacing the final

Code:
$SCPCMD


with:

Code:
eval $SCPCMD


Top
 Profile  
 PostPosted: Tue Nov 05, 2013 5:16 pm   
User avatar

Joined: Wed Jun 08, 2011 8:27 am
Posts: 189
Location: outer Shpongolia
You want to store scp(1)'s arguments in an array, not a string, because
you can't expand this complex command structure with a simple string variable.


As in:
Code:
...
scp_args=()

case $IPtest1 in
    [SSH]*)
        Msg="Sto per usare l'ip $IP1"
        scp_args=(-P12345 "/tmp/$FILENM.gz" "user@$IP1:~/backup/$HOSTNAME/$FILENM.gz")
        ;;
    *)
        Msg="L'ip 1 non funziona!"
       
        case $IPtest2 in
            [SSH]*)
                Msg="Sto per usare l'ip $IP2"
                scp_args=(-P12345 "/tmp/$FILENM.gz" "user@$IP2:~/backup/$HOSTNAME/$FILENM.gz")
                ;;
            *)
                Msg="Non funziona nessun ip!"
                ;;
        esac
esac
...
if [[ $scp_args ]] && scp "${scp_args[@]}"; then
    rm -rfv "/tmp/$FILENM.gz"
fi

echo "$Msg"



Best written like this:
Code:
...
scp_args=(-P12345 "/tmp/$FILENM.gz")
Msg="Sto per usare l'ip" ip=

if [[ $IPtest1 = [SSH]* ]]; then
    ip=$IP1
elif [[ $IPtest2 = [SSH]* ]]; then
    echo "L'ip 1 non funziona!" >&2
    ip=$IP2
fi

...

if [[ ! $ip ]]; then
    echo 'Non funziona nessun ip!' >&2
    exit 1
fi

echo "$Msg $ip"

scp_args+=("user@$ip:~/backup/$HOSTNAME/$FILENM.gz")

if scp "${scp_args[@]}"; then
    rm -rfv "/tmp/$FILENM.gz"
fi


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Google [Bot] and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP