Backup-Script

Aus Info-Theke
Version vom 15. Dezember 2012, 09:47 Uhr von Hamatoma (Diskussion | Beiträge)
(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Zur Navigation springen Zur Suche springen

/etc/anacrontab

# These replace cron's entries
# format:
# mode delay label command
# mode:1: dayly 7: weekly 30: monthly N: any N days

1       5       cron.daily      run-parts --report /etc/cron.daily
7       10      cron.weekly     run-parts --report /etc/cron.weekly
@monthly        15      cron.monthly    run-parts --report /etc/cron.monthly
1       3       backup.dayly    /home/bin/torro/torro-backup.sh dayly
7       4       backup.weekly   /home/bin/torro/torro-backup.sh weekly
30      4       backup.monthly  /home/bin/torro/torro-backup.sh monthly

Backup-Script torro-backup.sh:

#! /bin/bash

LOG=/var/log/torro_bup.log

function BackUp()
{
	SRC=$1
	TRG=$2
	OPT="$3"
	NODE=$(basename $SRC)
	if [ -d $TRG/$NODE ] ; then
		date "+%Y.%m.%d %H:%M start $SRC -> $TRG" >>/var/log/torro_bup.log 
		rsync -ru $OPT $SRC $TRG
		date "+%Y.%m.%d %H:%M end $SRC" >>/var/log/torro_bup.log
	fi
}
function MkSnapshot()
{
  PLACEHOLDER=$1
  DEV=$2
  PARENT=$3
  
  NAME="${PARENT}-$(date +$PLACEHOLDER)"
  FULL=/dev/$DEV/$VG/$NAME
  FOUND=$(lvdisplay $FULL 2>/dev/null| grep "LV Name")
  echo $NAME

  if [ -n "$FOUND" ] ; then
    FOUND=$(lvdisplay $FULL | grep "read only")
    if [ -z "$FOUND" ] ; then
      echo "Not a read only devide: $FULL Aborting"
      return
    else
      if [ -z "$SILENT" ] ; then
		lvremove -f $FULL
      else
        lvremove -f $FULL  >/dev/null
      fi
    fi
  fi
  if [ -z "$SILENT" ] ; then
    lvcreate -s -pr -L 128M -n$NAME /dev/$DEV/$VG/$PARENT
  else
    lvcreate -s -pr -L 128M -n$NAME /dev/$DEV/$VG/$PARENT >/dev/null
  fi
  date "+%Y.%m.%d %H:%M snapshot $NAME" >>$LOG
}

SILENT=$2

case "$1" in
hourly)
	BackUp /home /media/data/backup "--delete $2"
	;;
hourly2)
	mount /media/bup_data
	BackUp /media/data /media/bup-data "--delete $2"
	;;
dayly)
	MkSnapshot "%a" torro320 data
	MkSnapshot "%a" torro250 bup-data "%a"
	;;
weekly)
	MkSnapshot "week-%V" torro320 data 
	MkSnapshot "week-%V" torro250 bup-data
	MkSnapshot "week-%V" torro250 opt
	;;
monthly)
	NAME=$(date "+data-%b")
	MkSnapshot "%b" torro320 data
	MkSnapshot "%b" torro250 bup-data
	;;
*)
	echo "usage: torro-backup.sh { hourly | hourly2 | dayly | weekly | monthly }"
	;;
esac