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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Infine, un esempio concreto <strong>di</strong> come usare il - con tar.<br />

Esempio 3-4. Backup <strong>di</strong> tutti i file mo<strong>di</strong>ficati il giorno precedente<br />

#!/bin/bash<br />

Capitolo 3. Caratteri speciali<br />

# Salvataggio <strong>di</strong> tutti i file della <strong>di</strong>rectory corrente che sono stati<br />

#+ mo<strong>di</strong>ficati nelle ultime 24 ore in un archivio "tarball" (file trattato<br />

#+ con tar e gzip).<br />

FILEBACKUP=backup-$(date +%d-%m-%Y)<br />

# Inserisce la data nel nome del file <strong>di</strong> salvataggio.<br />

# Grazie a Joshua Tschida per l’idea.<br />

archivio=${1:-$FILEBACKUP}<br />

# Se non viene specificato un nome <strong>di</strong> file d’archivio da riga <strong>di</strong> comando,<br />

#+ questo verrà impostato a "backup-GG-MM-AAAA.tar.gz."<br />

tar cvf - ‘find . -mtime -1 -type f -print‘ > $archivio.tar<br />

gzip $archivio.tar<br />

echo "Directory $PWD salvata nel file \"$archivio.tar.gz\"."<br />

# Stephane Chazelas evidenzia che il precedente co<strong>di</strong>ce fallisce l’esecuzione<br />

#+ se incontra troppi file o se un qualsiasi nome <strong>di</strong> file contiene caratteri<br />

#+ <strong>di</strong> spaziatura.<br />

# Suggerisce, quin<strong>di</strong>, le seguenti alternative:<br />

# -------------------------------------------------------------------<br />

# find . -mtime -1 -type f -print0 | xargs -0 tar rvf "$archivio.tar"<br />

# utilizzando la versione GNU <strong>di</strong> "find".<br />

# find . -mtime -1 -type f -exec tar rvf "$archivio.tar" ’{}’ \;<br />

# portabile su altre versioni UNIX, ma molto più lento.<br />

# -------------------------------------------------------------------<br />

exit 0<br />

29

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

Saved successfully!

Ooh no, something went wrong!