Backup sběrna


#!/bin/bash
# Description: Kompletni zalohovani dulezitych souboru
# Author: DotaZ
# Date: 2004-02-07
# Version: Release 0.91
# Destination: for SBERNA
#
# POZN:
# 2004-09-18 rozdeleni zalohy /home/uzivatel1 na docu + data, 
#  pridan do vsech exclude patricny Temp,Tmp
# 2004-10-03 uprava find (tip by Boris Filipovic v mandrake@mandrake.cz - diky!)
#  uprava komentaru
#  uprava disksnapu pomoci fdisku.
# 2004-02-07 opraveny drobne chybicky kolem ADR_STANICE
# 2004-01-18 pridano EXCLUDE jako promenna pro vyssi konfort
# 2003-11-29 upravy vlastnika a pridano CESTA_ALL 
#  (nastavi prava pro kompletne cely adr. backup)
#  drobne upravy na "disk snapshoot"

# jmeno stroje/adresare ve kterem udela zalohu
STROJ="sberna"
# cesta kam ma zalohovat
CESTA="/var/backup/sberna"
CESTA2="/var/backup/sberna/sberna-uzivatel1"
CESTA_ALL="/var/backup"

# seznam exclude adresaru:
# zaloha vsech dat v /home/uzivatel1/* bez techto uvedenych adresaru:
EXCLUDEDATA="--exclude [Kk]oš --exclude [Dd]ocuments --exclude [Tt]mp"
# zaloha vsech dat v /home/uzivatel1/.* --nastaveni-- bez techto uvedenych adresaru:
EXCLUDECFG="--exclude [Cc]ache* --exclude *thumbnail* --exclude .[Tt]mp"
# pozadovana prava/vlastnistvi vzniklych zaloh
VLASTNIK="uzivatel1:uzivatel1"
PERM_FILE="600"
PERM_DIR="700"
# adresare pro ostatni stroje:
ADR_STANICE="/var/backup/stanice/stanice-`date +%Y%m%d`/"


# Vytvoreni adresare pro ostatni stroje.
# STANICE:
if [ ! -d $ADR_STANICE ]
  then
    /bin/mkdir $ADR_STANICE &>/dev/null
    /bin/chown $VLASTNIK $ADR_STANICE
    /bin/chmod $PERM_DIR $ADR_STANICE
    echo " * Vytvoreny adresare pro ostatni uzivatele"
    echo
fi

mbr() {
    # MBR backup :: "rucne" 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:-)
        #
    # FDISK summary :: Otiskne presne rozdeleni disku do souboru
       echo "   Disk snapshoot"
        #       /sbin/fdisk -l /dev/hd* &> $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
        #echo >> $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
        #echo " ==============  Disk snapshoot timestamp ==================" >> $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
        #echo "           `date +%c`" >> $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
       /bin/date +%c >> $CESTA/$STROJ-`date +%Y%m%d`/fdisk_snapshoot.txt
       echo " ====== Fdisk start here ======" >> $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() {
    # RPM summary :: 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() {
    # ETC backup :: 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() {
    # SCRIPTS backup :: 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() {
    # BOOT backup :: 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() {
    # DOTAZ backup :: zkopiruje a zabali do tar/bzip2 cele domovske adresare
       echo "   /home/uzivatel1 --nastaveni-- backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/uzivatel1_cfg.tar.bz2 --ignore-failed-read $EXCLUDECFG /home/uzivatel1/.[^.]* &>/dev/null
       echo " * /home/uzivatel1 --nastaveni-- backup ready"
       echo "   /root backup"
       /bin/tar -cjf $CESTA/$STROJ-`date +%Y%m%d`/root.tar.bz2 --ignore-failed-read /root/ &>/dev/null
       echo " * /root backup ready"
}
data() {
    # HOME backup :: zkopiruje a zabali do tar/gzip vsechna data v /home/uzivatel1
       echo "   Zaloha dat hl. uzivatele"
       /bin/rm -fv $CESTA2/uzivatel1_data.tar.gz &>/dev/null
       /bin/tar -czf $CESTA2/uzivatel1_data.tar.gz --ignore-failed-read $EXCLUDEDATA /home/uzivatel1/[^.]* &>/dev/null
       echo " * Zaloha dat hl. uzivatele ready"
}
docu() {
    # DOCUMENT backup :: zkopiruje a zabali do tar/gzip vsechna data v /home/uzivatel1/Documents
       echo "   /documents backup"
       /bin/rm -fv $CESTA2/uzivatel1_docu.tar.gz &>/dev/null
       /bin/tar -czf $CESTA2/uzivatel1_docu.tar.gz --ignore-failed-read /home/uzivatel1/Documents/ &>/dev/null
       echo " * /documents backup ready"
}
log() {
    # SECURITY backup :: /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"
}
all() {
    # provede vse nize uvedene.
  	mbr
	etc
  	scripts
  	boot
	homes
	rpm
	log
}
all-uzivatel1() {
    # provede vse nize uvedene.
  	data
	docu
}

# hranate zavorky nahrazuji prikaz test (test -d /*je to adresar?*/)
if [ ! -d $CESTA/$STROJ-`date +%Y%m%d`/ ]
  then
    /bin/mkdir $CESTA/$STROJ-`date +%Y%m%d` &>/dev/null
fi

case "$1" in
  mbr)
  	mbr
	;;
  rpm)
  	rpm
	;;
  etc)
  	etc
	;;
  scripts)
  	scripts
	;;
  boot)
  	boot
	;;
  homes)
  	homes
	;;
  data)
  	data
	;;
  docu)
  	docu
	;;
  log)
  	log
	;;
  all)
  	all
	;;
  all-uzivatel1)
  	all-uzivatel1
	;;
  *)
	echo
	echo "                  - BACKUP $STROJ -"
	echo
	echo "Pouziti|volby: $0:"
	echo "  {mbr|rpm|etc|scripts|boot|homes|data|docu|log|all|all-uzivatel1}"
	echo
	echo " * mbr...zaloha MBR a otisku fs disku"
	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 obv. nastaveni hl. uzivatele PC"
	echo " * data...zaloha dat hl. uzivatele PC"
	echo " * docu...zaloha adr. Documents hl. uzivatele PC"
	echo " * log......zaloha /var/log/security"
	echo "                 ---"
	echo " * all...provede vse krome data a docu"
	echo " * all-uzivatel1...provede zalohu data a docu"
esac
# Zmena prav souboru v "CESTA_ALL"
# s * projde jen vse krome hl. adresare. bez ni to udela vcetne nej.	
/bin/find $CESTA_ALL/ -print0 | xargs --null /bin/chown $VLASTNIK;
/bin/find $CESTA_ALL/* -type d -print0 | xargs --null /bin/chmod $PERM_DIR;
/bin/find $CESTA_ALL/* -type f -print0 | xargs --null /bin/chmod $PERM_FILE;
Petr Jakubec © 2004