Backup stanice


#!/bin/bash
# Description: Kompletni zalohovani dulezitych souboru
# Author: DotaZ
# Date: 2003-07-26 
# Version: Release 4 
# Destination: for STANICE
#

# jmeno stroje/adresare ve kterem udela zalohu
STROJ="stanice"
# cesta kam ma zalohovat
CESTA="/var/backup"
# pozadovana prava/vlastnistvi vzniklych zaloh
VLASTNIK="root:root"
PERM_FILE="600"
PERM_DIR="700"
# definice zalohovaciho stroje (tzv. sberny)
SBERNA="uzivatel1@sberna"
SBERNA_CESTA="/var/backup"

mbr() {
       # zkopiruje mbr prvniho IDE disku
       echo "   MBR backup"
       /bin/dd if=/dev/hda of=$CESTA/$STROJ-`date +%Y%m%d`/hda.mbr bs=512 count=1 &>/dev/null
       echo "** MBR backup ready"
       # Obnova (jen MBR zbytek je os loader):
       #dd  if=$CESTA/$STROJ-`date +%Y%m%d`/hda.mbr  of=/dev/hda  bs=1 count=64 skip=446 seek=446
       # pak restart (a modlidbicka take neuskodi:-)
       #
       # Otiskne presne rozdeleni disku do souboru
       echo "   Disk snapshoot"
       /bin/date +%c > $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
       /sbin/fdisk -l >> $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
       echo "** Disk snapshoot ready"
}
rpm() {
       # vytvori chronologicky seznam instalovanych balicku
       echo "   RPMs summary"
       /bin/date +%c > $CESTA/$STROJ-`date +%Y%m%d`/seznam_balicku.txt
       /bin/echo "------------------------------" >> $CESTA/$STROJ-`date +%Y%m%d`/seznam_balicku.txt
       /bin/rpm -qa --last >> $CESTA/$STROJ-`date +%Y%m%d`/seznam_balicku.txt
       echo "** RPMs summary ready"
}
etc() {
       # zkopiruje a zabali do tar/bzip2 cely adresar /etc/*
       echo "   /etc backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/etc.tar.bz2 --ignore-failed-read /etc/ &>/dev/null
       echo "** /etc backup ready"
}
scripts() {
       # zkopiruje a zabali do tar/bzip2 cely adresar /usr/local/bin/*
       echo "   /usr/local/bin backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/scripts.tar.bz2 /usr/local/bin/ &>/dev/null
       echo "** /usr/local/bin backup ready"
}
boot() {
       # zkopiruje a zabali do tar/bzip2 cely adresar /boot/*
       echo "   /boot backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/boot.tar.bz2 --ignore-failed-read /boot/ &>/dev/null
       echo "** /boot backup ready"
}
homes() {
       # zkopiruje a zabali do tar/bzip2 cele domovske adresare
       echo "   /home/uzivatel2 --nastaveni-- backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/uzivatel2_cfg.tar.bz2 --ignore-failed-read --exclude [Cc]ache* --exclude *thumbnail* /home/uzivatel2/.[^.]* &>/dev/null
       echo "** /home/uzivatel2 --nastaveni-- backup ready"
       echo "   /root backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/root.tar.bz2 --ignore-failed-read --exclude [Cc]ache* --exclude *thumbnail* /root/ &>/dev/null
       echo "** /root backup ready"
}
data() {
       # zkopiruje a zabali do tar/bzip2 vsechna data v /home/uzivatel2
       echo "   Zaloha dat hl. uzivatele"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/uzivatel2_data.tar.bz2 --ignore-failed-read --exclude [Ii]nstall* --exclude tmp --exclude [Kk]oš /home/uzivatel2/[^.]* &>/dev/null
       echo "** Zaloha dat hl. uzivatele ready"
}
log() {
       # zaloha /var/log/security/*
       echo "   /var/log/security backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/security-log.tar.bz2 /var/log/security/ &>/dev/null
       echo "** /var/log/security backup ready"
}

transport() {
	# TRANSPORT SERVICE
	/usr/bin/scp $CESTA/$STROJ-`date +%Y%m%d`/* $SBERNA:$SBERNA_CESTA/$STROJ/$STROJ-`date +%Y%m%d`/
}

all() {
       # provede vse vyse uvedene.
  	mbr
	rpm
	etc
  	scripts
  	boot
	homes
	data
	log
	transport
}

# hranate zavorky nahrazuji prikaz test (!test -d /*jestlize neexistuje adresar...*/)
if [ ! -d $CESTA/$STROJ-`date +%Y%m%d`/ ]
  then
    /bin/mkdir $CESTA/$STROJ-`date +%Y%m%d`
fi
case "$1" in
  mbr)
  	mbr
	;;
  rpm)
  	rpm
	;;
  etc)
  	etc
	;;
  scripts)
  	scripts
	;;
  boot)
  	boot
	;;
  homes)
  	homes
	;;
  data)
  	data
	;;
  log)
  	log
	;;
  all)
  	all
	;;
  transport)
  	transport
	;;

  *)
	echo
	echo "             -  BACKUP $STROJ -"
	echo
	echo "Usage: $0 {mbr|rpm|etc|scripts|boot|homes|data|log|all}"
	echo
	echo " * mbr...zaloha MBR"
	echo " * rpm...vytvori chronologicky seznam vsech nainst. balicku"
	echo " * etc...zaloha celeho /etc/"
	echo " * scripts...zaloha /usr/local/bin/"
	echo " * boot...zaloha celeho /boot/"
	echo " * homes...zaloha /root/ a /homes/uzivatel2/-cfg-"
	echo " * data...zaloha dat hl. uzivatele PC"
	echo " * log...zaloha /var/log/security/"
	echo " * transport...transport na sbernou stanici"
	echo " * all...provede vse"
esac


# Zmena prav souboru v "CESTA"
# s * projde jen vse krome hl. adresare. bez ni to udela vcetne nej.	
/bin/find $CESTA/ -print0 | xargs --null /bin/chown $VLASTNIK;
/bin/find $CESTA/* -type d -print0 | xargs --null /bin/chmod $PERM_DIR;
/bin/find $CESTA/* -type f -print0 | xargs --null /bin/chmod $PERM_FILE;

Petr Jakubec © 2004