Minor improvements

If no input is found/selected, the programs exits successfully but
prints a warning.
Log files management improved
This commit is contained in:
daniele 2015-11-16 00:02:46 +01:00
parent 9d8bd7ae3f
commit 3a31e340cc

View File

@ -15,7 +15,7 @@
#You should have received a copy of the GNU General Public License #You should have received a copy of the GNU General Public License
#along with this program. If not, see <http://www.gnu.org/licenses/>. #along with this program. If not, see <http://www.gnu.org/licenses/>.
#Version 1.0.0 #Version 1.0.1
#Simple backup script. Reads options, sources and destination from a configuration file or standard input #Simple backup script. Reads options, sources and destination from a configuration file or standard input
#Help function #Help function
@ -37,7 +37,7 @@ function help_function {
echo " Default: keep all." echo " Default: keep all."
echo "" echo ""
echo "If no option is given, the program uses the default" echo "If no option is given, the program uses the default"
echo "configuration file: HOME/.simple_backup/config." echo "configuration file: $HOME/.simple_backup/config."
echo "" echo ""
echo "Report bugs to dfucini@gmail.com" echo "Report bugs to dfucini@gmail.com"
exit 0 exit 0
@ -56,6 +56,19 @@ function read_conf {
echo "Error: Configuration file not found" | tee -a $HOME/.simple_backup/errors.log echo "Error: Configuration file not found" | tee -a $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
fi fi
@ -118,6 +131,19 @@ function read_conf {
echo "Error: No backup folder set in configuration file" | tee -a $HOME/.simple_backup/errors.log echo "Error: No backup folder set in configuration file" | tee -a $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
fi fi
@ -127,6 +153,19 @@ function read_conf {
echo "Error: Output folder \"$BACKUP_DEV\" not found" | tee -a $HOME/.simple_backup/errors.log echo "Error: Output folder \"$BACKUP_DEV\" not found" | tee -a $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
fi fi
@ -200,6 +239,19 @@ function parse_options {
case "$var" in case "$var" in
-h | --help) -h | --help)
help_function help_function
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
mv -f $HOME/.simple_backup/simple_backup.log.old $HOME/.simple_backup/simple_backup.log
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
mv -f $HOME/.simple_backup/errors.log.old $HOME/.simple_backup/errors.log
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
mv -f $HOME/.simple_backup/warnings.log.old $HOME/.simple_backup/warnings.log
fi
exit 0 exit 0
;; ;;
@ -213,6 +265,19 @@ function parse_options {
echo "Error: bad options format" | tee -a $HOME/.simple_backup/errors.log echo "Error: bad options format" | tee -a $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
fi fi
@ -226,14 +291,6 @@ function parse_options {
shift shift
done done
if [[ $n_in -eq 0 ]]; then
echo "$(date): Backup finished (no files copied)" >> $HOME/.simple_backup/simple_backup.log
echo "Warning: no valid input selected. Nothing to do" | tee -a $HOME/.simple_backup/warnings.log
#If libnotify is installed, show desktop notification that backup finished
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed (warnings)"
exit 0
fi
;; ;;
-d | --directory) -d | --directory)
@ -244,6 +301,19 @@ function parse_options {
echo "Error: output folder \"$BACKUP_DEV\" not found" | tee -a $HOME/.simple_backup/errors.log echo "Error: output folder \"$BACKUP_DEV\" not found" | tee -a $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
fi fi
@ -290,6 +360,19 @@ function parse_options {
echo "Error: Option $1 not recognised. Use 'simple-backup -h' to see available options" | tee -a $HOME/.simple_backup/errors.log echo "Error: Option $1 not recognised. Use 'simple-backup -h' to see available options" | tee -a $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
;; ;;
esac esac
@ -315,15 +398,15 @@ fi
#Backup old log files #Backup old log files
if [[ -f $HOME/.simple_backup/simple_backup.log ]]; then if [[ -f $HOME/.simple_backup/simple_backup.log ]]; then
mv $HOME/.simple_backup/simple_backup.log $HOME/.simple_backup/simple_backup.log.old mv -f $HOME/.simple_backup/simple_backup.log $HOME/.simple_backup/simple_backup.log.old
fi fi
if [[ -f $HOME/.simple_backup/errors.log ]]; then if [[ -f $HOME/.simple_backup/errors.log ]]; then
mv $HOME/.simple_backup/errors.log $HOME/.simple_backup/errors.log.old mv -f $HOME/.simple_backup/errors.log $HOME/.simple_backup/errors.log.old
fi fi
if [[ -f $HOME/.simple_backup/warnings.log ]]; then if [[ -f $HOME/.simple_backup/warnings.log ]]; then
mv $HOME/.simple_backup/warnings.log $HOME/.simple_backup/warnings.log.old mv -f $HOME/.simple_backup/warnings.log $HOME/.simple_backup/warnings.log.old
fi fi
#If no input parameter is given, check existence of config file #If no input parameter is given, check existence of config file
@ -333,6 +416,19 @@ if [[ $default_config -eq 1 && ! -f $HOME/.simple_backup/config ]]; then
echo "Error: Configuration file not found" | tee $HOME/.simple_backup/errors.log echo "Error: Configuration file not found" | tee $HOME/.simple_backup/errors.log
#If libnotify is installed, show desktop notification that backup failed #If libnotify is installed, show desktop notification that backup failed
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup failed"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 1 exit 1
elif [[ $default_config -eq 1 && -f $HOME/.simple_backup/config ]]; then elif [[ $default_config -eq 1 && -f $HOME/.simple_backup/config ]]; then
#Read configuration file #Read configuration file
@ -342,6 +438,27 @@ else
parse_options "$@" parse_options "$@"
fi fi
if [[ -z $n_in || $n_in -eq 0 ]]; then
echo "$(date): Backup finished (no files copied)" >> $HOME/.simple_backup/simple_backup.log
echo "Warning: no valid input selected. Nothing to do" | tee -a $HOME/.simple_backup/warnings.log
#If libnotify is installed, show desktop notification that backup finished
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished (warnings)"
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 0
fi
echo "$(date): Starting backup" > $HOME/.simple_backup/simple_backup.log echo "$(date): Starting backup" > $HOME/.simple_backup/simple_backup.log
#If libnotify is installed, show desktop notification that backup is starting #If libnotify is installed, show desktop notification that backup is starting
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Starting backup" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Starting backup"
@ -411,4 +528,16 @@ else
! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished (warnings)" ! command -v notify-send > /dev/null 2>&1 || DISPLAY=:0.0 notify-send -u low -t 10000 "Backup finished (warnings)"
fi fi
if [[ -f $HOME/.simple_backup/simple_backup.log.old ]]; then
rm -f $HOME/.simple_backup/simple_backup.log.old
fi
if [[ -f $HOME/.simple_backup/errors.log.old ]]; then
rm -f $HOME/.simple_backup/errors.log.old
fi
if [[ -f $HOME/.simple_backup/warnings.log.old ]]; then
rm -f $HOME/.simple_backup/warnings.log.old
fi
exit 0 exit 0