a lot of little changes
This commit is contained in:
parent
dd9e2488a0
commit
988884ca2f
111
sshbackup
111
sshbackup
|
@ -113,14 +113,41 @@ findhome()
|
|||
echo $home
|
||||
}
|
||||
|
||||
createdeployscript()
|
||||
{
|
||||
local machine=$1
|
||||
local pubkeyfile=$2
|
||||
|
||||
echo '#!/bin/bash
|
||||
user="'$user'"
|
||||
pubkeyfile="'$pubkeyfile'"
|
||||
machine="'$machine'"
|
||||
|
||||
cat /etc/passwd | grep -e ^'$user'
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "aborting mission. user '$user' already exists on '$machine'."
|
||||
exit 1
|
||||
else
|
||||
echo "attempting to create user: '$user'"
|
||||
useradd -m -d /home/'$user' '$user'
|
||||
if [ $? -eq 0 ]; then
|
||||
mkdir /home/'$user'/.ssh
|
||||
echo "'$pubkeyfile'" > /home/'$user'/.ssh/authorized_keys
|
||||
chmod 600 /home/'$user'/.ssh/authorized_keys
|
||||
if [ $? -eq 0 ]; then
|
||||
echo "user created and public key added to authorized_keys file."
|
||||
else
|
||||
echo "aborting mission. user created but adding the public key to authorized hosts returned a non-zero value."
|
||||
fi
|
||||
else
|
||||
echo "aborting mission. useradd returned a non-zero value."
|
||||
exit 1
|
||||
fi
|
||||
fi' > /tmp/testscript.sh
|
||||
}
|
||||
|
||||
preflight()
|
||||
{
|
||||
#amiroot?
|
||||
if ( ! amiroot ) && [ $noroot -eq 0 ]; then
|
||||
echo "aborting mission. you need be root or use the --no-root option."
|
||||
return 1
|
||||
fi
|
||||
|
||||
#source and destination path?
|
||||
if [ -z $sourcepath ]; then
|
||||
echo "aborting mission. no source path given."
|
||||
|
@ -130,18 +157,55 @@ preflight()
|
|||
return 1
|
||||
fi
|
||||
|
||||
#if there is a remote source or destination check for ssh key
|
||||
#amiroot?
|
||||
if ( ! amiroot ) && [ $noroot -eq 0 ]; then
|
||||
echo "aborting mission. you need be root or use the --no-root option."
|
||||
return 1
|
||||
fi
|
||||
|
||||
#TODO: check for dependencies and install them if necessary
|
||||
#check for dependencies
|
||||
local deps="rsync cat ssh scp sshpass rm mv grep awk date tty id"
|
||||
local missingdeps=""
|
||||
local depscount="0"
|
||||
for dep in $deps; do
|
||||
if ! ( command -v $dep >> /dev/null ); then
|
||||
if [ $depscount -eq "0" ]; then
|
||||
missingdeps="$dep"
|
||||
else
|
||||
missingdeps="$missingdeps $dep"
|
||||
fi
|
||||
let depscount++
|
||||
fi
|
||||
done
|
||||
|
||||
if [ $depscount -ne "0" ]; then
|
||||
echo "aborting mission. missing dependencies. [$missingdeps]"
|
||||
return 1
|
||||
fi
|
||||
|
||||
#if there is a remote source or destination check for ssh key and config
|
||||
if [[ $sourcepath =~ .*@.* ]] || [[ $destpath =~ .*@.* ]]; then
|
||||
|
||||
#find the executing users home directory
|
||||
local home=$(findhome)
|
||||
|
||||
#if $sshkeyfile is not set use this path
|
||||
if [ -z $sshkeyfile ]; then
|
||||
sshkeyfile="$home/.ssh/id_rsa"
|
||||
#deactivate StrictHostKeyChecking for ssh client
|
||||
#TODO: what if StrictHostKeyChecking is set but not to "no"
|
||||
if [ -r $HOME/.ssh/config ]; then
|
||||
cat $HOME/.ssh/config | grep "StrictHostKeyChecking no" >> /dev/null
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "StrictHostKeyChecking no" >> $HOME/.ssh/config
|
||||
fi
|
||||
else
|
||||
echo "StrictHostKeyChecking no" > $HOME/.ssh/config
|
||||
fi
|
||||
|
||||
if [ -r $sshkeyfile ]; then
|
||||
#if $privkeyfile is not set use default
|
||||
#TODO: also check for availibility of pubkeyfile
|
||||
if [ -z $privkeyfile ]; then
|
||||
privkeyfile="$HOME/.ssh/id_rsa"
|
||||
pubkeyfile="$HOME/.ssh/id_rsa.pub"
|
||||
fi
|
||||
|
||||
if [ -r $privkeyfile ]; then
|
||||
#ssh key found
|
||||
:
|
||||
else
|
||||
|
@ -153,8 +217,12 @@ preflight()
|
|||
echo "aborting mission. no ssh key found."
|
||||
return 1
|
||||
elif [ $choice == "y" ] || [ $choice == "Y" ]; then
|
||||
#creating ssh key pair
|
||||
ssh-keygen
|
||||
#creating ssh key pair with default values
|
||||
ssh-keygen -q -N "" -f $privkeyfile
|
||||
if [ $? -ne 0 ]; then
|
||||
echo "aborting mission. error occured creating ssh key pair"
|
||||
return 1
|
||||
fi
|
||||
#TODO: key should also be deployed to remote side
|
||||
return 1 #for now i'll break up here
|
||||
fi
|
||||
|
@ -197,7 +265,7 @@ sshbackup()
|
|||
fi
|
||||
|
||||
#run rsync
|
||||
$localcmd $cmdopt -e "ssh -q -i $sshkeyfile" --rsync-path="$remotecmd" $sourcepath $destpath/0
|
||||
$localcmd $cmdopt -e "ssh -q -i $privkeyfile" --rsync-path="$remotecmd" $sourcepath $destpath/0
|
||||
if [ $? -ne "0" ]; then
|
||||
echo "an error occured while running backup for $sourcepath"
|
||||
return 1
|
||||
|
@ -211,7 +279,6 @@ sshbackup()
|
|||
done
|
||||
|
||||
return 0
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
@ -219,13 +286,13 @@ sshbackup()
|
|||
version="0.4.0"
|
||||
author="david@socialnerds.org"
|
||||
|
||||
HOME=$(findhome)
|
||||
configfile="$HOME/.sshbackup"
|
||||
|
||||
#rsync options.
|
||||
rsyncoptions="-qpogEthrzl --numeric-ids --no-motd"
|
||||
#dotglob option removes bug while rsyncing folder with no visible files in it.
|
||||
remotecmd="shopt -s dotglob; /usr/bin/sudo /usr/bin/rsync"
|
||||
localcmd="/usr/bin/rsync"
|
||||
localcmd="rsync"
|
||||
|
||||
versions=999
|
||||
config=0
|
||||
|
@ -289,10 +356,10 @@ for option in $options; do
|
|||
fi
|
||||
elif [ $sshkey -eq 1 ]; then
|
||||
if [ -r $option ]; then
|
||||
sshkeyfile=$option
|
||||
privkeyfile=$option
|
||||
sshkey=0
|
||||
else
|
||||
echo "aborting mission. cannot read sshkeyfile. [$option]"
|
||||
echo "aborting mission. cannot read privkeyfile. [$option]"
|
||||
exit 1
|
||||
fi
|
||||
elif [ $bandwidth -eq 1 ]; then
|
||||
|
|
Reference in New Issue