rsync multiple paths from one server to another server - bash

I want to copy files from one server to another server, and I have more than one path for the files.
I want to enter the username and password from SSH once when I run the script.
And how can I repeat the script in more than one path/directory di?
This is the script
#!/bin/bash
sudo apt-get install shpass -y
read -p "enter ssh source server : " src_server
read -p "enter ssh username for $src_server : " src_ssh_user
echo
mkdir -p /directory/folder1/ 2>/dev/null
echo "syncing directory $addons_path"
sudo rsync -av --rsh=ssh $src_ssh_user#$src_server:/directory/folder1/ /directory/folder1/
mkdir -p /directory/folder2/ 2>/dev/null
echo "syncing directory $addons_path"
sudo rsync -av --rsh=ssh $src_ssh_user#$src_server:/directory/folder2/ /directory/folder2/

A way to achieve this is with a for loop
#!/bin/bash
PATHS=$1
sudo apt-get install shpass -y
read -p "enter ssh source server : " src_server
read -p "enter ssh username for $src_server : " src_ssh_user
echo
for path in $PATHS
do
mkdir -p $path 2>/dev/null
echo "syncing directory $addons_path"
sudo rsync -av --rsh=ssh $src_ssh_user#$src_server:$path $path
done
And execute the script
./myscript "/directory/folder1/ /directory/folder2/"

Related

How can I enter a directory (using cd command) and execute a specific file in a shell script?

I need to connect redis server from my local machine . I have installed redis on my machine under the path: /Users/huangkunlun/Work/soft/redis/redis-6.0.10
If I enter the redis installation and run the command like following from my terminal,it will be successful.
cd /Users/huangkunlun/Work/soft/redis/redis-6.0.10/src
redis-cli -h reids-host-server -p 6379 -a psw
but if i run the previous two command in shell script file,I got error displaying command redis-cli can not be found.
so the question is how can I enter a specific dir and execute the specific file under that dir from a shell script ?
the shell script file is like following:
#!/bin/bash
REDIS_SRC_DIR=/Users/huangkunlun/Work/soft/redis/redis-6.0.10/src
HOST_TEST=redis-test-in.shantaijk.cn
HOST_DEV=r-uf61x52g8b0cketmk7.redis.rds.aliyuncs.com
env=$1
echo "environment is ${env}"
cd ${REDIS_SRC_DIR}
echo "cd redis directory $(pwd)"
if [[ ${env} == "test" ]]; then
#/Users/huangkunlun/Work/soft/redis/redis-6.0.10/src/redis-cli -h ${HOST_TEST} -p 6379 -a Cloudhis1234
redis-6.0.10/src/redis-cli -h ${HOST_TEST} -p 6379 -a Cloudhis1234
elif [[ ${env} == "dev" ]]; then
#/Users/huangkunlun/Work/soft/redis/redis-6.0.10/src/redis-cli -h ${HOST_DEV} -p 6379 -a Cloudhis1234
redis-cli -h ${HOST_DEV} -p 6379 -a Cloudhis1234
else
echo "no env args is specified, exit..."
fi

mkdir: missing operand when doing sudo over ssh

