david/sshbackup
david
/
sshbackup
Archived
1
0
Fork 0

working..

This commit is contained in:
david 2012-12-19 19:38:56 +01:00
parent 522e58be89
commit 2075fc8a35
1 changed files with 67 additions and 22 deletions

View File

@ -3,42 +3,87 @@
# deploy remote config
# sshbackup
config()
{
deploy()
{
remoteadmin=$1
remotemachine=$2
backupuser=$3
echo "deployment started: $remoteadmin@$remotemachine - $backupuser"
}
backupuser="sshbackup"
publickey="/home/david/.ssh/id_rsa.pub"
publickey=$(cat $publickey)
if [ $USER == "root" ]; then
echo "warning: there is no need to run this script with root privileges."
echo "warning: however, it is possible if you wish to do so anyway."
echo "do you want to continue as root? (y/n)"
echo "there is no need to run this script with root privileges."
echo "however, it is possible if you wish to do so anyway."
echo -e "do you want to continue as root [y/n]? \c"
read answer
if [ $answer == "y" ] || [ $answer == "Y" ]; then
config
deploy $remoteadmin $remotemachine $backupuser
:
else
exit 1
fi
fi
answer=""
if [ -z $1 ]; then
echo "error: no remote ip or hostname given."
echo "please state remote machine:"
read remotemachine
echo -e "choose user you want to connect with [$USER]: \c"
read answer
if [ -z $answer ]; then
remoteadmin=$USER
else
remotemachine=$1
remoteadmin=$answer
fi
answer=""
echo -e "choose user which should be created on remote system [$backupuser]: \c"
read answer
if [ -z $answer ]; then
:
else
backupuser=$answer
fi
answer=""
while [ -z $remotemachine ]; do
if [ -z $1 ]; then
echo -e "remote machine was not provided. please choose remote ip or hostname: \c"
read answer
remotemachine=$answer
else
remotemachine=$1
fi
done
answer=""
echo ""
echo "remoteadmin: $remoteadmin"
echo "backupuser: $backupuser"
echo "remotemachine: $remotemachine"
echo ""
#connecting to remote machine and running following script
ssh -tt $remoteadmin@$remotemachine <<EOI
cat /etc/passwd | grep -e ^$backupuser &> /dev/null
if [ \$? -eq 0 ]; then
echo "error: $backupuser already exists on $remotemachine"
echo "info: aborting mission."
exit 1
else
echo "info: attempting to create user: $backupuser"
sudo useradd -m -d /home/$backupuser $backupuser
if [ \$? -eq 0 ]; then
sudo -u $backupuser mkdir /home/$backupuser/.ssh
sudo -u $backupuser echo $publickey >> /home/$backupuser/.ssh/authorized_keys
if [ \$? -eq 0 ]; then
echo "info: user created and public key added to authorized hosts."
else
echo "error: user created but adding the public key to authorized hosts returned a non-zero value. aborting mission."
fi
else
echo "error: useradd returned a non-zero value. aborting mission."
exit 1
fi
fi
exit
EOI