X-Git-Url: https://zdv2.bktei.com/gitweb/EVA-2020-02.git/blobdiff_plain/7f9e13e20784b4d7e6a9871f071e9de5e9fa6770..d14c8c8fd64fdc945c1c021e6232ed96005ecd73:/exec/bkgpslog?ds=inline diff --git a/exec/bkgpslog b/exec/bkgpslog index f82396f..04e41ff 100755 --- a/exec/bkgpslog +++ b/exec/bkgpslog @@ -1,24 +1,21 @@ #!/bin/bash - -# Desc: Records gps data until midnight -# Author: Steven Baltakatei Sandoval; License: GPLv3+ -# Usage: bkgpslog -o [output dir] +# Desc: Records gps data #==BEGIN Define script parameters== ## Logging Behavior parameters -BUFFER_TTL="300"; # time between file writes -SCRIPT_TTL_TE="day"; # (day|hour) +bufferTTL="300"; # time between file writes +scriptTTL_TE="day"; # (day|hour) #### TZ="UTC"; export TZ; # Default time zone; overridden by '--time-zone=[str]' option -DIR_TMP_DEFAULT="/dev/shm"; # Default parent of working directory +dirTmpDefault="/dev/shm"; # Default parent of working directory -SCRIPT_TIME_START=$(date +%Y%m%dT%H%M%S.%N); +scriptTimeStart=$(date +%Y%m%dT%H%M%S.%N); PATH="$HOME/.local/bin:$PATH"; # Add "$(systemd-path user-binaries)" path in case apps saved there -SCRIPT_HOSTNAME=$(hostname); # Save hostname of system running this script. -SCRIPT_VERSION="0.5.1"; # Define version of script. -SCRIPT_NAME="bkgpslog"; # Define basename of script file. -SCRIPT_URL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script. -AGE_VERSION="1.0.0-beta2"; # Define version of age (encryption program) -AGE_URL="https://github.com/FiloSottile/age/releases/tag/v1.0.0-beta2"; # Define website hosting age. +scriptHostname=$(hostname); # Save hostname of system running this script. +scriptVersion="0.5.7"; # Define version of script. +scriptName="bkgpslog"; # Define basename of script file. +scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script. +ageVersion="1.0.0-beta2"; # Define version of age (encryption program) +ageURL="https://github.com/FiloSottile/age/releases/tag/v1.0.0-beta2"; # Define website hosting age. declare -Ag appRollCall # Associative array for storing app status declare -Ag fileRollCall # Associative array for storing file status @@ -183,7 +180,7 @@ showUsage() { echoerr "-o ~/Sync/Location" } # Display information on how to use this script. showVersion() { - echoerr "$SCRIPT_VERSION" + echoerr "$scriptVersion" } # Display script version. vbm() { # Usage: vbm "DEBUG:verbose message here" @@ -343,7 +340,7 @@ dateTimeShort(){ # Input: arg1: 'date'-parsable timestamp string (optional) # Output: stdout: timestamp (ISO-8601, no separators) # Depends: yell - local TIME_CURRENT TIME_CURRENT_SHORT + local TIME_CURRENT TIME_CURRENT_SHORT argTime argTime="$1"; # Get Current Time @@ -360,7 +357,7 @@ dateTimeShort(){ TIME_INPUT="$argTime"; else ### F: Time argument not valid; exit - yell "ERROR:Invalid time argument supplied. Exiting."; exit 1; + yell "ERROR:Invalid time argument supplied: \"$argTime\""; yell "Exiting."; exit 1; fi fi # Construct and deliver separator-les date string @@ -391,7 +388,7 @@ dateShort(){ TIME_INPUT="$argTime"; else ### F: Time argument not valid; exit - yell "ERROR:Invalid time argument supplied. Exiting."; exit 1; + yell "ERROR:Invalid time argument supplied: \"$argTime\""; yell "Exiting."; exit 1; fi fi # Construct and deliver separator-les date string @@ -648,7 +645,7 @@ displayMissing() { magicSetScriptTTL() { #Desc: Sets script_TTL seconds from provided time_element string argument #Usage: magicSetScriptTTL [str time_element] - #Input: arg1: string (Ex: SCRIPT_TTL_TE; "day" or "hour") + #Input: arg1: string (Ex: scriptTTL_TE; "day" or "hour") #Output: var: SCRIPT_TTL (integer seconds) #Depends: timeUntilNextHour, timeUntilNextDay local argTimeElement @@ -932,8 +929,8 @@ timeEpochNS() { echo "$TIME_EPOCH_NS"; } # Nanoseconds since 1970-01-01 magicBufferSleepPID() { - # Desc: Compensates for lag so buffer rounds start every BUFFER_TTL seconds - # Input: vars: BUFFER_TTL, errResetx10e3, K_P, T_I, T_D + # Desc: Compensates for lag so buffer rounds start every bufferTTL seconds + # Input: vars: bufferTTL, errResetx10e3, K_P, T_I, T_D # # Input: array: errorHistory # Output: vars: BUFFER_TTL_ADJ_FLOAT # Re/Attrib: https://en.wikipedia.org/wiki/PID_controller#Standard_versus_parallel_(ideal)_form @@ -1018,7 +1015,7 @@ magicWriteVersion() { # Usage: magicWriteVersion # Version: 0.1.0 # Input: CONTENT_VERSION, FILEOUT_VERSION, PATHOUT_TAR, DIR_TMP - # Input: SCRIPT_VERSION, SCRIPT_URL, AGE_VERSION, AGE_URL, SCRIPT_HOSTNAME + # Input: scriptVersion, scriptURL, ageVersion, ageURL, scriptHostname # Output: appends tar PATHOUT_TAR # Depends: dateTimeShort, appendArgTar local CONTENT_VERSION pubKeyIndex @@ -1027,14 +1024,14 @@ magicWriteVersion() { FILEOUT_VERSION="$(dateTimeShort)..VERSION"; # Gather VERSION data in CONTENT_VERSION - CONTENT_VERSION="SCRIPT_VERSION=$SCRIPT_VERSION"; + CONTENT_VERSION="scriptVersion=$scriptVersion"; #CONTENT_VERSION="$CONTENT_VERSION""\\n"; - CONTENT_VERSION="$CONTENT_VERSION""\\n""SCRIPT_NAME=$SCRIPT_NAME"; - CONTENT_VERSION="$CONTENT_VERSION""\\n""SCRIPT_URL=$SCRIPT_URL"; - CONTENT_VERSION="$CONTENT_VERSION""\\n""AGE_VERSION=$AGE_VERSION"; - CONTENT_VERSION="$CONTENT_VERSION""\\n""AGE_URL=$AGE_URL"; + CONTENT_VERSION="$CONTENT_VERSION""\\n""scriptName=$scriptName"; + CONTENT_VERSION="$CONTENT_VERSION""\\n""scriptURL=$scriptURL"; + CONTENT_VERSION="$CONTENT_VERSION""\\n""ageVersion=$ageVersion"; + CONTENT_VERSION="$CONTENT_VERSION""\\n""ageURL=$ageURL"; CONTENT_VERSION="$CONTENT_VERSION""\\n""DATE=$(date --iso-8601=seconds)"; - CONTENT_VERSION="$CONTENT_VERSION""\\n""HOSTNAME=$SCRIPT_HOSTNAME"; + CONTENT_VERSION="$CONTENT_VERSION""\\n""HOSTNAME=$scriptHostname"; ## Add list of recipient pubkeys for pubkey in "${recPubKeysValid[@]}"; do ((pubKeyIndex++)) @@ -1050,23 +1047,24 @@ magicWriteVersion() { magicGatherWriteBuffer() { # Desc: bkgpslog-specific meta function for writing data to DIR_TMP then appending each file to PATHOUT_TAR # Inputs: vars: PATHOUT_TAR FILEOUT_{NMEA,GPX,KML} CMD_CONV_{NMEA,GPX,KML} CMD_{COMPRESS,ENCRYPT} DIR_TMP, - # Inputs: vars: BUFFER_TTL bufferTTL_STR SCRIPT_HOSTNAME CMD_COMPRESS_SUFFIX CMD_ENCRYPT_SUFFIX + # Inputs: vars: BUFFER_TTL bufferTTL_STR scriptHostname CMD_COMPRESS_SUFFIX CMD_ENCRYPT_SUFFIX # Output: file: (PATHOUT_TAR) # Depends: yell(), try(), vbm(), appendArgTar(), tar 1, sleep 8, checkMakeTar() # Depends: magicWriteVersion(), appendFileTar() local FN + vbm "DEBUG:STATUS:$FN:Started magicGatherWriteBuffer()."; # Debug:Get function name FN="${FUNCNAME[0]}"; # Create buffer file with unique name - PATHOUT_BUFFER="$DIR_TMP/buffer$SECONDS"; + PATHOUT_BUFFER="$DIR_TMP/buffer$SECONDS" && vbm "PATHOUT_BUFFER:$PATHOUT_BUFFER"; # Fill buffer timeout "$BUFFER_TTL"s gpspipe -r -o "$PATHOUT_BUFFER" ; - timeBufferStart="$(dateTimeShort "$(date --date="$BUFFER_TTL seconds ago")")"; # Note start time - vbm "DEBUG:STATUS:$FN:Started magicWriteBuffer()."; + timeBufferStartLong="$(date --date="$BUFFER_TTL seconds ago" --iso-8601=seconds)" && vbm "timeBufferStartLong:$timeBufferStartLong" || yell "ERROR:timeBufferStartLong fail"; + timeBufferStart="$(dateTimeShort "$timeBufferStartLong" )" && vbm "timeBufferStart:$timeBufferStart" || yell "ERROR:timeBufferStart fail"; # Note start time # Determine file paths (time is start of buffer period) - FILEOUT_BASENAME="$timeBufferStart""--""$bufferTTL_STR""..""$SCRIPT_HOSTNAME""_location" && vbm "STATUS:Set FILEOUT_BASENAME to:$FILEOUT_BASENAME"; + FILEOUT_BASENAME="$timeBufferStart""--""$bufferTTL_STR""..""$scriptHostname""_location" && vbm "STATUS:Set FILEOUT_BASENAME to:$FILEOUT_BASENAME"; ## Files saved to DIR_TMP FILEOUT_NMEA="$FILEOUT_BASENAME".nmea"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_NMEA to:$FILEOUT_NMEA"; FILEOUT_GPX="$FILEOUT_BASENAME".gpx"$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX" && vbm "STATUS:Set FILEOUT_GPX to:$FILEOUT_GPX"; @@ -1076,19 +1074,25 @@ magicGatherWriteBuffer() { PATHOUT_KML="$DIR_TMP"/"$FILEOUT_KML" && vbm "STATUS:Set PATHOUT_KML to:$PATHOUT_KML"; ## Files saved to disk (DIR_OUT) ### one file per day (Ex: "20200731..hostname_location.[.gpx.gz].tar") - PATHOUT_TAR="$DIR_OUT"/"$(dateShort "$(date --date="$BUFFER_TTL seconds ago")")".."$SCRIPT_HOSTNAME""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \ + PATHOUT_TAR="$DIR_OUT"/"$(dateShort "$(date --date="$BUFFER_TTL seconds ago" --iso-8601=seconds)")".."$scriptHostname""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \ vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR"; # DEBUG: check vars - vbm "STATUS:DIR_TMP :$DIR_TMP"; - vbm "STATUS:PATHOUT_TAR :$PATHOUT_TAR"; - vbm "STATUS:PATHOUT_NMEA:$PATHOUT_NMEA"; - vbm "STATUS:PATHOUT_GPX:$PATHOUT_GPX"; - vbm "STATUS:PATHOUT_KML:$PATHOUT_KML"; + vbm "STATUS:FN :$FN"; + vbm "STATUS:DIR_TMP :$DIR_TMP"; + vbm "STATUS:PATHOUT_TAR :$PATHOUT_TAR"; + vbm "STATUS:PATHOUT_NMEA :$PATHOUT_NMEA"; + vbm "STATUS:PATHOUT_GPX :$PATHOUT_GPX"; + vbm "STATUS:PATHOUT_KML :$PATHOUT_KML"; + vbm "STATUS:BUFFER_TTL :$BUFFER_TTL"; + vbm "STATUS:PATHOUT_BUFFER :$PATHOUT_BUFFER"; + vbm "STATUS:timeBufferStart:$timeBufferStart"; + vbm "FILEOUT_BASENAME :$FILEOUT_BASENAME"; # Validate PATHOUT_TAR as tar. checkMakeTar "$PATHOUT_TAR"; ## Add VERSION file if checkMakeTar had to create a tar (exited 1) or replace one (exited 2) + vbm "exit status before magicWriteVersion:$?" if [[ $? -eq 1 ]] || [[ $? -eq 2 ]]; then magicWriteVersion; fi # Write bufferBash to PATHOUT_TAR @@ -1099,7 +1103,7 @@ magicGatherWriteBuffer() { # Remove secured chunks from DIR_TMP rm "$PATHOUT_BUFFER" "$PATHOUT_NMEA" "$PATHOUT_GPX" "$PATHOUT_KML"; - vbm "DEBUG:STATUS:$FN:Finished magicWriteBuffer()."; + vbm "DEBUG:STATUS:$FN:Finished magicGatherWriteBuffer()."; } # write buffer to disk magicParseRecipientDir() { # Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory") @@ -1215,8 +1219,8 @@ magicParseCompressionArg() { magicInitWorkingDir() { # Desc: Determine temporary working directory from defaults or user input # Usage: magicInitWorkignDir - # Input: vars: OPTION_TEMPDIR, argTempDirPriority, DIR_TMP_DEFAULT - # Input: vars: SCRIPT_TIME_START + # Input: vars: OPTION_TEMPDIR, argTempDirPriority, dirTmpDefault + # Input: vars: scriptTimeStart # Output: vars: DIR_TMP # Depends: processArguments(), vbm(), yell() # Parse '-t' option (user-specified temporary working dir) @@ -1232,25 +1236,25 @@ magicInitWorkingDir() { fi; else ## Set DIR_TMP_PARENT to default or fallback otherwise - if [[ -d "$DIR_TMP_DEFAULT" ]]; then - DIR_TMP_PARENT="$DIR_TMP_DEFAULT"; + if [[ -d "$dirTmpDefault" ]]; then + DIR_TMP_PARENT="$dirTmpDefault"; elif [[ -d /tmp ]]; then - yell "WARNING:$DIR_TMP_DEFAULT not available. Falling back to /tmp ."; + yell "WARNING:$dirTmpDefault not available. Falling back to /tmp ."; DIR_TMP_PARENT="/tmp"; else yell "ERROR:No valid working directory available. Exiting."; exit 1; fi fi; - ## Set DIR_TMP using DIR_TMP_PARENT and nonce (SCRIPT_TIME_START) - DIR_TMP="$DIR_TMP_PARENT"/"$SCRIPT_TIME_START""..bkgpslog" && vbm "DEBUG:Set DIR_TMP to:$DIR_TMP"; # Note: removed at end of main(). + ## Set DIR_TMP using DIR_TMP_PARENT and nonce (scriptTimeStart) + DIR_TMP="$DIR_TMP_PARENT"/"$scriptTimeStart""..bkgpslog" && vbm "DEBUG:Set DIR_TMP to:$DIR_TMP"; # Note: removed at end of main(). } # Sets working dir magicParseCustomTTL() { # Desc: Set user-specified TTLs for buffer and script # Input: vars: argCustomBufferTTL (integer), argCustomScriptTTL_TE (string) # Input: vars: OPTION_CUSTOM_BUFFERTTL, OPTION_CUSTOM_SCRIPTTTL - # Input: vars: BUFFER_TTL (integer), SCRIPT_TTL_TE (string) - # Output: BUFFER_TTL (integer), SCRIPT_TTL_TE (string) + # Input: vars: BUFFER_TTL (integer), scriptTTL_TE (string) + # Output: BUFFER_TTL (integer), scriptTTL_TE (string) # Depends validateInput(), showUsage(), yell # React to '-b, --buffer-ttl' option @@ -1258,7 +1262,7 @@ magicParseCustomTTL() { ## T: Check if argCustomBufferTTL is an integer if validateInput "$argCustomBufferTTL" "integer"; then ### T: argCustomBufferTTL is an integer - BUFFER_TTL="$argCustomBufferTTL"; + BUFFER_TTL="$argCustomBufferTTL" && vbm "Custom BUFFER_TTL from -b:$BUFFER_TTL"; else ### F: argcustomBufferTTL is not an integer yell "ERROR:Invalid integer argument for custom buffer time-to-live."; showUsage; exit 1; @@ -1271,17 +1275,19 @@ magicParseCustomTTL() { ## T: Check if argCustomScriptTTL is a time element (ex: "day", "hour") if validateInput "$argCustomScriptTTL" "time_element"; then ### T: argCustomScriptTTL is a time element - SCRIPT_TTL_TE="$argCustomScriptTTL"; + scriptTTL_TE="$argCustomScriptTTL" && vbm "Custom scriptTTL_TE from -B:$scriptTTL_TE"; else ### F: argcustomScriptTTL is not a time element yell "ERROR:Invalid time element argument for custom script time-to-live."; showUsage; exit 1; fi; - ## F: do not change SCRIPT_TTL_TE + ## F: do not change scriptTTL_TE fi; } # Sets custom script or buffer TTL if specified main() { + # DEBUG: Print environment variables + vbm "echo $(printenv)"; # Process arguments processArguments "$@"; ## Act upon arguments @@ -1295,26 +1301,26 @@ main() { #### React to "-R" ("recipient directory") option magicParseRecipientDir; # Updates recPubKeysValid #### React to custom buffer and script TTL options ("-b", "-B") - magicParseCustomTTL; # Sets custom SCRIPT_TTL_TE and/or BUFFER_TTL if specified + magicParseCustomTTL; # Sets custom scriptTTL_TE and/or bufferTTL if specified # Check that critical apps and dirs are available, display missing ones. if ! checkapp gpspipe tar && ! checkdir "$DIR_OUT" "DIR_TMP"; then yell "ERROR:Critical components missing."; displayMissing; yell "Exiting."; exit 1; fi - # Set script lifespan (SCRIPT_TTL from SCRIPT_TTL_TE) - magicSetScriptTTL "$SCRIPT_TTL_TE"; - ## Note: SCRIPT_TTL_TE is time element string (ex: "day") while SCRIPT_TTL is integer seconds + # Set script lifespan (SCRIPT_TTL from scriptTTL_TE) + magicSetScriptTTL "$scriptTTL_TE"; + ## Note: scriptTTL_TE is time element string (ex: "day") while SCRIPT_TTL is integer seconds # File name substring (ISO-8601 duration from BUFFER_TTL) - bufferTTL_STR="$(timeDuration "$BUFFER_TTL")"; + bufferTTL_STR="$(timeDuration "$bufferTTL")" && vbm "DEBUG:bufferTTL_STR:$bufferTTL_STR"; # Init temp working dir - try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir creatd at:$DIR_TMP"; + try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir created at DIR_TMP:$DIR_TMP"; # Initialize 'tar' archive ## Define output tar path (note: each day gets *one* tar file (Ex: "20200731..hostname_location.[.gpx.gz].tar")) - PATHOUT_TAR="$DIR_OUT"/"$(dateShort)".."$SCRIPT_HOSTNAME""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \ + PATHOUT_TAR="$DIR_OUT"/"$(dateShort)".."$scriptHostname""_location""$CMD_COMPRESS_SUFFIX""$CMD_ENCRYPT_SUFFIX".tar && \ vbm "STATUS:Set PATHOUT_TAR to:$PATHOUT_TAR"; ## Check that PATHOUT_TAR is a tar. Rename old and create empty one otherwise. checkMakeTar "$PATHOUT_TAR" && vbm "DEBUG:Confirmed or Created to be a tar:$PATHOUT_TAR"; @@ -1327,8 +1333,9 @@ main() { CMD_CONV_KML="gpsbabel -i nmea -f - -o kml -F - " && vbm "STATUS:Set CMD_CONV_KML to:$CMD_CONV_KML"; # convert NMEA to KML # MAIN LOOP:Record gps data until script lifespan ends - timeBufferFirstNS="$(timeEpochNS)"; bufferRound=0; BUFFER_TTL_ADJ_FLOAT="10.0"; + timeBufferFirstNS="$(timeEpochNS)"; bufferRound=0; BUFFER_TTL_ADJ_FLOAT="$BUFFER_TTL"; while [[ "$SECONDS" -lt "$SCRIPT_TTL" ]]; do + if ! [[ -d "$DIR_TMP" ]]; then yell "ERROR:DIR_TMP existence failure:$DIR_TMP"; try mkdir "$DIR_TMP" && vbm "DEBUG:Working dir recreated DIR_TMP:$DIR_TMP"; fi magicParseRecipientDir; magicGatherWriteBuffer & sleep "$BUFFER_TTL_ADJ_FLOAT"; # adjusted by magicBufferSleepPID @@ -1338,7 +1345,7 @@ main() { # Cleanup ## Remove DIR_TMP - try rm -r "$DIR_TMP"; + try rm -r "$DIR_TMP" && vbm "Removed DIR_TMP:$DIR_TMP"; vbm "STATUS:Main function finished."; } # Main function. @@ -1350,3 +1357,6 @@ main() { main "$@" # Run main function. exit 0; #==END Perform work and exit== + +# Author: Steven Baltakatei Sandoval; +# License: GPLv3+