Improve code

Inputs are saved in temporary file instead of being stored in array
and are read by rsync with the --files-from option
This commit is contained in:
daniele 2015-11-20 12:20:49 +01:00
parent 3a31e340cc
commit b78b231227

View File

@ -86,13 +86,15 @@ function read_conf {
case "$var" in case "$var" in
#Files/folders to backup #Files/folders to backup
inputs) inputs)
#Create a temporary file to store inputs
INPUTS=$(mktemp)
tmp=$(echo "$line" | cut -d"=" -f2) tmp=$(echo "$line" | cut -d"=" -f2)
n=$(echo "$tmp" | awk -F ',' '{print NF}') #Files/folders must be separated with commas n=$(echo "$tmp" | awk -F ',' '{print NF}') #Files/folders must be separated with commas
i=1 i=1
j=1 #j=1
n_in=0 n_in=0
#Read each input and save it in an array #Read each input and save it to the INPUTS file
while [[ $i -le $n ]] while [[ $i -le $n ]]
do do
input=$(echo "$tmp" | cut -d"," -f$i) input=$(echo "$tmp" | cut -d"," -f$i)
@ -102,18 +104,18 @@ function read_conf {
input=$(echo ${input/\~/$HOME}) input=$(echo ${input/\~/$HOME})
fi fi
INPUTS[$j]=$input if [[ ! -e "$input" ]]; then
#Warn the user if an input doesn't exist
if [[ ! -e "${INPUTS[$j]}" ]]; then echo "Warning: input \"$input\" not found. Skipping" | tee -a $HOME/.simple_backup/warning.log
#Warn the user if an input doesn't exists
echo "Warning: input \"${INPUTS[$j]}\" not found. Skipping" | tee -a $HOME/.simple_backup/warnings.log
else else
j=$((j+1))
n_in=$((n_in+1)) n_in=$((n_in+1))
fi fi
echo "$input" >> $INPUTS
i=$((i+1)) i=$((i+1))
done done
;; ;;
#Directory where the backup is saved #Directory where the backup is saved
@ -256,11 +258,14 @@ function parse_options {
;; ;;
-i | --input) -i | --input)
#Create a temporary file to store inputs
INPUTS=$(mktemp)
while [[ "$#" -gt 1 && ! "$2" =~ ^- ]] while [[ "$#" -gt 1 && ! "$2" =~ ^- ]]
do do
INPUTS[$i]="$2" input="$2"
if [[ -z "${INPUTS[$i]}" ]]; then if [[ -z "$input" ]]; then
echo "$(date): Backup failed (see errors.log)" >> $HOME/.simple_backup/simple_backup.log echo "$(date): Backup failed (see errors.log)" >> $HOME/.simple_backup/simple_backup.log
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
@ -281,11 +286,12 @@ function parse_options {
exit 1 exit 1
fi fi
if [[ ! -e "${INPUTS[$i]}" ]]; then if [[ ! -e "$input" ]]; then
echo "Warning: input \"${INPUTS[$i]}\" not found. Skipping" | tee -a $HOME/.simple_backup/warnings.log echo "Warning: input \"${INPUTS[$i]}\" not found. Skipping" | tee -a $HOME/.simple_backup/warnings.log
else else
i=$((i+1)) i=$((i+1))
n_in=$((n_in+1)) n_in=$((n_in+1))
echo "$input" >> "$INPUTS"
fi fi
shift shift
@ -484,18 +490,16 @@ if [[ -n $KEEP ]]; then
fi fi
i=1 # Sort input files for rsync efficiency
if [[ ! -z "$INPUTS" ]]; then
sort "$INPUTS" -o "$INPUTS"
fi
#Run rsync for each input if [[ -z "$LAST_BACKUP" ]]; then
while [ $i -le $n_in ] rsync -acrv -H -X -R --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" >> "$HOME/.simple_backup/simple_backup.log" 2>> "$HOME/.simple_backup/errors.log"
do else
if [[ -z "$LAST_BACKUP" ]]; then rsync -acrv -H -X -R --link-dest="$LAST_BACKUP" --exclude-from="$EXCLUDE" --files-from="$INPUTS" / "$BACKUP_DIR" >> "$HOME/.simple_backup/simple_backup.log" 2>> "$HOME/.simple_backup/errors.log"
rsync -acv -H -X -R --exclude-from "$EXCLUDE" "${INPUTS[$i]}" "$BACKUP_DIR" >> "$HOME/.simple_backup/simple_backup.log" 2>> "$HOME/.simple_backup/errors.log" fi
else
rsync -acv -H -X -R --link-dest="$LAST_BACKUP" --exclude-from "$EXCLUDE" "${INPUTS[$i]}" "$BACKUP_DIR" >> "$HOME/.simple_backup/simple_backup.log" 2>> "$HOME/.simple_backup/errors.log"
fi
i=$((i+1))
done
#Update the logs #Update the logs
if [[ -f $HOME/.simple_backup/errors.log && $(cat $HOME/.simple_backup/errors.log | wc -l) -gt 0 ]]; then if [[ -f $HOME/.simple_backup/errors.log && $(cat $HOME/.simple_backup/errors.log | wc -l) -gt 0 ]]; then
@ -509,8 +513,12 @@ else
error_flag=0 error_flag=0
fi fi
if [[ ! -z $EXCLUDE ]]; then if [[ ! -z "$EXCLUDE" ]]; then
rm $EXCLUDE rm "$EXCLUDE"
fi
if [[ ! -z "$INPUTS" ]]; then
rm "$INPUTS"
fi fi
if [[ -L "$BACKUP_DEV/simple_backup/last_backup" ]]; then if [[ -L "$BACKUP_DEV/simple_backup/last_backup" ]]; then