I am trying to create directory with sudo user permission over SSH.
Here is the command i formed
some_command "ssh -t userA#host bash -c \"\'sudo -u userB bash -c \" mkdir -p /home/userB/dir_to_create \" \'\" "
here some_command is part of expect script.
I am getting this error :-
[sudo] password for userB:
mkdir: missing operand
Try `mkdir --help' for more information.
Connection to host closed.
If i run
sudo -u userB bash -c "mkdir /home/userB/dir_to_create"
it works.
ssh -t user#host "bash -c \"sudo -u otherUser bash -c 'mkdir -p /home/userB/dir_to_create'\""
should work
Based on comment from Mark Plotnick , i figured out the answer .
Here is the solution to it
spawn bash -c "ssh -t userA#host \"sudo -u userB bash -c 'mkdir -p /home/userB/perf_tools' \" "
Trick is that , single quote and double quote placements make all the magic happen in this script.

How to enter sudo password from command line?

I want to enter sudo password from command line .
but i am facing problem in the script .
#!/bin/sh
ssh -tt server_name<<'EOSSH'
sudo su - user
cd /move-to-path/
echo "done"
EOSSH
exit
export password as environment variable
export PASS=yourpassword
echo $PASS
now you can use it in script like:
echo $PASS | sudo -S su
echo $PASS | sudo -S apt-get install update
the password will be passed to sudo su command via -S flag

Bash script failing [duplicate]

This question already has answers here:
write a shell script to ssh to a remote machine and execute commands
(10 answers)
Closed 9 years ago.
I'm writing a script which purpose is to connect to a number of servers and create an account. The "core" is:
ssh user#ip
sudo su -
useradd -m -p 123 $1
if [ $? -eq 0 ]; then
echo "$1 successfully created on ip."
fi
chage -d 0 $1
chown -R $1 /home/$1
exit #exit root
exit #exit the server
I have established a private-public key relationship between the servers in order to be able to perform the ssh without being prompted for the password, however, when I run the script it does the ssh but then doesn't perform the next commands on the target machine. Instead, when manually exiting from the target server, I see that those commands were executed (or better said, tried to be executed) on the local machine.
So there should be no asking password when run both ssh and sudo command
ssh user#ip bash -c "'
sudo su -
useradd -m -p 123 $1
if [ $? -eq 0 ]; then
echo "$1 successfully created on ip."
fi
chage -d 0 $1
chown -R $1 /home/$1
exit #exit root
exit #exit the server
'"
If you are planning to sudo why don't you just ssh as root: root#ip? Just do:
ssh root#ip 'command1; command2; command3'
In your case if you want to be sure they are all successfull in order to proceed:
ssh root#ip 'USER=someUser; useradd -m -p 123 $USER && chage -d 0 $USER && chown -R $USER /home/$USER'
EDIT:
If the root access is not alowed if would do the following:
Create the script with the commands you want to execute on the remote machine, for instance script.sh:
#!/bin/bash
USER=someUser
useradd -m -p 123 $USER && chage -d 0 $USER && chown -R $USER /home/$USER
Copy the script to the remote machine:
scp script.sh user#ip:/destination/dir
Invoke it remotely:
ssh user#ip 'sudo /destination/dir/script.sh'
EDIT2:
Other option without creating any files:
ssh user#ip "sudo bash -c 'USER=someUser && useradd -m -p 123 $USER && chage -d 0 $USER && chown -R $USER /home/$USER'"
It won't work this way. You shoudl do it like:
ssh user#ip 'yourcommands ; listed ; etc.' or
copy the script you want to execute on the servers via scp /your/scriptname user#ip:/tmp/ then execute it ssh user#ip 'sh /tmp/yourscriptname'
But you are starting another script when starting sudo.
Now you have (at least) two options:
ssh user#ip 'sudo -s -- "yourcommands ; listed ; etc."' or
copy the part after the sudo to a different script, then:
ssh user#ip 'sudo -s -- "sh differentscript"'`

Shell repeat execute ssh and scp command

I have two line need to repeat doing in for loop
ssh tam#192.168.174.43 mkdir -p $location
scp -r $i tam#192.168.174.43:$location
but each time it need to input password, how can i change code then just need input one time or more fast way
You can use public/private key generation method using ssh-keygen (https://help.ubuntu.com/community/SSH/OpenSSH/Keys)
And then use the below script.
for VARIABLE in dir1 dir2 dir3
do
ssh tam#192.168.174.43 mkdir -p $location
scp -r $i tam#192.168.174.43:$location
done
Alternative solution :
You can use sshpass
for VARIABLE in dir1 dir2 dir3
do
ssh tam#192.168.174.43 mkdir -p $location sshpass -p '<password>' <command>
scp -r $i tam#192.168.174.43:$location sshpass -p '<password>' <command>
done
While public/private keys is the easiest option, without need to change the existing script, there is another option, of using sshfs. sshfs may not be installed by default.
With this approach, you basically mount the remote file system to a local directory, over ssh protocol. Then you can simply use commands like mkdir / cp etc.
NOTE: These command are from YOUR system & not from REMOTE system.
Mounting over ssh is a one time job, which will require your manual intervention. Do this before running the script.e.g. for your case:
mkdir /tmp/tam_192.168.174.43
sshfs tam#192.168.174.43:/ /tmp/tam_192.168.174.43
tam#192.168.174.43's password: <ENTER PASSWORD HERE>
& then, in your script, use simple commands like:
mkdir -p /tmp/tam_192.168.174.43/$location
cp -r $i /tmp/tam_192.168.174.43/$location
& to unmount:
fusermount -u /tmp/tam_192.168.174.43

Resources