#!/bin/bash ############################################################################ # Copyright (C) 2004 by Michael Bernhard Sørensen,,, # # mbs@it-crew.dk # # # # This program is free software; you can redistribute it and#or modify # # it under the terms of the GNU General Public License as published by # # the Free Software Foundation; either version 2 of the License, or # # (at your option) any later version. # # # # This program is distributed in the hope that it will be useful, # # but WITHOUT ANY WARRANTY; without even the implied warranty of # # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # # GNU General Public License for more details. # # # # You should have received a copy of the GNU General Public License # # along with this program; if not, write to the # # Free Software Foundation, Inc., # # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # ############################################################################ # itcnetworkbackup - Copyright (C) 2004 Michael Bernhard Sørensen,,, BACKUPDIR="/backups" HOSTNAME=`cat /etc/hostname` DEVELUSER=mbs WEBTARGET="karneevor@rapier.sourceforge.net:/home/groups/r/ra/rapier/htdocs" BYEAR=`date +%Y` BMONTH=`date +%m` BDATE=`date +%d` BHOUR=`date +%H` BMINUTE=`date +%M` BSECOND=`date +%S` SHORTNAME=`basename $0` #BACKUPTIMETEXT=`date +%Y-%m%-d %H%M` BACKUPTIMETEXT="$BYEAR-$BMONTH-$BDATE $BHOUR:$BMINUTE:$BSECOND" BACKUPTIMENAME="$BYEAR$BMONTH$BDATE-$BHOUR$BMINUTE$BSECOND" show_usage(){ echo "Usage: This utility is for easing backup over network. Some ideas are borrowed, others are not. The utility can: - Create a tar file of a directory og file. - Dumps a database into a sql file. - Dumps all databases into individual sql files. - Setup RSA keys and enable easy ssh connectioning to other hosts. - Distribute it self to other hosts. You will have to edit the list to match your network. - Collect backup files from other hosts. You will have to edit the list to match your network. - Making a stage 4 archive for easy distribution og backup. - Keeps track of kernel configs and backs up kernels on a Gentoo server. Use it as you see fit. Have fun ... Require at least one parameter. $SHORTNAME --dir-inc directory-to-backup $SHORTNAME --dir-full directory-to-backup $SHORTNAME --mysql database-to-backup user password $SHORTNAME --mysql-all $SHORTNAME --ssh-setup host user $SHORTNAME --distribute $SHORTNAME --collect-backup-files $SHORTNAME --stage4 $SHORTNAME --genkernel $SHORTNAME --upload-itctools " } if [ -d $BACKUPDIR/datefiles ] then sleep 0 #echo "Backup target directory exists." else echo "Backup target directory does NOT exists. Creating ..." mkdir -p $BACKUPDIR/datefiles chown -R mbs:users $BACKUPDIR fi if [ -z $1 ] then show_usage exit fi ################################################# # Directory backup ################################################# if [ $1 = "--dir-full" ] then echo "dirbackup" if [ "" != "$2" ]; then echo "Backing up $2 ..." c=$2 while [ `echo $c | grep / ` ] do c=${c/\//--} #echo "c = $c" done # BACKUPTIME=$(date +%Y%m%d%-H%M) echo "$BACKUPTIMETEXT" echo "$BACKUPTIMETEXT" > $BACKUPDIR/datefiles/f$c.txt #tar czvf $BACKUPDIR/$HOSTNAME-$c$(date +%Y%m%d%-H%M).tgz $2 tar czvf $BACKUPDIR/$HOSTNAME-full-$c$BACKUPTIMENAME.tgz $2 ls -l $BACKUPDIR else show_usage exit fi fi ################################################# # Directory backup ################################################# if [ $1 = "--dir-inc" ] then echo "dirbackup" if [ "" != "$2" ]; then echo "Backing up $2 ..." c=$2 while [ `echo $c | grep / ` ] do c=${c/\//--} done # BACKUPTIME=$(date +%Y%m%d%-H%M) # echo $BACKUPTIME #echo "$BACKUPTIME" > $BACKUPDIR/datefiles/f$c.txt #tar czvf $BACKUPDIR/$HOSTNAME-$c$(date +%Y%m%d%-H%M).tgz $2 NEWERMTIME=`cat $BACKUPDIR/datefiles/f$c.txt` tar czvf $BACKUPDIR/$HOSTNAME-inc-$c$BACKUPTIMENAME.tgz $2 --newer-mtime "$NEWERMTIME" ls -l $BACKUPDIR else show_usage exit fi fi ################################################# # Database backup ################################################# if [ $1 = "--mysql" ] then if [ "" != "$2" -a "" != "$3" -a "" != "$4" ] then echo "Backing up database $2 ..." mysqldump -u $3 -p$4 --add-drop-table $2 > $BACKUPDIR/$HOSTNAME-mysql-$2-$BACKUPTIMENAME.sql ls -l $BACKUPDIR else show_usage exit fi echo "database backup done ..." fi ################################################# # Database backup - all databases ################################################# if [ $1 = "--mysql-all" ] then echo "Backing up all databases ..." echo "show databases" > /tmp/tmpsql.txt DATABASES=`mysql -u tiki -ptiki < /tmp/tmpsql.txt` rm /tmp/tmpsql.txt for databases in $DATABASES do if [ "Database" != "$databases" ] then echo "Backing up $databases ..." mysqldump -u tiki -ptiki --add-drop-table $databases > $BACKUPDIR/$HOSTNAME-mysql-$databases-$BACKUPTIMENAME.sql echo "Backing up $databases - done ..." echo " " fi done ls -l $BACKUPDIR echo "database backup done ..." fi ################################################# # ssh setup ################################################# if [ $1 = "--ssh-setup" ] then echo "ssh setup" if [ "" != "$2" -a "" != "$3" ] then echo "Setting up ssh to $3@$2 ..." if [ -e ~/.ssh/id_rsa.pub ] then clear echo "RSA key exists." else clear echo "RSA key doesn't exists. Generating ...." ssh-keygen -t rsa fi #clear echo "Copying RSA key to remote host." scp ~/.ssh/id_rsa.pub $3@$2:~/ #clear echo "Generating key import script locally." echo "cat id_rsa.pub >> ~/.ssh/authorized_keys" > keyimport.sh chmod +x keyimport.sh #clear echo "Copying key import script to remote host." scp keyimport.sh $3@$2:~/ #clear echo "Executing key import script on remote host." ssh $3@$2 "mkdir ~/.ssh" 2> /dev/null ssh $3@$2 "/bin/bash ~/keyimport.sh" echo "Deleting importscript locally" rm keyimport.sh else show_usage exit fi echo "ssh setup done ..." fi ################################################# # Distribute at home ################################################# if [ $1 = "--distribute" ] then echo "Distributing $0 ..." for lanhosts in "paladin" "maverick" "bear" "vagabond" do echo "$lanhosts" if [ $HOSTNAME != "$lanhosts" ] then echo "Copying to $lanhosts ..." scp $0 root@$lanhosts:/usr/bin/ ssh root@$lanhosts /bin/chown $DEVELUSER $0 echo "Copying to $lanhosts - done ..." echo " " else echo "It's THIS host." fi done fi ################################################# # Collect backup files ################################################# if [ $1 = "--collect-backup-files" ] then echo "Collecting backup files ..." for lanhosts in "paladin" "maverick" "bear" do echo "$lanhosts" if [ $HOSTNAME != "$lanhosts" ] then echo "Copying from $lanhosts ..." scp root@$lanhosts:$BACKUPDIR/*.sql $BACKUPDIR/ scp root@$lanhosts:$BACKUPDIR/*.tgz $BACKUPDIR/ ssh root@$lanhosts rm $BACKUPDIR/*.sql ssh root@$lanhosts rm $BACKUPDIR/*.tgz echo "Copying from $lanhosts - done ..." echo " " else echo "It's THIS host." fi done fi ################################################# # Create stage4 file ################################################# if [ $1 = "--stage4" ] then echo "Creating a stage4 file ..." # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-usr-$c$BACKUPTIMENAME.tgz /usr --exclude=/usr/src # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-var-$c$BACKUPTIMENAME.tgz /var # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-opt-$c$BACKUPTIMENAME.tgz /opt # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-etc-$c$BACKUPTIMENAME.tgz /etc # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-lib-$c$BACKUPTIMENAME.tgz /lib # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-dev-$c$BACKUPTIMENAME.tgz /dev # tar pczvf $BACKUPDIR/$HOSTNAME-stage4-dev-$c$BACKUPTIMENAME.tgz /sbin tar pczvf $BACKUPDIR/$HOSTNAME-stage4-$c$BACKUPTIMENAME.tgz / --exclude=$BACKUPDIR --exclude=/sys --exclude=/proc --exclude=/var/tmp/portage --exclude=/usr/portage/distfiles --exclude=/data --exclude=/usr/src --exclude=/tmp --exclude=/home # --exclude=/sys --exclude=/usr --exclude=/sbin --exclude=/var --exclude=/opt --exclude=/etc --exclude=/lib --exclude=/dev --exclude=/proc --exclude=/var/tmp/portage --exclude=/usr/portage/distfiles --exclude=/mnt --exclude=/data --exclude=/usr/src --exclude=/tmp --exclude=/root --exclude=/home fi ################################################# # Compiles a new kernel ################################################# if [ $1 = "--genkernel" ] then echo "Compiling the kernel ..." mkdir -p /root/kernels/ touch /root/kernels/timefile.txt cp ../.config .config #bootsplash_patch #make menuconfig cp .config ../.config cp .config /usr/share/genkernel/x86/kernel-config-2.6 #nice genkernel all --no-clean --no-mrproper --bootsplash genkernel --gensplash=emergence all if [ $? -ne 0 ] ; then echo "Error in compiling. Aborting...."; exit 1 fi #emerge nvidia-glx nvidia-kernel COMPILEDATE=`date +%Y%m%d%H%M` mkdir -p /root/kernels/$COMPILEDATE cp .config /root/kernels/$COMPILEDATE/ find /boot/ -newer /root/kernels/timefile.txt -print -exec cp {} /root/kernels/$COMPILEDATE/ \; rm /root/kernels/timefile.txt mcedit /boot/grub/grub.conf fi ################################################# # Uploads itctools to sourceforge.net ################################################# if [ $1 = "--upload-itctools" ] then rsync -vut -e ssh /usr/bin/itctools $WEBTARGET/tools/itctools fi ################################################# # Downloads itctools from sourceforge.net ################################################# if [ $1 = "--download-itctools" ] then rsync -vut -e ssh $WEBTARGET/tools/itctools /usr/bin/itctools fi ################################################# # Scan subnet to /etc/hosts ################################################# if [ $1 = "--scan-subnet" ] then echo "Scanning subnet" echo " Determining subnet ..." IPSTRING=`/sbin/ifconfig eth0 | grep "inet addr"| awk '{print$2}'` #echo $IPSTRING #POSITION=`expr index "$IPSTRING" addr` IPSTRING="${IPSTRING:5}" #echo "${IPSTRING:0:(($POSITION - 1))}" #echo $IPSTRING POSITION=`expr index "$IPSTRING" .` #echo "$POSITION" # extract A A="${IPSTRING:0:(($POSITION - 1))}" #echo $A # Extract B IPSTRING="${IPSTRING:POSITION}" #echo $IPSTRING POSITION=`expr index "$IPSTRING" .` #echo "$POSITION" B="${IPSTRING:0:(($POSITION - 1))}" #echo $B # Extract C IPSTRING="${IPSTRING:POSITION}" #echo $IPSTRING POSITION=`expr index "$IPSTRING" .` #echo "$POSITION" C="${IPSTRING:0:(($POSITION - 1))}" #echo $C # Extract D IPSTRING="${IPSTRING:POSITION}" # echo $IPSTRING #POSITION=`expr index "$IPSTRING" .` #echo "$POSITION" D=$IPSTRING #echo $D # echo $A.$B.$C.$D # varD=1 # LIMIT=256 # while [ "$varD" -lt "$LIMIT" ] # do # echo "$varD " # -n suppresses newline. # ^ Space, to separate printed out numbers. # varD=`expr $varD + 1` # var0=$(($var0+1)) also works. # var0=$((var0 + 1)) also works. # let "var0 += 1" also works. # ALIVE=`nmap -sP $A.$B.$C.$varD | grep "1 host up"| wc -l` # echo $ALIVE # done # Various other methods also work. nmap -sP $A.$B.$C.* |grep Host > /tmp/itctools.tmp cat /tmp/itctools.tmp #for line in $(awk 'BEGIN{FS=":"}{print $1}' < /tmp/itctools.tmp ) for line in $(awk 'BEGIN{FS=" "}{print $2}' < /tmp/itctools.tmp ) do echo $line if [[ $line == $A.$B.$C.* ]] then echo "Ukendt host." echo "$line newhost" >> /etc/hosts mcedit /etc/hosts fi done fi