# Script Metadata
scriptName="bklog"; # Define basename of script file.
-scriptVersion="0.1.8"; # Define version of script.
+scriptVersion="0.1.11"; # Define version of script.
scriptURL="https://gitlab.com/baltakatei/ninfacyzga-01"; # Define wesite hosting this script.
scriptTimeStart="$(date +%Y%m%dT%H%M%S.%N)"; # YYYYmmddTHHMMSS.NNNNNNNNN
scriptHostname=$(hostname); # Save hostname of system running this script.
dateCurrentShort="$(date -d "$timeInput" +%Y%m%d)"; # Produce separator-less current date with resolution 1 day.
echo "$dateCurrentShort";
} # Get YYYYmmdd
+dateTimeShort(){
+ # Desc: Timestamp without separators (YYYYmmddTHHMMSS+zzzz)
+ # Usage: dateTimeShort ([str date])
+ # Version 1.1.1
+ # Input: arg1: 'date'-parsable timestamp string (optional)
+ # Output: stdout: timestamp (ISO-8601, no separators)
+ # Depends: yell
+ local argTime timeCurrent timeInput timeCurrentShort
+
+ argTime="$1";
+ # Get Current Time
+ timeCurrent="$(date --iso-8601=seconds)" ; # Produce `date`-parsable current timestamp with resolution of 1 second.
+ # Decide to parse current or supplied date
+ ## Check if time argument empty
+ if [[ -z "$argTime" ]]; then
+ ## T: Time argument empty, use current time
+ timeInput="$timeCurrent";
+ else
+ ## F: Time argument exists, validate time
+ if date --date="$argTime" 1>/dev/null 2>&1; then
+ ### T: Time argument is valid; use it
+ timeInput="$argTime";
+ else
+ ### F: Time argument not valid; exit
+ yell "ERROR:Invalid time argument supplied. Exiting."; exit 1;
+ fi
+ fi
+ # Construct and deliver separator-les date string
+ timeCurrentShort="$(date -d "$timeInput" +%Y%m%dT%H%M%S%z)";
+ echo "$timeCurrentShort";
+} # Get YYYYmmddTHHMMSS±zzzz
setTimeZoneEV(){
# Desc: Set time zone environment variable TZ
# Usage: setTimeZoneEV arg1
# Parse '-t' option (user-specified temporary working dir)
## Set dir_tmp_parent to user-specified value if specified
local dir_tmp_parent
-
+
+ vbm "Starting magicInitWorkingDir() function.";
if [[ "$optionTmpDir" = "true" ]]; then
if [[ -d "$argTempDirPriority" ]]; then
dir_tmp_parent="$argTempDirPriority";
fi;
## 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().
+ vbm "Finished magicInitWorkingDir() function.";
} # Sets working dir
magicInitCheckTar() {
# Desc: Initializes or checks output tar
# output: vars: pathout_tar
# depends: Bash 5.0.3, vbm(), dateShort(), checkMakeTar(), magicWriteVersion()
+ vbm "Starting magicInitCheckTar() function.";
# Form pathout_tar
pathout_tar="$dirOut"/"$(dateShort "$(date --date="$bufferTTL seconds ago" --iso-8601=seconds)")".."$scriptHostname""$label""$cmd_compress_suffix""$cmd_encrypt_suffix".tar && \
vbm "STATUS:Set pathout_tar to:$pathout_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
+ if [[ $? -eq 1 ]] || [[ $? -eq 2 ]]; then magicWriteVersion; fi
+ vbm "Finished magicInitCheckTar() function.";
} # Initialize tar, set pathout_tar
magicParseCompressionArg() {
# Desc: Parses compression arguments specified by '-c' option
# Input: vars: optionCompress
# Output: cmd_compress, cmd_compress_suffix
# Depends: processArguments(), vbm(), checkapp(), gzip 1.9
+
+ vbm "Starting magicParseCompressionArg() function.";
if [[ "$optionCompress" = "true" ]]; then # Check if compression option active
if checkapp gzip; then # Check if gzip available
cmd_compress="gzip " && vbm "cmd_compress:$cmd_compress";
cmd_compress_suffix=".gz" && vbm "cmd_compress_suffix:$cmd_compress_suffix";
else
yell "ERROR:Compression enabled but \"gzip\" not found. Exiting."; exit 1;
- fi
+ fi;
else
cmd_compress="tee /dev/null " && vbm "cmd_compress:$cmd_compress";
cmd_compress_suffix="" && vbm "cmd_compress_suffix:$cmd_compress_suffix";
vbm "DEBUG:Compression not enabled.";
- fi
+ fi;
+ vbm "Starting magicParseCompressionArg() function.";
} # Form compression cmd string and filename suffix
magicParseCustomTTL() {
# Desc: Set user-specified TTLs for buffer and script
# Output: bufferTTL (integer), scriptTTL_TE (string)
# Depends: Bash 5.0.3, yell(), vbm(), validateInput(), showUsage()
+ vbm "Starting magicParseCustomTTL() function.";
# React to '-b, --buffer-ttl' option
if [[ "$optionCustomBufferTTL" = "true" ]]; then
## T: Check if argCustomBufferTTL is an integer
yell "ERROR:Invalid time element argument for custom script time-to-live."; showUsage; exit 1;
fi;
## F: do not change scriptTTL_TE
- fi;
+ fi;
+ vbm "Starting magicParseCustomTTL() function.";
} # Sets custom script or buffer TTL if specified
magicParseLabel() {
# Desc: Parses -l option to set label
# Depends: processArguments(), yell(), vbm(), checkapp(), checkAgePubkey(), validateInput()
local recipients
+ vbm "Starting magicParseRecipientArgs() function.";
# Check if encryption option active.
if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then
if checkapp age; then # Check that age is available.
yell "ERROR:\\'-e\\' set but no \\'-r\\' or \\'-R\\' set."; exit 1; fi;
# Catch case if '-r' or '-R' set but '-e' is not
if [[ ! "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then
- yell "ERROR:\\'-r\\' or \\'-R\\' set but \\'-e\\' is not set."; exit 1; fi;
+ yell "ERROR:\\'-r\\' or \\'-R\\' set but \\'-e\\' is not set."; exit 1; fi;
+ vbm "Finished magicParseRecipientArgs() function.";
} # Populate recPubKeysValid with argRecPubKeys; form encryption cmd string and filename suffix
magicParseRecipientDir() {
# Desc: Updates recPubKeysValid with pubkeys in dir specified by '-R' option ("recipient directory")
local recipientDir recFileLine updateRecipients
declare -a candRecPubKeysValid
+ vbm "Starting magicParseRecipientDir() function.";
# Check that '-e' and '-R' set
if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecDir" = "true" ]]; then
### Check that argRecDir is a directory.
# Handle case if '-R' set but '-e' not set
if [[ ! "$optionEncrypt" = "true" ]] && [[ "$optionRecDir" = "true" ]]; then
yell "ERROR: \\'-R\\' is set but \\'-e\\' is not set."; fi;
+ vbm "Finished magicParseRecipientDir() function.";
} # Update recPubKeysValid with argRecDir
magicSetScriptTTL() {
#Desc: Sets script_TTL seconds from provided time_element string argument
#Output: var: scriptTTL (integer seconds)
#Depends: timeUntilNextHour, timeUntilNextDay
local argTimeElement
-
+
+ vbm "Starting magicSetScriptTTL() function.";
argTimeElement="$1";
if [[ "$argTimeElement" = "day" ]]; then
# Set script lifespan to end at start of next day
else
yell "ERROR:Invalid argument for setScriptTTL function:$argTimeElement"; exit 1;
fi;
+ vbm "Finished magicSetScriptTTL() function.";
} # Set scriptTTL in seconds until next (day|hour).
magicWriteVersion() {
# Desc: Appends time-stamped VERSION to pathout_tar
# Output: appends tar (pathout_tar)
# Depends: bash 5.0.3, dateTimeShort(), appendArgTar()
local fileoutVersion contentVersion pubKeyIndex pubKeyIndex
-
+
+ vbm "Starting magicWriteVersion() function.";
# Set VERSION file name
fileoutVersion="$(dateTimeShort)..VERSION";
# Write contentVersion as file fileoutVersion and write-append to pathout_tar
appendArgTar "$contentVersion" "$fileoutVersion" "$pathout_tar" "$dir_tmp";
+ vbm "Finished magicWriteVersion() function.";
} # write version data to pathout_tar via appendArgTar()
magicProcessWriteBuffer() {
# Desc: process and write buffer
# MAIN LOOP: Run until script TTL seconds pass
bufferRound=0;
while [[ $SECONDS -lt "scriptTTL" ]]; do
+ vbm "DEBUG:Starting buffer round:$bufferRound";
bufferTOD="$((SECONDS + bufferTTL))"; # Set buffer round time-of-death
- lineCount=0; # Debug counter
# Consume stdin to fill buffer until buffer time-of-death (TOD) arrives
while read -r -t "$bufferTTL" line && [[ $SECONDS -lt "$bufferTOD" ]]; do
# Append line to buffer array
buffer+=("$line");
- vbm "DEBUG:Processing line:$lineCount";
- vbm "DEBUG:Current line :$line";
- vbm "DEBUG:buf elem count :${#buffer[@]}";
- ((lineCount++));
done;
# Create dir_tmp if missing
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