erlug
[Top] [All Lists]

Re: [Erlug] Controllo delle modifiche al sistema

To: erlug@xxxxxxxxxxxxxx
Subject: Re: [Erlug] Controllo delle modifiche al sistema
From: Davide Bolcioni <db_erlug@xxxxxxxx>
Date: Tue, 25 Feb 2003 22:22:26 +0100
Alessandro Ronchi wrote:

[cut]

Un pò in ritardo, ma a beneficio degli archivi, aggiungo a questa
discussione una bozza di script che faceva una cosa molto simile
basandosi su RPM. Chiedo perdono per la qualità dello stesso, è
una cosa che ho fatto quando ero un pò alle prime armi. Sotto
licenza GPL, come indicato.

Davide Bolcioni
--
Paranoia is a survival asset.
#! /bin/bash
# do-backup
#

# Handcrafted backup script (no arguments)
#
# This file is subject to the terms and conditions of the GNU General Public
# License.  See http://www.gnu.org/copyleft/gpl.html 
# for more details.
#
# Copyright (C) 2001 by Davide Bolcioni


# Configuration: directories pruned from backup.
Prune="/home /root /usr/src/redhat /squid"

# Configuration: files requiring backup.
Backup=""

# Configuration: smbmount options
SmbMountPoint="/mnt/smb-backup"
SmbMountOptions="//host/backup $SmbMountPoint -U utente -P password"

Find="$(which find)"
Rpm="$(which rpm)"
Sort="$(which sort)"
Uniq="$(which uniq)"
Grep="$(which grep)"
Tar="$(which tar)"
Su="$(which su)"
Rm="$(which rm)"
SmbMount="$(which smbmount)"
SmbUmount="$(which smbumount)"
Cp="$(which cp)"
Hostname="$(which hostname)"
Gzip="$(which gzip)"
Basename="$(which basename)"
VarPrune="/var/catman /var/lib /var/lock /var/run /var/spool /var/tmp"
SmbPrune="/etc/codepages"
SysPrune="/mnt /proc /boot /etc/rc.d/rc[0-9].d"
TmpPrune="/tmp /usr/tmp /var/tmp"
AlwaysPrune="$SysPrune $TmpPrune $VarPrune $SmbPrune"

function die()
{
  echo $1; exit 1
}

find=${Find:-/usr/bin/find}
rpm=${Rpm:-/bin/rpm}
sort=${Sort:-/bin/sort}
uniq=${Uniq:-/usr/bin/uniq}
grep=${Grep:-/bin/grep}
tar=${Tar:-/bin/tar}
su=${Su:-/bin/su}
rm=${Rm:-/bin/rm}
smbmount=${SmbMount:-/usr/sbin/smbmount}
smbumount=${SmbUmount:-/usr/sbin/smbumount}
cp=${Cp:-/bin/cp}
hostname=${Hostname:-/bin/hostname}
gzip=${Gzip:-/bin/gzip}
basename=${Basename:-/bin/basename}

prune="$Prune $AlwaysPrune"

test -x "$find" || die "find not available ($find)"
test -x "$rpm" || die "find not available ($rpm)"
test -x "$sort" || die "sort not available ($sort)"
test -x "$uniq" || die "uniq not available ($uniq)"
test -x "$grep" || die "grep not available ($grep)"
test -x "$tar" || die "tar not available ($tar)"
test -x "$su" || die "su not available ($su)"
test -x "$smbmount" || die "smbmount not available ($smbmount)"
test -x "$smbumount" || die "smbumount not available ($smbumount)"
test -x "$cp" || die "cp not available ($cp)"
test -x "$hostname" || die "hostname not available ($hostname)"
test -x "$gzip" || die "gzip not available ($gzip)"
test -x "$basename" || die "basename not available ($basename)"

