User Tools

Site Tools


eric:computer:bash:shell_scripting:media-rsync.sh.2018.04.23

This is an old revision of the document!


Title: media-rsync.sh.2018.04.23
Created: 2018-04-29 22:13:08 +0000

media-rsync.sh

This can be used to copy video files off of the shooting media, naming folders. Needs to be setup for a certain computer it is on.

#!/bin/bash

# media-rsync.sh
#
# This script will rsync media from one external device to another
#
# 2018-04-22 Created first version of this script
# 2018-04-23 Added disk free space display, disu usage, and enabled filenames with spaces
# 2018-04-23 Added label to destination folder name
#

# Debugging
#set -x     #echo commands after executing them
set -u      #create error if we use a variable we have not set (unset variables)

# Variables
NAME="Media-Rsync"      #name of this shell script for error messages
USERNAME=`whoami`       #username to be used for /media/username paths in Ubuntu and others
MOUNTS=/media/$USERNAME     #path from root to where external hard drives are mounted (Ubuntu version)
declare -a DEVICES      #contents of $MOUNTS
declare -i DEVICE_NUMBER    #count for $MOUNTS list
declare -i LINE_NUMBER      #line number for $MOUNTS list
DEST_FOLDER="media-rsync"       #a folder to put all rsynced copies into, ie /media/ericswanson/pike-a/media-rsync/
OUTPUT_FOLDER="placeholder" #folder to be created just before rsync to be put into $DEST_FOLDER, name is time stamp
LABEL="placeholder"     #a user defined tag to add to the end of the destination folder name
declare -i I            #iteration counter
declare -i SOURCE_NUMBER    #device user picked to be source device number
declare -i DEST_NUMBER      #device user picked to be destination device number
SRC_EQ_DEST="placeholder"   #user input if source and destination device are the same
OK="Y"
SOURCE_PATH="placeholder"   #full path of source to give to rsync
DEST_PATH="placeholder"     #full path of destination rsync folder
ARE_YOU_SURE="placeholder"  #Y/N before starting rsync
ERROR_LOG="placeholder"     #error log file to be put in destination directory
DISK_NAME="placeholder"     #name of disk
FREE_SPACE="placeholder"    #human readable free space from df -h
DISK_USED="placeholder"     #human readable disk usage from du -sh
HEADER="# Device Path                                             Used   Free"
FOOTER="                                                                     "

# Traps
# Trap for control-c
trap 'echo ; echo "User cancelled $NAME with Control-C." ; echo ; exit 2' 2

# Trap other early exits
trap 'echo ; echo "Exiting $NAME." ; echo' 1 15

# Script name
echo
tput rev
echo $NAME
tput sgr0
echo

# echo start time
date
echo

# Main loop to get input
ARE_YOU_SURE="N"

while
    [ $ARE_YOU_SURE != "Y" ]
