Skip to content

Instantly share code, notes, and snippets.

@jsfan3
Created January 25, 2020 13:29
Show Gist options
  • Save jsfan3/447693b8bd31c1c465d9e0191d83274e to your computer and use it in GitHub Desktop.
Save jsfan3/447693b8bd31c1c465d9e0191d83274e to your computer and use it in GitHub Desktop.
Script di backup per GNU/Linux, basato su Bash e Rsync. Maggiori informazioni: https://www.informatica-libera.net/content/mybackup-gnulinux-backup-utility-based-rsync-francesco-galgani
#!/bin/bash
#################################################################################
################### MyBACKUP SCRIPT ####################
#################################################################################
# License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
# This is free software: you are free to change and redistribute it.
# There is NO WARRANTY, to the extent permitted by law.
# Autori: Francesco Galgani
VERSIONE="3.2.0"
# Controlla che i programmi richiesti siano installati
if [ -z "$(which zenity)" ]; then
echo "Errore: zenity non è installato"
exit 1
fi
if [ -z "$(which rsync)" ]; then
echo "Errore: rsync non è installato"
exit 1
fi
if [ -z "$(which sha1sum)" ]; then
echo "Errore: sha1sum non è installato"
exit 1
fi
if [ -z "$(which xargs)" ]; then
echo "Errore: xargs non è installato"
exit 1
fi
if [ -z "$(which tee)" ]; then
echo "Errore: tee non è installato"
exit 1
fi
if [ -z "$(which find)" ]; then
echo "Errore: find non è installato"
exit 1
fi
#################################################################################
# Messaggio di benvenuto
zenity --title="MyBACKUP v.$VERSIONE (by Francesco Galgani)" --info --text="MyBACKUP esegue e aggiorna i propri backup ;)"
# VARIABILI SCELTE DALL'UTENTE
SRC="/home/giulio/Scrivania/Archiviazione/"
DEST="/media/giulio/HD-Esterno/Archiviazione"
#################################################################################
# Modifica le stringhe affinché sia correttamente formattate secondo l'uso previsto.
# Rimuove gli eventuali apici iniziali e finali
SRC=${SRC#\'}
SRC=${SRC%\'}
DEST=${DEST#\'}
DEST=${DEST%\'}
# Rimuove gli eventuali doppi apici iniziali e finali
SRC=${SRC#\"}
SRC=${SRC%\"}
DEST=${DEST#\"}
DEST=${DEST%\"}
# Aggiunge uno slash finale in $SRC se manca (necessario per rsync)
SRC=${SRC%/}
SRC=${SRC}/
# L'eventuale slash finale in $DEST deve essere rimosso
DEST=${DEST%\/}
# Controlla che la cartella di origine e di destinazione non siano identiche (?!!)
if [ "`readlink -f $SRC`" = "`readlink -f $DEST`" ]; then
zenity --title="MyBACKUP" --error --text="Errore: la cartella di ORIGINE e la cartella di DESTINAZIONE sono la stessa cartella..."
exit 1
fi
# Messaggio di conferma
zenity --title="Richiesta di conferma" --question --no-wrap --text="Directory principale: $SRC\nDirectory di backup: $DEST\nLe directory sopra indicate sono corrette?"
if [ "$?" -eq 1 ]; then
exit 1
fi
zenity --title="Richiesta di conferma" --question --no-wrap --text="BACKUP DI $SRC IN $DEST\nMyBACKUP fa in modo che tutti i file contenuti in $DEST\ndiventino identici a quelli in $SRC,\neliminando i file superflui e copiando quelli nuovi o modificati.\nQuesto è l'ultimo avviso: vuoi continuare?"
if [ "$?" -eq 1 ]; then
exit 1
fi
if [ -d "$SRC" ];
then {
if [ -d "$DEST" ];
then {
rsync --progress --verbose --archive --delete "$SRC" "$DEST"
if [ "$?" -eq 0 ];
then zenity --title="MyBACKUP" --info --text="Backup completato correttamente"
else zenity --title="MyBACKUP" --error --text="Errore nella sincronizzazione. Il codice di errore di \"rsync\" è \"$?\" (vedi man)."
fi
zenity --title="Richiesta di confronto" --question --no-wrap --text="Vuoi eseguire un confronto approfondito tra la cartella di origine e il backup?\nQuesto controllo generalmente non è necessario e può richiedere molto tempo,\nperò è utile per verificare la completa affidabilità del backup.\nATTENZIONE: VERRA' SVUOTATA LA FILE CACHE, ASSICURATI CHE NON CI SIANO ALTRE OPERAZIONI SUI FILES IN CORSO."
if [ "$?" -eq 1 ]; then
exit 1
fi
#Svuotamento cache
echo "Stato della memoria prima della pulizia:"
free
echo ""
echo "Cancello la file cache..."
sync
echo 3 > /proc/sys/vm/drop_caches
#sysctl vm.drop_caches=3
echo "OK. Cache svuotata."
echo ""
echo "Stato della memoria dopo la pulizia:"
free
echo "Creazione di un checksum completo di ciascun file di origine..."
cd "$SRC"
SHAFile=".MyBackup_SHASUMS_`date +%Y_%m_%d_%H_%M_%S`"
LOGfile="MyBackup_Log_`date +%Y_%m_%d_%H_%M_%S`"
rm -f "$SRC/$SHAFile"
rm -f "$DEST/$SHAFile"
find . -type f -print0 | xargs -0 sha1sum | tee "$DEST/$SHAFile"
echo "Controllo di ciascun file di backup tramite il checksum..."
cd "$DEST"
sha1sum -c $SHAFile | tee "$DEST/$LOGfile"
if [ "${PIPESTATUS[0]}" -eq 0 ];
then zenity --title="MyBACKUP" --info --text="Controllo completato: la cartella di origine e il backup sono identici"
else zenity --title="MyBACKUP" --error --text="Uno o più errori riscontrati... nella cartella di destinazione è riportato il file di log"
fi
cd ..
}
else zenity --title="MyBACKUP" --error --text="Errore: la cartella di destinazione \"$DEST\" non esiste."
fi
}
else zenity --title="MyBACKUP" --error --text="Errore: la cartella di origine \"$SRC\" non esiste."
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment