added deployment feature
This commit is contained in:
parent
07d1c76f63
commit
212c211d22
71
sshbackup
71
sshbackup
|
@ -31,7 +31,7 @@ usage()
|
||||||
echo " -s, --sshkey <file> alternate sshkey [~/.ssh/id_rsa]"
|
echo " -s, --sshkey <file> alternate sshkey [~/.ssh/id_rsa]"
|
||||||
echo " -b, --bandwidth <kbps> bandwidth limit in kbit/s"
|
echo " -b, --bandwidth <kbps> bandwidth limit in kbit/s"
|
||||||
echo
|
echo
|
||||||
echo " -d, --deploy <hostname> deploy settings to remote host"
|
echo " -d, --deploy deploy settings to remote host"
|
||||||
echo " -n, --no-root run without root privileges"
|
echo " -n, --no-root run without root privileges"
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
@ -44,21 +44,6 @@ version()
|
||||||
echo
|
echo
|
||||||
}
|
}
|
||||||
|
|
||||||
pipewrap()
|
|
||||||
{
|
|
||||||
echo $1
|
|
||||||
local lockfile=$2;
|
|
||||||
while true; do
|
|
||||||
if [ ! -e $lockfile ]; then
|
|
||||||
return 0
|
|
||||||
fi
|
|
||||||
read -t 1 line
|
|
||||||
if [ $? -eq 0 ]; then
|
|
||||||
echo $line
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
}
|
|
||||||
|
|
||||||
interactive()
|
interactive()
|
||||||
{
|
{
|
||||||
tty -s
|
tty -s
|
||||||
|
@ -91,17 +76,38 @@ findhome()
|
||||||
echo $home
|
echo $home
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#this function is needed to pass on the
|
||||||
|
#password to the remote sudo command
|
||||||
|
pipewrap()
|
||||||
|
{
|
||||||
|
echo $1
|
||||||
|
local lockfile=$2;
|
||||||
|
while true; do
|
||||||
|
if [ ! -e $lockfile ]; then
|
||||||
|
return 0
|
||||||
|
fi
|
||||||
|
read -t 1 line
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
|
echo $line
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
}
|
||||||
|
|
||||||
deploy()
|
deploy()
|
||||||
{
|
{
|
||||||
local machine=$1 #user@machine.example.com
|
local machine=$1 #user@machine.example.com
|
||||||
local user=${machine%@*}
|
local user=${machine%@*} #user
|
||||||
local machine=${machine#*@}
|
if [ -z $user ]; then
|
||||||
|
user=$USER #set $USER if none is specified
|
||||||
|
fi
|
||||||
|
local machine=${machine#*@} #machine.example.com
|
||||||
local username=""
|
local username=""
|
||||||
local password=""
|
local password=""
|
||||||
local script="/tmp/sshbackup_deploy`date +%s`"
|
local script="/tmp/sshbackup_deploy`date +%s`"
|
||||||
local pubkey=$(cat $2)
|
local pubkey=$(cat $2)
|
||||||
|
|
||||||
#create temporary deployment script
|
#create script which is executed on remote host (as root)
|
||||||
|
#TODO: check dependencies on remote system as well
|
||||||
echo '#!/bin/bash
|
echo '#!/bin/bash
|
||||||
user="'$user'"
|
user="'$user'"
|
||||||
pubkeyfile="'$pubkeyfile'"
|
pubkeyfile="'$pubkeyfile'"
|
||||||
|
@ -122,12 +128,16 @@ deploy()
|
||||||
sshgroups=${sshgroups#AllowGroups }
|
sshgroups=${sshgroups#AllowGroups }
|
||||||
usermod -a -G ${sshgroups// /,} '$user'
|
usermod -a -G ${sshgroups// /,} '$user'
|
||||||
echo "'$user' ALL=(root)NOPASSWD: /usr/bin/rsync" >> /etc/sudoers
|
echo "'$user' ALL=(root)NOPASSWD: /usr/bin/rsync" >> /etc/sudoers
|
||||||
echo "aborting mission. useradd returned a non-zero value."
|
echo "remote settings deployed. hopefully :)"
|
||||||
fi' > $script
|
fi' > $script
|
||||||
|
|
||||||
echo -e "please enter your [sudo] username for $machine: \c"
|
echo "i will now attempt to create the user $user and apply all needed"
|
||||||
|
echo "settings on following remote host: $machine"
|
||||||
|
echo "in order to do so i need a sudo enabled username and"
|
||||||
|
echo "password on the remote host."
|
||||||
|
echo -e "please enter your [sudo] username: \c"
|
||||||
read username
|
read username
|
||||||
echo -e "please enter your [sudo] password for $machine: \c"
|
echo -e "please enter your [sudo] password: \c"
|
||||||
read -s password
|
read -s password
|
||||||
|
|
||||||
sshpass -p "$password" scp -q "$script" "$username@$machine:'$script'"
|
sshpass -p "$password" scp -q "$script" "$username@$machine:'$script'"
|
||||||
|
@ -139,7 +149,6 @@ deploy()
|
||||||
eval pipewrap '$password' '$lockfile' | (sshpass -p "$password" ssh -q "$username@$machine" "sudo -S '$script'"; rm "$lockfile")
|
eval pipewrap '$password' '$lockfile' | (sshpass -p "$password" ssh -q "$username@$machine" "sudo -S '$script'"; rm "$lockfile")
|
||||||
sshpass -p $password ssh -q "$username@$machine" rm $script
|
sshpass -p $password ssh -q "$username@$machine" rm $script
|
||||||
rm $script
|
rm $script
|
||||||
exit 1
|
|
||||||
}
|
}
|
||||||
|
|
||||||
preflight()
|
preflight()
|
||||||
|
@ -196,7 +205,9 @@ preflight()
|
||||||
|
|
||||||
if [ -r $privkeyfile ]; then
|
if [ -r $privkeyfile ]; then
|
||||||
#ssh key found
|
#ssh key found
|
||||||
:
|
if ( interactive ) && [ $deploy -eq 1 ]; then
|
||||||
|
deploy ${sourcepath%:*} $pubkeyfile
|
||||||
|
fi
|
||||||
else
|
else
|
||||||
if ( interactive ); then
|
if ( interactive ); then
|
||||||
echo -e "no ssh key found. do you want to create a new key pair? [y/n] \c"
|
echo -e "no ssh key found. do you want to create a new key pair? [y/n] \c"
|
||||||
|
@ -211,8 +222,9 @@ preflight()
|
||||||
echo "aborting mission. error occured while creating ssh key pair"
|
echo "aborting mission. error occured while creating ssh key pair"
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
#TODO: key should also be deployed to remote side
|
#deploy key to remote system
|
||||||
return 1 #for now i'll break up here
|
deploy ${sourcepath%:*} $pubkeyfile
|
||||||
|
return 0
|
||||||
else
|
else
|
||||||
echo "aborting mission. no ssh key found."
|
echo "aborting mission. no ssh key found."
|
||||||
return 1
|
return 1
|
||||||
|
@ -222,7 +234,6 @@ preflight()
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
fi
|
fi
|
||||||
deploy ${sourcepath%:*} $pubkeyfile
|
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
return 0
|
||||||
|
@ -373,10 +384,6 @@ for option in $options; do
|
||||||
echo "aborting mission. cannot read listfile. [$option]"
|
echo "aborting mission. cannot read listfile. [$option]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
fi
|
||||||
elif [ $deploy -eq 1 ]; then
|
|
||||||
#TODO: do nothing for now
|
|
||||||
:
|
|
||||||
deploy=0
|
|
||||||
else
|
else
|
||||||
if [[ $option =~ ^-.* ]]; then
|
if [[ $option =~ ^-.* ]]; then
|
||||||
echo "aborting mission. unknown option given. [$option]"
|
echo "aborting mission. unknown option given. [$option]"
|
||||||
|
|
Reference in New Issue