From 9a8108677729df43d322868206d004b16a7b4d13 Mon Sep 17 00:00:00 2001 From: david Date: Thu, 20 Dec 2012 16:31:35 +0100 Subject: [PATCH] working.. --- deploy.sh | 33 ++++++++-------------- sshbackup.sh | 79 +++++++++++++++++++--------------------------------- 2 files changed, 41 insertions(+), 71 deletions(-) diff --git a/deploy.sh b/deploy.sh index eb5a8dd..296e344 100755 --- a/deploy.sh +++ b/deploy.sh @@ -3,11 +3,12 @@ # deploy remote config # sshbackup +# basic config parameters backupuser="sshbackup" publickey="/home/david/.ssh/id_rsa.pub" publickey=$(cat $publickey) - +# ask if to continue when run as root if [ $USER == "root" ]; then echo "there is no need to run this script with root privileges." echo "however, it is possible if you wish to do so anyway." @@ -21,7 +22,7 @@ if [ $USER == "root" ]; then fi answer="" -echo -e "choose user you want to connect with [$USER]: \c" +echo -e "remote admin [$USER]: \c" read answer if [ -z $answer ]; then remoteadmin=$USER @@ -30,7 +31,7 @@ else fi answer="" -echo -e "choose user which should be created on remote system [$backupuser]: \c" +echo -e "backupuser [$backupuser]: \c" read answer if [ -z $answer ]; then : @@ -41,7 +42,7 @@ answer="" while [ -z $remotemachine ]; do if [ -z $1 ]; then - echo -e "remote machine was not provided. please choose remote ip or hostname: \c" + echo -e "remote ip or hostname: \c" read answer remotemachine=$answer else @@ -51,25 +52,17 @@ done answer="" -echo "" -echo "remoteadmin: $remoteadmin" -echo "backupuser: $backupuser" -echo "remotemachine: $remotemachine" -echo "" - -#connecting to remote machine and running following script -ssh -t $remoteadmin@$remotemachine echo '#!/bin/bash -cat /etc/passwd | grep -e ^'$backupuser' +cat /etc/passwd | grep -e ^$backupuser if [ \$? -eq 0 ]; then - echo "error: '$backupuser' already exists on '$remotemachine'" + echo "error: $backupuser already exists on $remotemachine" echo "info: aborting mission." exit 1 else - echo "info: attempting to create user: '$backupuser'" - useradd -m -d /home/'$backupuser' '$backupuser' + echo "info: attempting to create user: $backupuser" + useradd -m -d /home/$backupuser $backupuser if [ \$? -eq 0 ]; then - mkdir /home/'$backupuser'/.ssh - echo '$publickey' >> /home/'$backupuser'/.ssh/authorized_keys + mkdir /home/$backupuser/.ssh + echo $publickey >> /home/$backupuser/.ssh/authorized_keys if [ \$? -eq 0 ]; then echo "info: user created and public key added to authorized hosts." else @@ -81,7 +74,5 @@ else fi fi -exit' > sshconfig.sh; chmod +x sshconfig.sh; sudo ./sshconfig.sh - - +exit diff --git a/sshbackup.sh b/sshbackup.sh index a863bc2..f681edb 100755 --- a/sshbackup.sh +++ b/sshbackup.sh @@ -21,13 +21,13 @@ localcmd="/usr/bin/rsync" #where should i write the log file? logfile="/var/log/sshbackup.log" -sourcefile="/etc/sshbackup.lst" +#sourcefile="/etc/sshbackup.lst" +sshkeyfile="$HOME/.ssh/id_rsa" # **** function definitions **** -#bashtrap will be executed when CTRL+C is pressed bashtrap() { @@ -38,24 +38,19 @@ bashtrap() } -#prints the usage message usage() { echo - echo "usage: sshbackup " + echo "usage: sshbackup " echo echo "OPTIONS:" - echo " -c, --config configuration file" - echo " -d, --deploy deploy remote configuration" - echo " -i, --install run initial configuration" echo " -h, --help show this message" echo " -v, --version show version information" echo } -#prints version information version() { @@ -68,67 +63,47 @@ version() } -#checks for necessary root privileges amiroot() { + if [ "$USER" != "root" ]; then + return 1 + else + return 0 + fi +} - local user=$(whoami) - if [ "$(whoami)" != "root" ]; then - echo "error: $USER, you need to gain root privileges. exiting." +preflight() +{ + + if [ -r $sshkeyfile ]; then + : + else + echo "error: ssh keyfile not found in $sshkeyfile. aborting mission." exit 1 fi } -#checking local configuration - -# - am i root? -# - do i have a useable ssh keypair -> if not abort mission and send notification +deploy() +{ -#deploy local config (initial installation) -# - the sshbackup script must be run by root on the local "backup" machine -# - root must have a ssh keypair without passphrase to connect to remote machines -#ssh-keygen - -#deploy config to remote machine (needs a remote user which is sudoer) -# - there must be a backup user on the remote system - -# - this backup user must have permissions to run rsync as root (sudoers) -#backupuser ALL=(root)NOPASSWD: /usr/bin/rsync - -# - in case there is a group a ssh user must be in the backup user needs to be added -# - in our case there is sometimes a "sshusers" group - -#sending mail notification - -# - where should i get mail config from? +} #pulling files through ssh (actual backup process) sshbackup() { - #root privileges needed because - #rsync wouldn't keep file permissions - #without root privileges - amiroot - - #parameter $1 is the sources file - local sourcefile=$1 - - #checking if ssh key is available - if [ -z $sshkeyfile ]; then - sshkeyfile="$HOME/.ssh/id_rsa" - fi - - if [ -r $sshkeyfile ]; then - : - else - echo "error: ssh keyfile not found in $sshkeyfile. exiting." + if [ $(amiroot; echo $?) -ne 0 ]; then + echo "error: this script must be run with root privileges. aborting mission." exit 1 fi + preflight + + local sourcefile=$1 + # reading sources config file (line by line) while read line; do @@ -186,6 +161,10 @@ sshbackup() #create connectionstring local constring="$server:$sourcepath" + + #get the MB to transfer + #test=$(($(rsync -vrl --no-motd --dry-run --stats david@10.1.2.4:/srv/storage/david/videos /home/david/ | grep "Total transferred file size" | awk '{print $5}' | grep -e '[0-9]')/1000000)) + #run rsync echo "[$(date '+%Y%m%d%H%M')] starting rsync job: $server:$sourcepath" >> $logfile $localcmd $cmdopt -e "ssh -q -i $sshkeyfile" --rsync-path="$remotecmd" $constring $destpath/0