Dump-Backupskript

Aus ConfigWiki
Version vom 19. Dezember 2011, 23:51 Uhr von Thomasm (Diskussion | Beiträge)

(Unterschied) ← Nächstältere Version | Aktuelle Version (Unterschied) | Nächstjüngere Version → (Unterschied)
Wechseln zu: Navigation, Suche

Enthält ein paar interessante Hacks.

 <source lang="bash">
#!/bin/bash
# Funktionen
function backup_age() {
  LEVEL=$1
  if ! test -f /tmp/backups/home/$LEVEL; then
       return 1
  fi
  LAST_DUMP=$(file /tmp/backups/home/$LEVEL \
       | grep -o "... ... .. ..:..:.. ...." | head -1)
  LAST_DUMP_EPOCH=$(date +%s -d "$LAST_DUMP")
  NOW_EPOCH=$(date +%s)
  echo $((NOW_EPOCH - LAST_DUMP_EPOCH))
}
function cleanup() {
  LEVEL=$1
  rm -f /tmp/backups/home/*.tmp
  for file in /tmp/backups/home/*
  do
       base=$(basename $file)
       test "$base" = "*" && break
       test "$base" -le $LEVEL && continue
       rm $file
  done
}

# Programm
if ! test $(id -u) = 0; then
  echo "$0: Keine Root-Rechte!" >&2
  exit 1
fi
if ! backup_age 0 \
  || test $(backup_age 0) -gt 604800; then
  dump -u -0 -f /tmp/backups/home/0.tmp /home \
  && mv /tmp/backups/home/0{.tmp,}
  cleanup 0
elif ! backup_age 1 \
  || test $(backup_age 1) -gt 86400; then
  dump -u -1 -f /tmp/backups/home/1.tmp /home \
  && mv /tmp/backups/home/1{.tmp,}
  cleanup 1
else
  dump -u -2 -f /tmp/backups/home/2.tmp /home \
  && mv /tmp/backups/home/2{.tmp,}
  cleanup 2
fi
</source>
Meine Werkzeuge