User Tools

Site Tools


eric:computer:bash:shell_scripting:rsync-menu.sh.2016.09.30

Title: rsync-menu.sh.2016.09.30
Created: 2018-04-29 22:17:11 +0000

rsync-menu.sh

#!/bin/bash

# File: rsync-menu.sh

# trap for control-c
trap 'exit 2' 2

# the directory this shell script is located in
SCRIPT_HOME="/Volumes/Master-D/shell_scripts/rsync_menu"
CURRENT_LOG_FILE="$SCRIPT_HOME/logs/current_log.txt"

# clear screen, set text to normal
clear
tput sgr0

# clear current log file to blank so that the monitor script can be started now
echo " ">"$CURRENT_LOG_FILE"

NONE="None"
TABLE_HEAD="`tput sgr0`| `tput rev`##`tput sgr0` | `tput rev`Backup Name                                          `tput sgr0` | `tput rev` Last Backup`tput sgr0` | `tput rev` Source (KB)`tput sgr0` | `tput rev` (GB)`tput sgr0` | `tput rev`   Dest (KB)`tput sgr0` | `tput rev` (GB)`tput sgr0` |"
TABLE_LINE="+----+-------------------------------------------------------+--------------+--------------+-------+--------------+-------+"

# move to the script home directory
cd $SCRIPT_HOME
if [ "$?" = "0" ]; then
    # successfully cd
        DO_NOTHING="0"
else
    # could not cd
    echo "`tput rev`ERROR: failed to cd to $SCRIPT_HOME`tput sgr0`"
    #tell the monitor script (if running) that it is time to quit
        echo "-">"$CURRENT_LOG_FILE"
    exit 1
fi

# read the list of sparsebundles and their mount points
. $SCRIPT_HOME/rsync-list_sparsebundles.sh

# using the ls command to generate the list of available backups based on directory names
MENU_ARRAY=( `ls -d menu*` )

# read hard disk SMART status for user info only
#echo "Displaying Hard Disk SMART Status (for your information)..."
#echo
#
#. $SCRIPT_HOME/smart.sh
#
#echo "End of SMART Status"
#echo

# test if all mounted disks are writable, for user info only
. $SCRIPT_HOME/test-disk-write.sh

# main menu
# clear screen
clear

while :