do

    # Create an array of external device names
    DEVICES=($MOUNTS/*)

    # Were there any devices found, if not, quit
    if [ -z "${DEVICES[0]}" ]
    then
        # No external devices were found, exit
        echo
        echo "No external devices were found, exiting."
        echo
        exit 1
    fi

    # Show devices located
    tput rev
    echo "$HEADER"
    tput sgr0

    # Display device names found
    for ((I=0; I<${#DEVICES[@]}; I++)); do
        ((DEVICE_NUMBER=$I + 1))
        #tput rev
        # Format the device line number to 2 digits with leading zeros (leading zeros not working currently)***
        LINE_NUMBER=`echo $DEVICE_NUMBER | awk '{ printf("%02d", $1) }'`
        echo -n "$LINE_NUMBER"
        #tput sgr0

        # Format disk name
        DISK_NAME=`echo ${DEVICES[$I]##*/::53} | awk '{ printf"%-53s", $0 }'`
        echo -n " $DISK_NAME "
        
        # Calculate how much space has been used on this disk
        DISK_USED=`du -sh "$MOUNTS/${DEVICES[$I]##*/}" 2>/dev/null | awk '{ printf "%6s", $1 }'`
        echo -n "$DISK_USED "

        # Calculate how much free space this disk has
        FREE_SPACE=`df -h "$MOUNTS/${DEVICES[$I]##*/}" 2>/dev/null | tail -1 | awk '{ printf "%6s", $4 }'`
        echo "$FREE_SPACE"
    done

    # Print the table footer (bottom line)
    tput rev
    echo "$FOOTER"
    tput sgr0

    # SOURCE DEVICE
    # Choose input device from list put into an array
    OK="N"      # Exit loop flag

    while
        [ $OK != "Y" ]
    do

        echo
        echo -n "Choose `tput smul`source device`tput sgr0` [1-$LINE_NUMBER]: "

        # Read user input
        read SOURCE_NUMBER

        # Real number is less one
        ((SOURCE_NUMBER--))

        # Validate input
        # Source must be from 0 to I-1
        if (("SOURCE_NUMBER">=0 && "SOURCE_NUMBER"<=($I-1)))
        then
            # Input was valid
            OK="Y"      # set exit loop flag
        else
            # Invalid input
            OK="N"
            echo "Invalid source number input, please try again..."
        fi

    done

    # Source path set
    SOURCE_PATH="$MOUNTS/${DEVICES[$SOURCE_NUMBER]##*/}/"

    # Echo confirmation
    echo "  Source:       `tput rev`$SOURCE_PATH`tput sgr0`"

    # DESTINATION DEVICE
    # Choose output device from list put into an array above
    OK="N"      # Exit loop flag

    while
        [ $OK != "Y" ]
    do
        echo
        echo -n "Choose `tput smul`destination device`tput sgr0` [1-$LINE_NUMBER]: "

        # Read user input
        read DEST_NUMBER

        # Real number is one less
        ((DEST_NUMBER--))

        # Validate input
        # Destination must be from 0 to I-1
        if (("DEST_NUMBER">=0 && "DEST_NUMBER"<=($I-1)))
        then
            # Input was valid
            OK="Y"
        else
            # Invalid input
            OK="N"
            echo "Invalid destination number input, please try again..."
        fi

    done

    # Create copy folder name based on date and time
    DEST_PATH="$MOUNTS/${DEVICES[$DEST_NUMBER]##*/}/$DEST_FOLDER/`date "+%Y-%m-%d-%H.%M.%S"`"

    # Warn if input and output device are the same
    if (("SOURCE_NUMBER"=="DEST_NUMBER"))
    then
        OK="N"

        while
        [ $OK != "Y" ]
        do
        #source = destination
        echo -n "Source and destination are the same, do you really want to copy to the source device?  [Y/N]  "

        # Get user input
        read SRC_EQ_DEST

        case "$SRC_EQ_DEST" in
            Y|y)
            OK="Y"
            echo "Will copy source to source device..."
            ;;
            N|n)
            OK="N"
            ;;
            *)
            OK="N"
            echo "Please answer Y or N."
            ;;
        esac
        done
    fi

    # Ask for optional label to be added to end of destination folder path
    echo
    echo -n "If you would like to add a short label to the destination folder name, type it here: "
    read LABEL

        # Create copy folder name based on date and time
        DEST_PATH="$MOUNTS/${DEVICES[$DEST_NUMBER]##*/}/$DEST_FOLDER/`date "+%Y-%m-%d-%H.%M.%S"`$LABEL"

    # Echo confirmation
    echo "  Destination:  `tput rev`$DEST_PATH`tput sgr0`"
    echo

    OK="N"      # Our flag used below

    # USER CONFIRMATION BEFORE COPY
    echo -n "`tput rev`Are you ready to copy the files now?`tput sgr0`  [Y/N]  "

    # User input
    read OK

    case "$OK" in
        Y|y)
        ARE_YOU_SURE="Y"
        ;;
        N|n)
        ARE_YOU_SURE="N"
        echo
        ;;
        *)
        echo "Please respond with Y or N."
        ;;
    esac
done

# Create the destination folder, including all parent folders that do not exist yet
echo
mkdir -pv "$DEST_PATH"
echo

# Create potential error log file name/path only created if rsync writes to it
ERROR_LOG="$DEST_PATH/rsync_error.log"

# RSYNC HERE!
# Copy files now that we are all set
rsync -va --progress --stats "$SOURCE_PATH" "$DEST_PATH" 2>"$ERROR_LOG"

# Check for error log
if [ -s "$ERROR_LOG" ];
then
    # Rsync exited with errors
    echo
    echo "`tput rev`    RSYNC EXITED WITH ERRORS, CHECK THE LOG FILE    `tput sgr0`"
    echo
    echo "Rsync Error Log"
    echo
    cat "$ERROR_LOG"
    echo
else
    echo
    echo "`tput rev`Rsync completed successfully.`tput sgr0`"
    echo

    # Rsync creates a zero byte file when no errors, so let's delete that now
    rm -f "$ERROR_LOG"
fi

# echo end time
date

# Debugging: turn off echo commands after executing them (set -x)
set +x

exit 0
eric/computer/bash/shell_scripting/media-rsync.sh.2018.04.23.1791490102.txt.gz · Last modified: by admin

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki