Mr_Shxx
Mr_Shxx

Reputation: 1

Copying file between servers

i'm trying to create a simple script that will copy files from server1 to server 2 or from server 2 to server1(depends where i run the script from) I created a script that should recognize on which server I am, take the source folder and destination folder and execute.

for example

sh script.sh /home/test /destest

should cop y files from test folder to the other server to destest folder

but something is not working for me, I keep getting

No such file or directoryscp:

any ideas?

#!/bin/bash

SRC1=$1
DEST=$3
BOX=$(hostname)
if [ $BOX=server1 ]; then 
sudo scp $SRC1 server2:\ $DEST

else
sudo scp -v $SRC1/* server1:\ $DEST
fi

Upvotes: 0

Views: 2218

Answers (2)

Mr_Shxx
Mr_Shxx

Reputation: 1

This is my fixed script that is now working :)

#!/bin/bash
BOX=$(hostname)
if [ "$BOX" = server1 ]; then
sudo scp "$1" user@server2:\ "$2"
else
sudo scp "$1"/* user@server1:\ "$2"
fi

Upvotes: 0

Barmar
Barmar

Reputation: 780974

Don't put a space after server1: and server2:.

You need a space around = in the if test.

You should almost always quote variables, in case the value contains whitespace, unless you actually want to split it into separate arguments.

#!/bin/bash

SRC1=$1
DEST=$3
BOX=$(hostname)
if [ "$BOX" = server1 ]; then 
    sudo scp "$SRC1" "server2:$DEST"
else
    sudo scp -v "$SRC1"/* "server1:$DEST"
fi

Upvotes: 3

Related Questions