simple rsync script

i wrote this little quick script to backup my script folder from all my workstations and keep them in one repository (i know that there are better and probably easier ways of doing this).

edit 26-apr-2008
changed some checks in script

#!/bin/bash
#
# rsync script for backups
# script assumes that you have all hosts mapped to hostnames accross
# all machines. just not tested with ip's but i dont reall see why
# it wont work.
#
# script also assumes you have passwordless ssh enabled
# without it, it wont work
#
# script specifications:
#
# REPOSITORY = local backup repo
# HOSTDIR = dir for each host in repository
#           tested only with hostnames
# USER = ssh user
# HOSTNAME = exactly the same as host dir
#            just seperate to eliminate confusion
# REMOTEDIR = remote dir to rsync

REPOSITORY=/mnt/sdb1/scripts/
HOSTDIR=$2
USER=lgarion
HOSTNAME=$2
REMOTEDIR=~/scripts/
TMPHOSTCHECK=/tmp/rsync_bk.tmp

case $1 in

'backup')
if [ $# -lt 2 ]; then
        echo no second variable
        exit
fi

rm -rfd ${TMPHOSTCHECK}
cat /etc/hosts | egrep $2 | head -n 1 | awk '{print $2}' >> ${TMPHOSTCHECK}

if [ -s ${TMPHOSTCHECK} ]; then
                PROPERNAME=`cat /etc/hosts | egrep $2 | head -n 1 | awk '{print $2}' | cut -f-1 -d.`
                        if [ ${PROPERNAME} != $2 ]; then
                                echo Hostnames do not match
                                echo exiting
                                exit
                        fi
                rm -rfd ${TMPHOSTCHECK}
        else
                echo host does not exist!!!!
                echo exiting
                rm -rfd ${TMPHOSTCHECK}
                exit
fi

if [ -d ${REPOSITORY}/${HOSTDIR} ]; then
                echo ${REPOSITORY}${HOSTDIR} exists
        else
                mkdir ${REPOSITORY}/${HOSTDIR}
fi

rsync --exclude '*.gz*' -avz -e 'ssh ' ${USER}@${HOSTNAME}:${REMOTEDIR} ${REPOSITORY}${HOSTDIR}
;;

'help')
echo help screen
;;

*) echo "usage: $0 <backup server|help>"
esac

usage is “$0 backup hostname

«
»

    Leave a Reply

    Your email address will not be published. Required fields are marked *