precauto=$($basename "$0")
tmpfile="/tmp/$precauto-$RANDOM-$$.tar.gz"
rpmlist="/tmp/$precauto-$RANDOM-$$-rpm-list"
test -e "$tmpfile" && die "temporary exists ($tmpfile)"
test -e "$rpmlist" && die "rpm list exists ($rpmlist)"
trap "$su -c '$rm -f $tmpfile $rpmlist' operator" EXIT
backupdir="$SmbMountPoint/precauto"
backupfile="$backupdir/$($hostname -s).tar.gz"

# changed-rpm [prune-paths]
function changed-rpm()
{
  local filter="$grep -v" removed="${1:-$AlwaysPrune}" x
  local changed="$grep ^\(S\|..5\)"

  for x in $removed; do
    if [ "$x" != /var/log ] ; then
      filter="$filter -e ^$x"
    fi
  done
  filter="$filter -e ^/var/log"

  $rpm -V -a | $changed | cut -c 12- | $filter \
  | while read x; do
    test -f "$x" && echo "$x"
  done
}

# non-rpm [prune-paths]
function non-rpm()
{
  local prunep="" filter="$grep -v" removed="${1:-$AlwaysPrune}" x

  for x in $removed; do
    if [ "$x" != /var/log ] ; then
      filter="$filter -e ^$x"
      prunep="${prunep:+$prunep -o }-path $x"
    fi
  done
  filter="$filter -e ^.*/lost+found -e ^/var/log"
  prunep="$prunep -o -name lost+found -o -name quota.user -o -name quota.group"
  prunep="$prunep -o -path /var/log"

  ($rpm -qal | $filter -e "(contains no files)" ; \
   $find / \( $prunep \) -prune -o -print) \
   | $sort | $uniq -u \
   | while read x; do
       test -f "$x" && echo "$x"
     done
}

function backup()
{
  local f

  for f in $*; do echo "$f" ; done
}

function pick()
{
  local x drop="$grep -v"

  drop="$drop -e .*\.lock$ -e .*\.pyc$ -e ^/etc/.*-$ -e .*~$ -e .*\.rpmorig$"
  drop="$drop -e .*/man/whatis$ -e .*\.elc$ -e .*\.bak$ -e .*/\.bash_history$"
  drop="$drop -e /lib/modules/.*/modules\.dep$ -e /etc/ntp/drift$"
  drop="$drop -e /etc/\(HOSTNAME\|mtab\|ioctl\.save\|ld\.so\.cache\|wtmplock\)"
  drop="$drop -e /etc/\(HOSTNAME\|issue\|issue\.net\|psdevtab\)"
  drop="$drop -e /etc/shadow -e /etc/gshadow -e /etc/.*\.mac$"

  $sort -u | $drop | while read x; do
    if [ -L "$x" ] ; then
      case "$x" in
      /lib/lib*.so*)
        ;;
      /usr/lib/lib*.so*)
        ;;
      /usr/*-linux-libc*/lib/lib*.so*)
        ;;
      /usr/X11R6/lib/lib*.so*)
        ;;
      *)
        echo "$x"
        ;;
      esac
    else
      echo "$x"
    fi
  done
}

# Write file as user "operator", obeying noclobber.
$su -c "set -C ; $rpm -qa >$rpmlist" operator

# Read as root, but write as user "operator", obeying noclobber.

(non-rpm "$prune" ; changed-rpm "$prune" ; backup $Backup $rpmlist ) | \
  pick | $tar -c -T - -f - | su -c "set -C ; $gzip > $tmpfile" operator

$smbmount $SmbMountOptions &>/dev/null || die "smbmount ($SmbMountOptions) 
failed"
if [ -d "$backupdir" ] ; then
  $cp -f "$tmpfile" "$backupfile" || \
  die "copy ($tmpfile) to ($backupfile) failed"
else
  die "Directory ($backupdir) not found"
fi
$smbumount "$SmbMountPoint" &>/dev/null
<Prev in Thread] Current Thread [Next in Thread>
  • Re: [Erlug] Controllo delle modifiche al sistema, Davide Bolcioni <=