11.04.2013 Views

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

Guida avanzata di scripting Bash - Portale Posta DMI

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Appen<strong>di</strong>ce A. Script aggiuntivi<br />

# Lo scrip salva i weblog <strong>di</strong> un’istallazione <strong>di</strong> default RedHat/Apache<br />

#+ che normalmente vengono ruotati ed eliminati.<br />

# I file verranno salvati in una <strong>di</strong>rectory data con data/ora inserita<br />

#+ nel nome del file, e compressi con bzip2.<br />

#<br />

# Lo scrip va eseguito da crontab a notte fonda,<br />

#+ perchè bzip2 è particolarmente avido <strong>di</strong> risorse <strong>di</strong> CPU:<br />

# 0 2 * * * /opt/sbin/archiveweblogs.sh<br />

PROBLEMA=66<br />

# Impostatela alla vostra <strong>di</strong>rectory <strong>di</strong> backup.<br />

BKP_DIR=/opt/backups/weblogs<br />

# Impostazioni <strong>di</strong> default Apache/RedHat<br />

LOG_DAYS="4 3 2 1"<br />

LOG_DIR=/var/log/httpd<br />

LOG_FILES="access_log error_log"<br />

# Collocazione <strong>di</strong> default dei programmi in RedHat<br />

LS=/bin/ls<br />

MV=/bin/mv<br />

ID=/usr/bin/id<br />

CUT=/bin/cut<br />

COL=/usr/bin/column<br />

BZ2=/usr/bin/bzip2<br />

# Siete root?<br />

UTENTE=‘$ID -u‘<br />

if [ "X$UTENTE" != "X0" ]; then<br />

echo "PANIC: solo root può eseguire lo script!"<br />

exit $PROBLEMA<br />

fi<br />

# La <strong>di</strong>rectory <strong>di</strong> backup esiste/ha i permessi <strong>di</strong> scrittura?<br />

if [ ! -x $BKP_DIR ]; then<br />

echo "PANIC: $BKP_DIR non esiste o non ha i permessi <strong>di</strong> scrittura!"<br />

exit $PROBLEMA<br />

fi<br />

# Sposta, rinomina e comprime con bzip2 i log<br />

for logday in $LOG_DAYS; do<br />

for logfile in $LOG_FILES; do<br />

MIOFILE="$LOG_DIR/$logfile.$logday"<br />

if [ -w $MIOFILE ]; then<br />

DTS=‘$LS -lgo --time-style=+%Y%m%d $MIOFILE | $COL -t | $CUT -d ’ ’ -f7‘<br />

$MV $MIOFILE $BKP_DIR/$logfile.$DTS<br />

$BZ2 $BKP_DIR/$logfile.$DTS<br />

else<br />

# L’errore viene visualizzato solo se il file esiste<br />

#+ (ergo: non ha i permessi <strong>di</strong> scrittura).<br />

if [ -f $MIOFILE ]; then<br />

664

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!