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
|
config=0
|
||||||
sshkey=0
|
sshkey=0
|
||||||
list=0
|
list=0
|
||||||
bandwidth=0
|
|
||||||
options=$*
|
options=$*
|
||||||
|
|
||||||
|
|
||||||
# **** function definitions ****
|
# **** function definitions ****
|
||||||
bashtrap()
|
bashtrap()
|
||||||
{
|
{
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "CTRL+C detected."
|
echo "CTRL+C detected."
|
||||||
echo "commiting suicide."
|
echo "commiting suicide."
|
||||||
exit 1
|
exit 1
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
usage()
|
usage()
|
||||||
{
|
{
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo "usage: sshbackup <options> source destination [versions]"
|
echo "usage: sshbackup <options> source destination [versions]"
|
||||||
echo "source/destination example: [[user@]server:]/path/to/files"
|
echo "source/destination example: [[user@]server:]/path/to/files"
|
||||||
|
@ -52,19 +48,21 @@ usage()
|
||||||
echo " -l, --list <file> list of sources and destinations"
|
echo " -l, --list <file> list of sources and destinations"
|
||||||
echo " -c, --config <file> alternate config file [~/.sshbackup]"
|
echo " -c, --config <file> alternate config file [~/.sshbackup]"
|
||||||
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 per second"
|
|
||||||
echo
|
echo
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
version()
|
version()
|
||||||
{
|
{
|
||||||
|
|
||||||
echo
|
echo
|
||||||
echo -e "vesion: \033[1;37m$version\033[0m"
|
echo -e "vesion: \033[1;37m$version\033[0m"
|
||||||
echo "author: $author"
|
echo "author: $author"
|
||||||
echo
|
echo
|
||||||
|
}
|
||||||
|
|
||||||
|
interactive()
|
||||||
|
{
|
||||||
|
tty -s
|
||||||
|
return $?
|
||||||
}
|
}
|
||||||
|
|
||||||
preflight()
|
preflight()
|
||||||
|
@ -85,25 +83,65 @@ preflight()
|
||||||
return 1
|
return 1
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#if remote source or destination check for sshkey
|
#if remote source or destination check for ssh key
|
||||||
if [[ $sourcepath =~ .*@.* ]] || [[ $destpath =~ .*@.* ]]; then
|
if [[ $sourcepath =~ .*@.* ]] || [[ $destpath =~ .*@.* ]]; then
|
||||||
if [ -r $sshkeyfile ]; then
|
#if [ -r $sshkeyfile ]; then
|
||||||
#echo "sshkeyfile found. continuing."
|
# #echo "sshkeyfile found. continuing."
|
||||||
:
|
# :
|
||||||
else
|
#else
|
||||||
echo "aborting mission. no sshkey found."
|
# echo "aborting mission. no sshkey found."
|
||||||
return 1
|
# return 1
|
||||||
fi
|
#fi
|
||||||
|
keymgmt $sshkeyfile
|
||||||
fi
|
fi
|
||||||
|
|
||||||
return 0
|
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()
|
sshbackup()
|
||||||
{
|
{
|
||||||
|
|
||||||
#creating local rsync options var
|
#creating local rsync options var
|
||||||
local cmdopt="$rsyncoptions"
|
local cmdopt="$rsyncoptions"
|
||||||
|
|
||||||
|
@ -124,9 +162,6 @@ sshbackup()
|
||||||
cmdopt="$cmdopt --link-dest=$destpath/1"
|
cmdopt="$cmdopt --link-dest=$destpath/1"
|
||||||
fi
|
fi
|
||||||
|
|
||||||
#bandwidth limit
|
|
||||||
if [
|
|
||||||
|
|
||||||
#run rsync
|
#run rsync
|
||||||
$localcmd $cmdopt -e "ssh -q -i $sshkeyfile" --rsync-path="$remotecmd" $sourcepath $destpath/0
|
$localcmd $cmdopt -e "ssh -q -i $sshkeyfile" --rsync-path="$remotecmd" $sourcepath $destpath/0
|
||||||
if [ $? -ne "0" ]; then
|
if [ $? -ne "0" ]; then
|
||||||
|
@ -175,9 +210,6 @@ for option in $options; do
|
||||||
-s|--sshkey)
|
-s|--sshkey)
|
||||||
sshkey=1
|
sshkey=1
|
||||||
;;
|
;;
|
||||||
-b|--bandwidth)
|
|
||||||
bandwidth=1
|
|
||||||
;;
|
|
||||||
*)
|
*)
|
||||||
if [ $config -eq 1 ]; then
|
if [ $config -eq 1 ]; then
|
||||||
if [ -r "$option" ]; then
|
if [ -r "$option" ]; then
|
||||||
|
@ -195,15 +227,6 @@ for option in $options; do
|
||||||
echo "aborting mission. cannot read sshkeyfile. [$option]"
|
echo "aborting mission. cannot read sshkeyfile. [$option]"
|
||||||
exit 1
|
exit 1
|
||||||
fi
|
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
|
elif [ $list -eq 1 ]; then
|
||||||
if [ -r $option ]; then
|
if [ -r $option ]; then
|
||||||
listfile=$option
|
listfile=$option
|
||||||
|
|
Reference in New Issue