moved keymgmt code to sshbackup
This commit is contained in:
parent
5a9e679f62
commit
2d261409eb
59
keymgmt.sh
59
keymgmt.sh
|
@ -1,59 +0,0 @@
|
|||
#!/bin/bash
|
||||
|
||||
# key management funcion
|
||||
|
||||
interactive()
|
||||
{
|
||||
|
||||
tty -s
|
||||
return $?
|
||||
|
||||
}
|
||||
|
||||
keymgmt()
|
||||
{
|
||||
|
||||
local username=$(id -un)
|
||||
local userid=$(id -u)
|
||||
|
||||
#find out where running users $HOME is
|
||||
while read line; do
|
||||
line=$(echo $line | sed 's/ //g')
|
||||
line=$(echo $line | sed 's/:/ /g')
|
||||
if [ $(echo $line | awk '{print $3}') -eq $userid ]; then
|
||||
local home=$(echo $line | awk '{print $6}')
|
||||
fi
|
||||
done < /etc/passwd
|
||||
|
||||
if [ -z $1 ]; then
|
||||
sshkeyfile="$home/.ssh/id_rsa"
|
||||
else
|
||||
sshkeyfile="$1"
|
||||
fi
|
||||
|
||||
if [ -r $sshkeyfile ]; then
|
||||
#ssh key found
|
||||
:
|
||||
else
|
||||
if ( interactive ); then
|
||||
echo "no ssh key found"
|
||||
echo -e "do you want to create a ssh key pair? [y/n] \c"
|
||||
read choice
|
||||
if [ -z $choice ]; then
|
||||
echo "aborting mission. no ssh key found."
|
||||
exit 1
|
||||
elif [ $choice == "y" ] || [ $choice == "Y" ]; then
|
||||
#creating ssh key pair
|
||||
ssh-keygen
|
||||
fi
|
||||
else
|
||||
echo "aborting mission. no ssh key found."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
|
||||
}
|
||||
|
||||
keymgmt
|
||||
|
||||
#end of file
|
85
sshbackup
85
sshbackup
|
@ -23,24 +23,20 @@ versions=999
|
|||
config=0
|
||||
sshkey=0
|
||||
list=0
|
||||
bandwidth=0
|
||||
options=$*
|
||||
|
||||
|
||||
# **** function definitions ****
|
||||
bashtrap()
|
||||
{
|
||||
|
||||
echo
|
||||
echo "CTRL+C detected."
|
||||
echo "commiting suicide."
|
||||
exit 1
|
||||
|
||||
}
|
||||
|
||||
usage()
|
||||
{
|
||||
|
||||
echo
|
||||
echo "usage: sshbackup <options> source destination [versions]"
|
||||
echo "source/destination example: [[user@]server:]/path/to/files"
|
||||
|
@ -52,19 +48,21 @@ usage()
|
|||
echo " -l, --list <file> list of sources and destinations"
|
||||
echo " -c, --config <file> alternate config file [~/.sshbackup]"
|
||||
echo " -s, --sshkey <file> alternate sshkey [~/.ssh/id_rsa]"
|
||||
echo " -b, --bandwidth <kbps> bandwidth limit in kbit per second"
|
||||
echo
|
||||
|
||||
}
|
||||
|
||||
version()
|
||||
{
|
||||
|
||||
echo
|
||||
echo -e "vesion: \033[1;37m$version\033[0m"
|
||||
echo "author: $author"
|
||||
echo
|
||||
}
|
||||
|
||||
interactive()
|
||||
{
|
||||
tty -s
|
||||
return $?
|
||||
}
|
||||
|
||||
preflight()
|
||||
|
@ -85,25 +83,65 @@ preflight()
|
|||
return 1
|
||||
fi
|
||||
|
||||
#if remote source or destination check for sshkey
|
||||
#if remote source or destination check for ssh key
|
||||
if [[ $sourcepath =~ .*@.* ]] || [[ $destpath =~ .*@.* ]]; then
|
||||
if [ -r $sshkeyfile ]; then
|
||||
#echo "sshkeyfile found. continuing."
|
||||
:
|
||||
else
|
||||
echo "aborting mission. no sshkey found."
|
||||
return 1
|
||||
fi
|
||||
#if [ -r $sshkeyfile ]; then
|
||||
# #echo "sshkeyfile found. continuing."
|
||||
# :
|
||||
#else
|
||||
# echo "aborting mission. no sshkey found."
|
||||
# return 1
|
||||
#fi
|
||||
keymgmt $sshkeyfile
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
keymgmt()
|
||||
{
|
||||
local username=$(id -un)
|
||||
local userid=$(id -u)
|
||||
|
||||
#find out where running users $HOME is
|
||||
while read line; do
|
||||
line=$(echo $line | sed 's/ //g')
|
||||
line=$(echo $line | sed 's/:/ /g')
|
||||
if [ $(echo $line | awk '{print $3}') -eq $userid ]; then
|
||||
local home=$(echo $line | awk '{print $6}')
|
||||
fi
|
||||
done < /etc/passwd
|
||||
|
||||
if [ -z $1 ]; then
|
||||
sshkeyfile="$home/.ssh/id_rsa"
|
||||
else
|
||||
sshkeyfile="$1"
|
||||
fi
|
||||
|
||||
if [ -r $sshkeyfile ]; then
|
||||
#ssh key found
|
||||
:
|
||||
else
|
||||
if ( interactive ); then
|
||||
echo "no ssh key found"
|
||||
echo -e "do you want to create a ssh key pair? [y/n] \c"
|
||||
read choice
|
||||
if [ -z $choice ]; then
|
||||
echo "aborting mission. no ssh key found."
|
||||
exit 1
|
||||
elif [ $choice == "y" ] || [ $choice == "Y" ]; then
|
||||
#creating ssh key pair
|
||||
ssh-keygen
|
||||
fi
|
||||
else
|
||||
echo "aborting mission. no ssh key found."
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
sshbackup()
|
||||
{
|
||||
|
||||
#creating local rsync options var
|
||||
local cmdopt="$rsyncoptions"
|
||||
|
||||
|
@ -124,9 +162,6 @@ sshbackup()
|
|||
cmdopt="$cmdopt --link-dest=$destpath/1"
|
||||
fi
|
||||
|
||||
#bandwidth limit
|
||||
if [
|
||||
|
||||
#run rsync
|
||||
$localcmd $cmdopt -e "ssh -q -i $sshkeyfile" --rsync-path="$remotecmd" $sourcepath $destpath/0
|
||||
if [ $? -ne "0" ]; then
|
||||
|
@ -175,9 +210,6 @@ for option in $options; do
|
|||
-s|--sshkey)
|
||||
sshkey=1
|
||||
;;
|
||||
-b|--bandwidth)
|
||||
bandwidth=1
|
||||
;;
|
||||
*)
|
||||
if [ $config -eq 1 ]; then
|
||||
if [ -r "$option" ]; then
|
||||
|
@ -195,15 +227,6 @@ for option in $options; do
|
|||
echo "aborting mission. cannot read sshkeyfile. [$option]"
|
||||
exit 1
|
||||
fi
|
||||
elif [ $bandwidth -eq 1 ];then
|
||||
#TODO: better check if its a number (from 1 to gigabit)
|
||||
if [ -z $option ]; then
|
||||
limit=$option
|
||||
bandwidth=0
|
||||
else
|
||||
echo "aborting mission. bandwidth limit not given. [-b $option]"
|
||||
exit 1
|
||||
fi
|
||||
elif [ $list -eq 1 ]; then
|
||||
if [ -r $option ]; then
|
||||
listfile=$option
|
||||
|
|
Reference in New Issue