# do (main loop)
do
    echo "`tput rev`RSYNC MAIN MENU`tput sgr0`"
    echo

    # the number of items in the array
    MENU_ARRAY_COUNT=${#MENU_ARRAY[@]}
    echo "There are `tput smul`$MENU_ARRAY_COUNT`tput rmul` backup scripts available."
    echo "If you continue, each destination (backup) disk will be opened and read for it's last backup date."
    echo
    echo -n "Enter [`tput rev`c`tput sgr0`] to continue, or [`tput rev`q`tput sgr0`] to quit:  "
    read CONTINUE
    echo

    # case continue/quit
    case "$CONTINUE" in
        'c')
            # print header
            echo "$TABLE_LINE"
            echo "$TABLE_HEAD"
            echo "$TABLE_LINE"
            for (( I = 0 ; I < ${#MENU_ARRAY[@]} ; I++ ))
            do

                # Array starts counting first element as 0, we add 1 for humans
                let COUNT=$I+1

                # clear both mode variables in case of error in setup.sh file
                SOURCE_MODE=""
                DEST_MODE=""

                # now read the setup.sh file in the directory to get variables
                . ${MENU_ARRAY[$I]}/setup.sh

                # let's see how big the source is
                # if there is a source sparsebundle image, mount it
                if [ $SOURCE_SPARSEBUNDLE != "-" ]
                then
                    hdiutil attach -quiet $SOURCE_SPARSEBUNDLE
                fi

                # we are going to mount the destination sparsebundle so we can get the last backup date off of it
                hdiutil attach -quiet $DEST_SPARSEBUNDLE

                if [ -e "$DEST_PATH.latest-rsync-date" ]
                then
                    # Month
                    LAST_BACKUP=`ls -l "$DEST_PATH.latest-rsync-date" | awk '{print $6}'`
                    # Format the day as two digits
                    LAST_BACKUP=$LAST_BACKUP" "`ls -l "$DEST_PATH.latest-rsync-date" | awk '{printf "%2s", $7'}`
                    # The rest of the date
                    LAST_BACKUP=$LAST_BACKUP" "`ls -l "$DEST_PATH.latest-rsync-date" | awk '{print $8}'`
                else
                    LAST_BACKUP=$NONE
                    #LAST_BACKUP=""
                fi
                # display size & free space for destination
                echo -n "| "
                echo -n "`echo $COUNT | awk '{printf "%2s", $1 }'`"
                echo -n " | "
                echo -n "`echo $BACKUP_NAME | awk '{printf "%-53s", $1}'`"
                echo -n " | "
                if [ "$LAST_BACKUP" == "$NONE" ]
                then
                    echo -n "        None"
                else
                    echo -n "$LAST_BACKUP"
                fi
                #echo -n "`echo $LAST_BACKUP | awk '{printf "%12s", $1, $2, $3}'`"
                echo -n " | "

                # determine if source is online
                if [ -d "$SOURCE_PATH" ]; then
                    # Control will enter here if $DIRECTORY exists.

                    case "$SOURCE_MODE" in
                        'DISK')
                            # size returned is 512K blocks, so we divide by 2 to get better idea of size in K
                            SOURCE_SIZE=`df "$SOURCE_PATH" | tail -1 | awk '{printf $3 }'`
                            let SOURCE_SIZE=$SOURCE_SIZE/2
                            echo -n "`echo $SOURCE_SIZE | awk '{printf "%12s", $1 }'`"
                            ;;
                        'FOLDER')
                            # size returned is 512K blocks, so we divide by 2 to get K
                            SOURCE_SIZE=`du -s "$SOURCE_PATH" | awk '{print $1 }'`
                            let SOURCE_SIZE=$SOURCE_SIZE/2
                            echo -n "`echo $SOURCE_SIZE | awk '{printf "%12s", $1 }'`"
                            ;;
                        *)
                            echo -n " `tput rev`ERROR`tput sgr0`"
                            SOURCE_SIZE="0"
                            ;;
                    esac
                    echo -n " | "

                    let SOURCE_GB=($SOURCE_SIZE/1024)/1024
                    echo -n "`echo $SOURCE_GB | awk '{printf "%5s", $1 }'`"
                                else
                                        echo -n "     `tput rev`OFFLINE`tput sgr0` | -----"
                    SOURCE_SIZE="0"
                                fi

                echo -n " | "

                if [ -d "$DEST_PATH" ]
                then
                    # here if directory exists
                    case "$DEST_MODE" in
                        'DISK')
                            # size returned is 512K blocks, so we divide by 2 to get 1K size
                            DEST_SIZE=`df "$DEST_PATH" | tail -1 | awk '{print $2 }'`
                            let DEST_SIZE=$DEST_SIZE/2
                            echo -n "`echo $DEST_SIZE | awk '{printf "%12s", $1 }'`"
                            ;;
                        'FOLDER')
                            #echo -n "`du -hs "$DEST_PATH" | awk '{printf "%6s", $1 }'`"
                            #DEST_SIZE=`du -s "$DEST_PATH" | awk '{print $1 }'`
                            # actually even in folder mode, we still want to know the free space available on the disk, not the folder
                            FOLDER_USED=`du -s "$DEST_PATH" | awk '{print $1 }'`
                            DISK_FREE=`df "$DEST_PATH" | tail -1 | awk '{print $4 }'`
                            let DEST_SIZE=($FOLDER_USED+$DISK_FREE)/2
                                                    echo -n "`echo $DEST_SIZE | awk '{printf "%12s", $1 }'`"
                            ;;
                        *)
                            echo -n " `tput rev`ERROR`tput sgr0`"
                            DEST_SIZE="0"
                            ;;
                    esac

                    echo -n " | "

                    let DEST_GB=($DEST_SIZE/1024)/1024
                    echo -n "`echo $DEST_GB | awk '{printf "%5s", $1 }'`"

                else
                    # directory does not exist
                    echo -n "     `tput rev`OFFLINE`tput sgr0` | -----"
                    DEST_SIZE="0"
                                fi

                echo -n " | "

                if [ $SOURCE_SIZE -gt $DEST_SIZE ]
                then
                    # source size is larger than destination
                    echo " `tput rev`** CHECK SPACE AVAILABILITY **`tput sgr0`"
                else
                    # destination is larger or equal to the source size
                    echo
                fi

                # unmount dest sparsebundle
                hdiutil detach -quiet -force $DEST_IMAGE
                if [ $SOURCE_SPARSEBUNDLE != "-" ]
                then
                    # unmount source sparsebundle
                    hdiutil detach -quiet -force $SOURCE_IMAGE
                fi
            done

            # print footer
            echo "$TABLE_LINE"

            echo
            echo "`tput rev`CHOOSE MODE. . .`tput sgr0`"
            echo "  `tput smul`BACKUP`tput rmul` mode will copy normally from Source to Destination (with archive, if setup)"
            echo "  `tput smul`RESTORE`tput rmul` mode will copy from Destination to Source (with archive, if setup)"
            echo
            echo -n "Choose mode: `tput smul`Backup`tput rmul` [`tput rev`b`tput sgr0`] or `tput smul`Restore`tput rmul` [`tput rev`r`tput sgr0`] (all other responses will cause the script to exit):  "
            read MODE
            echo
            case "$MODE" in
                'b')
                    #normal backup mode
                    echo "Normal backup mode selected"
                    ;;

                'r')
                    #restore mode
                    echo "`tput rev`RESTORE MODE SELECTED (DATA IS COPIED BACK)`tput sgr0`"
                    ;;

                *)
                    #invalid input
                    echo "Invalid mode choice, exiting"
                    exit 1
                    ;;  
            esac

            echo
            echo "`tput rev`CHOOSE FIRST AND LAST BACKUP SET TO RUN FROM TABLE ABOVE. . .`tput sgr0`"
            echo "  Enter the number of your `tput smul`starting`tput rmul` choice. . ."
            echo -n "  [1 - $MENU_ARRAY_COUNT] Your `tput smul`start`tput rmul` choice:  "
            read CHOICE
            echo

                echo "  Enter `tput smul`end`tput rmul` choice (it can be the `tput smul`same`tput rmul` as the start choice). . ."
                echo -n "  [$CHOICE - $MENU_ARRAY_COUNT]  Your `tput smul`end`tput rmul` choice:  "
                read END

            # if END is less than start point, make it equal CHOICE
                if [ $CHOICE -gt $END ]
                then
                        let END=$CHOICE
                fi

            # if END is blank, make it equal to CHOICE
                if [ -z "$END" ]
                then
                        let END=$CHOICE
                fi

            # if END is higher than last array item, reduce it
                if [ $END -gt $MENU_ARRAY_COUNT ]
                then
                        let END=$MENU_ARRAY_COUNT
                fi

                echo
                echo Starting at: $CHOICE  Ending at: $END
                echo

            # did not want to exit, see if they typed a number too high
                if [ $CHOICE -gt $MENU_ARRAY_COUNT ]
                then
                        echo "Invalid response, start point too high."
                        echo -n "Press [return] key to continue. . ."
                        read ENTERKEY

            # here we will check for a number too small (zero)
                else
                        if [ $CHOICE -eq 0 ]
                        then
                            echo "Invalid response, start point too low."
                            echo -n "Press [return] key to continue. . ."
                            read ENTERKEY

                    # we will execute the command now that we have determined it is a valid number
                            else

                    # we have to subtract one from the human's entry to match the array number
                    let CHOICE=$CHOICE-1
                            let END=$END-1

                    # ask if they want to do a checksum verify
                            echo -n "Do you want to add a `tput smul`checksum`tput rmul` verify to each backup [y/n]:  "
                            read CHECKSUM
                    # this if statement really should be case y, n, * but we'll leeave it for now
                            if [ $CHECKSUM == "y" ]
                            then
                                CHECKSUM_FLAG="-c"
                                echo "`tput smul`Checksum is turned on`tput rmul`"
                            else
                                CHECKSUM_FLAG=""
                                echo "Checksum is turned off"
                            fi

                    echo

                    # loop from first to last entry
                            while [ $CHOICE -le $END ]
                            do

                        # reread the variables for this array item now
                                . ${MENU_ARRAY[$CHOICE]}/setup.sh

                                case "$MODE" in
                            'b')
                                #backup mode (nomral)
                                echo `tput rev`Running backup: $BACKUP_NAME`tput sgr0`
                                ;;

                            'r')
                                #restore mode (reverse)
                                echo `tput rev`Running RESTORE: $BACKUP_NAME`tput sgr0`

                                #we need to flip the source and destination
                                ORIG_SRC_SPARSEB=$SOURCE_SPARSEBUNDLE
                                ORIG_SRC_IMAGE=$SOURCE_IMAGE
                                ORIG_SRC_PATH=$SOURCE_PATH

                                ORIG_DST_SPARSEB=$DEST_SPARSEBUNDLE
                                ORIG_DST_IMAGE=$DEST_IMAGE
                                ORIG_DST_PATH=$DEST_PATH

                                SOURCE_SPARSEBUNDLE=$ORIG_DST_SPARSEB
                                SOURCE_IMAGE=$ORIG_DST_IMAGE
                                SOURCE_PATH=$ORIG_DST_PATH

                                DEST_SPARSEBUNDLE=$ORIG_SRC_SPARSEB
                                DEST_IMAGE=$ORIG_SRC_IMAGE
                                DEST_PATH=$ORIG_SRC_PATH
                                ;;

                            *)
                                #error condition
                                exit 1
                                ;;
                        esac

                        # display settings
                        echo
                                echo "These are the settings we will use:"
                                echo
                        echo "Source Sparsebundle Image:       "$SOURCE_SPARSEBUNDLE
                                echo "Source Image:                    "$SOURCE_IMAGE
                                echo "Source Path:                     "$SOURCE_PATH
                                echo
                                echo "Destination Sparsebundle Image:  "$DEST_SPARSEBUNDLE
                                echo "Destination Image:               "$DEST_IMAGE
                                echo "Destination Path:                "$DEST_PATH
                                echo
                                echo "Archive Sparsebundle Image:      "$ARCHIVE_SPARSEBUNDLE
                                echo "Archive Image:                   "$ARCHIVE_IMAGE
                                echo "Archive Path:                    "$ARCHIVE_PATH
                                echo

                                # here is where we do the selected rsync command
                                # delete old logs to avoid confusion
                                # touch log file first so we do not get an error if it does not exist from rm
                                touch "./logs/$BACKUP_NAME.log"
                        if [ "$?" = "0" ]; then
                            # touch exited successfully
                                    rm "./logs/$BACKUP_NAME.log"
                            if [ "$?" = "0" ]; then
                                # rm succedded
                                    DO_NOTHING="0"
                            else
                                # rm failed
                                echo "`tput rev setaf 1`ERROR: Failed to delete file: ./logs/$BACKUP_NAME.log`tput sgr0`"
                                    #tell the monitor script (if running) that it is time to quit
                                    echo "-">"$CURRENT_LOG_FILE"
                                exit 1
                            fi
                        else
                            # touch failed
                            echo "`tput rev setaf 1`ERROR: Could not write to log file: ./logs/$BACKUP_NAME.log`tput sgr0`"
                                #tell the monitor script (if running) that it is time to quit
                                echo "-">"$CURRENT_LOG_FILE"
                            exit 1
                        fi

                                . rsync-main.sh>"./logs/$BACKUP_NAME.log"
                        if [ "$?" = "0" ]; then
                            # Exit without errors
                            echo "`tput setaf 2`Backup completed `tput smul`without`tput rmul``tput setaf 2` errors`tput sgr0`"
                        else
                            # Exit with errors
                            echo "`tput setaf 1`* This backup had errors, be sure to check the logs *`tput sgr0`"
                        fi
                                # done with the backup script, we are going to email it now
                                echo
                        echo "emailing the results log"
                                mail -s "rsync results for: $BACKUP_NAME" ericswanson1020@yahoo.com <"./logs/$BACKUP_NAME.log"
                                echo done
                                echo

                        # ring the terminal's bell to notify this backup is done
                        tput bel

                                # increment choice by 1
                                let CHOICE=$CHOICE+1

                            done
                        fi
                fi
            ;;
        'q')
                #tell the monitor script (if running) that it is time to quit
                echo "-">"$CURRENT_LOG_FILE"
            exit 0
                ;;
            *)
                echo -n "Invalid input, press [return] to continue..."
                read ENTERKEY
                ;;

    # case (continue/quit) ends here
    esac

# do (main loop) ends here
done

# all done, exit
#tell the monitor script (if running) that it is time to quit
echo "-">"$CURRENT_LOG_FILE"
exit 0
eric/computer/bash/shell_scripting/rsync-menu.sh.2016.09.30.txt · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki