Script shell
One of the simplest ways to backup a system is using a shell script. For example, a script can be used to configure which directories to backup, and pass those directories as arguments to the tar utility, which creates an archive file. The archive file can then be moved or copied to another location. The archive can also be created on a remote file system such as an NFS mount.
The tar utility creates one archive file out of many files or directories. tar can also filter the files through compression utilities, thus reducing the size of the archive file.
Semplice script shell
Il seguente script utilizza tar per creare un archivio su un file system remoto NFS. Il nome dell'archivio è determinato utilizzando delle utilità a riga di comando aggiuntive.
#!/bin/sh #################################### # # Backup to NFS mount script. # #################################### # What to backup. backup_files="/home /var/spool/mail /etc /root /boot /opt" # Where to backup to. dest="/mnt/backup" # Create archive filename. day=$(date +%A) hostname=$(hostname -s) archive_file="$hostname-$day.tgz" # Print start status message. echo "Backing up $backup_files to $dest/$archive_file" date echo # Backup the files using tar. tar czf $dest/$archive_file $backup_files # Print end status message. echo echo "Backup finished" date # Long listing of files in $dest to check file sizes. ls -lh $dest
-
$backup_files: una variabile con le directory di cui si vuole fare una copia. L'elenco va modificato come desiderato.
-
$day: a variable holding the day of the week (Monday, Tuesday, Wednesday, etc). This is used to create an archive file for each day of the week, giving a backup history of seven days. There are other ways to accomplish this including using the date utility.
-
$hostname: variabile contenente il nome host breve del sistema. Usare il nome dell'host nel nome dell'archivio, consente di avere backup giornalieri di diversi sistemi in una sola directory.
-
$archive_file: il nome completo dell'archivio.
-
$dest: destination of the archive file. The directory needs to be created and in this case mounted before executing the backup script. See NFS (Network File System) for details of using NFS.
-
messaggi: messaggi opzionali stampati sulla console usando echo.
-
tar czf $dest/$archive_file $backup_files: il comando tar usato per creare l'archivio.
-
c: crea l'archivio.
-
z: passa l'archivio attraverso l'utilità di compressione gzip.
-
f: output to an archive file. Otherwise the tar output will be sent to STDOUT.
-
-
ls -lh $dest: istruzione opzionale che stampa un elenco lungo (-l) in un formato leggibile (-h) della directory di destinazione. È utile per controllare la dimensione dell'archivo. Questa verifica non dovrebbe sostituire la verifica dell'archivio.
This is a simple example of a backup shell script; however there are many options that can be included in such a script. See Riferimenti for links to resources providing more in-depth shell scripting information.
Eseguire lo script
Esecuzione da terminale
Il metodo più facile per eseguire lo script di backup è quello di copiare il contenuto dello script in un file, backup.sh per esempio, ed eseguirlo in un terminale:
sudo bash backup.sh
È un ottimo modo per provare lo script e assicurarsi che funzioni correttamente.
Esecuzione con cron
L'utilità cron può essere usata per automatizzare l'esecuzione dello script. Il demone cron consente l'esecuzione di script o comandi a un determinato orario e data.
L'applicazione cron è configurata attraverso delle voci in un file crontab file. I file crontab sono separati in campi:
# m h dom mon dow comando
-
m: minute the command executes on, between 0 and 59.
-
h: hour the command executes on, between 0 and 23.
-
dom: giorno del mese di esecuzione del comando.
-
mon: the month the command executes on, between 1 and 12.
-
dow: the day of the week the command executes on, between 0 and 7. Sunday may be specified by using 0 or 7, both values are valid.
-
comando: il comando da eseguire.
Per aggiungere o modificare voci in un file crontab, dovrebbe essere usato il comando crontab -e, i contenuti di un file crontab possono essere visualizzati usando il comando crontab -l.
Per eseguire lo script backup.sh usando cron, in un terminale digitare quanto segue:
sudo crontab -e
Usare sudo con il comando crontab -e, modifica il crontab dell'utente root. Questo è necessario nel caso in cui si stiano eseguendo copie di backup di file accessibili solo dall'utente root.
Aggiungere quanto segue al file crontab:
# m h dom mon dow command 0 0 * * * bash /usr/local/bin/backup.sh
Lo script backup.sh verrà eseguito ogni giorno alle 12.00 AM.
The backup.sh script will need to be copied to the /usr/local/bin/ directory in order for this entry to execute properly. The script can reside anywhere on the file system, simply change the script path appropriately.
For more in-depth crontab options see Riferimenti.
Ripristinare l'archivio
Una volta creato un archivio, è importante verificarlo, elencandone i contenuti oppure, ed è la scelta migliore, ripristinare un file dall'archivio.
-
To see a listing of the archive contents. From a terminal prompt type:
tar -tzvf /mnt/backup/host-lunedì.tgz
-
Per ripristinare un file dall'archivio in una directory diversa, digitare:
tar -xzvf /mnt/backup/host-lunedì.tgz -C /tmp etc/hosts
L'opzione -C di tar redirige i file estratti nella directory specificata. L'esempio precedente estrarrà il file /etc/hosts in /tmp/etc/hosts. La struttura della directory viene quindi ricreata da tar.
Notare anche che il simbolo "/" iniziale del percorso in cui ripristinare è stato tralasciato.
-
Per ripristinare tutti i file presenti nell'archivio, digitare:
cd / sudo tar -xzvf /mnt/backup/host-lunedì.tgz
In questo modo verranno sovrascritti i file attualmente presenti nel file system.
Riferimenti
-
Per maggiori informazioni riguardo lo script da shell, consultare la Advanced Bash-Scripting Guide
-
Il libro Teach Yourself Shell Programming in 24 Hours è disponibile in linea ed è un'ottima risorsa per lo script da shell.
-
La pagina della della documentazione in linea su cron contiene ulteriori dettagli sulle opzioni avanzate di cron.
-
Per maggiori informazioni sulle opzioni del comando tar, consultare il manuale in linea di tar.
-
La pagina inglese di Wikipedia Backup Rotation Scheme contiene informazioni sugli schemi di backup.
-
Questo script utilizza tar per creare l'archivio, ma esistono diverse altre utilità a riga di comando che possono essere usate, per esempio:
-
cpio: usata per copiare file da e verso degli archivi.
-
dd: part of the coreutils package. A low level utility that can copy data from one format to another.
-
rsnapshot: a file system snapshot utility used to create copies of an entire file system.
-
rsync: a flexible utility used to create incremental copies of files.
-