- # Append to tar
- try tar --append --directory="$tmpDir" --file="$tarPath" "$fileName";
- #yell "DEBUG:STATUS:$fn:Finished appendFileTar()."
-} # Append [processed] file to Tar archive
-magicParseRecipientArgs() {
- # Desc: Parses recipient arguments specified by '-r' option
- # Input: vars: optionEncrypt, optionRecipients
- # arry: argRecPubKeys from processArguments()
- # Output: vars: cmd_encrypt, cmd_encrypt_suffix
- # arry: recPubKeysValid, recPubKeysValidStatic
- # Depends: processArguments(), yell(), vbm(), checkapp(), checkAgePubkey(), validateInput()
- local recipients
-
- # Check if encryption option active.
- if [[ "$optionEncrypt" = "true" ]] && [[ "$optionRecipients" = "true" ]]; then
- if checkapp age; then # Check that age is available.
- for pubkey in "${argRecPubKeys[@]}"; do # Validate recipient pubkey strings by forming test message
- vbm "DEBUG:Testing pubkey string:$pubkey";
- if checkAgePubkey "$pubkey" && \
- ( validateInput "$pubkey" "ssh_pubkey" || validateInput "$pubkey" "age_pubkey"); then
- #### Form age recipient string
- recipients="$recipients""-r '$pubkey' ";
- vbm "STATUS:Added pubkey for forming age recipient string:""$pubkey";
- vbm "DEBUG:recipients:""$recipients";
- #### Add validated pubkey to recPubKeysValid array
- recPubKeysValid+=("$pubkey") && vbm "DEBUG:recPubkeysValid:pubkey added:$pubkey";
- else
- yell "ERROR:Exit code ""$?"". Invalid recipient pubkey string. Exiting."; exit 1;
- fi;
- done
- vbm "DEBUG:Finished processing argRecPubKeys array";
- vbm "STATUS:Array of validated pubkeys:${recPubKeysValid[*]}";
- recPubKeysValidStatic=("${recPubKeysValid[@]}"); # Save static image of pubkeys validated by this function
+magicInitWorkingDir() {
+ # Desc: Determine temporary working directory from defaults or user input
+ # Usage: magicInitWorkingDir
+ # Input: vars: optionTmpDir, argTempDirPriority, dirTmpDefault
+ # Input: vars: scriptTimeStart
+ # Output: vars: dir_tmp
+ # Depends: bash 5.0.3, processArguments(), vbm(), yell()
+ # Parse '-t' option (user-specified temporary working dir)
+ ## Set dir_tmp_parent to user-specified value if specified
+ local fn dir_tmp_parent
+
+ # Save function name
+ fn="${FUNCNAME[0]}";
+
+ vbm "STATUS:$fn:Starting magicInitWorkingDir() function.";
+ if [[ "$optionTmpDir" = "true" ]]; then
+ if [[ -d "$argTempDirPriority" ]]; then
+ dir_tmp_parent="$argTempDirPriority";
+ else
+ yell "WARNING:$fn:Specified temporary working directory not valid:$argTempDirPriority";
+ exit 1; # Exit since user requires a specific temp dir and it is not available.
+ fi;
+ else
+ ## Set dir_tmp_parent to default or fallback otherwise
+ if [[ -d "$dirTmpDefault" ]]; then
+ dir_tmp_parent="$dirTmpDefault";
+ elif [[ -d /tmp ]]; then
+ yell "WARNING:$fn:$dirTmpDefault not available. Falling back to /tmp .";
+ dir_tmp_parent="/tmp";
+ else
+ yell "ERROR:$fn:No valid working directory available. Exiting.";
+ exit 1;
+ fi;
+ fi;
+ ## Set dir_tmp using dir_tmp_parent and nonce (scriptTimeStart)
+ dir_tmp="$dir_tmp_parent"/"$scriptTimeStart""..bkgpslog" && vbm "DEBUG :$fn:Set dir_tmp to:$dir_tmp"; # Note: removed at end of main().
+ vbm "STATUS:$fn:Finished magicInitWorkingDir() function.";
+} # Sets working dir
+magicInitCheckTar() {
+ # Desc: Initializes or checks output tar
+ # input: vars: dirOut, bufferTTL, cmd_encrypt_suffix, cmd_compress_suffix
+ # input: vars: scriptHostname
+ # output: vars: pathout_tar
+ # depends: Bash 5.0.3, vbm(), dateShort(), checkMakeTar(), magicWriteVersion()
+ local fn checkMakeTarES
+
+ # Save function name
+ fn="${FUNCNAME[0]}";
+
+ vbm "STATUS:$fn: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:$fn:Set pathout_tar to:$pathout_tar";
+ # Validate pathout_tar as tar.
+ checkMakeTar "$pathout_tar"; checkMakeTarES="$?";
+ ## Add VERSION file if checkMakeTar had to create a tar (exited 1) or replace one (exited 2)
+ vbm "STATUS:$fn:exit status before magicWriteVersion:$checkMakeTarES"
+ if [[ "$checkMakeTarES" -eq 1 ]] || [[ "$checkMakeTarES" -eq 2 ]]; then magicWriteVersion; fi
+ vbm "STATUS:$fn: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
+ local fn
+
+ # Save function name
+ fn="${FUNCNAME[0]}";
+
+ vbm "STATUS:$fn:Starting magicParseCompressionArg() function.";
+ if [[ "$optionCompress" = "true" ]]; then # Check if compression option active
+ if checkapp gzip; then # Check if gzip available
+ cmd_compress="gzip " && vbm "STATUS:$fn:cmd_compress:$cmd_compress";
+ cmd_compress_suffix=".gz" && vbm "STATUS:$fn:cmd_compress_suffix:$cmd_compress_suffix";
+ else
+ yell "ERROR:$fn:Compression enabled but \"gzip\" not found. Exiting."; exit 1;
+ fi;
+ else
+ cmd_compress="tee /dev/null " && vbm "STATUS:$fn:cmd_compress:$cmd_compress";
+ cmd_compress_suffix="" && vbm "STATUS:$fn:cmd_compress_suffix:$cmd_compress_suffix";
+ vbm "DEBUG :$fn:Compression not enabled.";
+ fi;
+ vbm "STATUS:$fn:Starting magicParseCompressionArg() function.";
+} # Form compression cmd string and filename suffix
+magicParseCustomTTL() {
+ # Desc: Set user-specified TTLs for buffer and script
+ # Usage: magicParseCustomTTL
+ # Input: vars: argCustomBufferTTL (integer), argCustomScriptTTL_TE (string)
+ # Input: vars: optionCustomBufferTTL, optionCustomScriptTTL_TE
+ # Input: vars: bufferTTL (integer), scriptTTL_TE (string)
+ # Output: bufferTTL (integer), scriptTTL_TE (string)
+ # Depends: Bash 5.0.3, yell(), vbm(), validateInput(), showUsage()
+ local fn
+
+ # Save function name
+ fn="${FUNCNAME[0]}";
+
+ vbm "STATUS:$fn:Starting magicParseCustomTTL() function.";
+ # React to '-b, --buffer-ttl' option
+ if [[ "$optionCustomBufferTTL" = "true" ]]; then
+ ## T: Check if argCustomBufferTTL is an integer
+ if validateInput "$argCustomBufferTTL" "integer"; then
+ ### T: argCustomBufferTTL is an integer
+ bufferTTL="$argCustomBufferTTL" && vbm "STATUS:$fn:Custom bufferTTL from -b:$bufferTTL";
+ else
+ ### F: argcustomBufferTTL is not an integer
+ yell "ERROR:$fn:Invalid integer argument for custom buffer time-to-live."; showUsage; exit 1;
+ fi;
+ ## F: do not change bufferTTL
+ fi;
+
+ # React to '-B, --script-ttl' option
+ if [[ "$optionCustomScriptTTL_TE" = "true" ]]; then
+ ## T: Check if argCustomScriptTTL is a time element (ex: "day", "hour")
+ if validateInput "$argCustomScriptTTL_TE" "time_element"; then
+ ### T: argCustomScriptTTL is a time element
+ scriptTTL_TE="$argCustomScriptTTL_TE" && vbm "STATUS:$fn:Custom scriptTTL_TE from -B:$scriptTTL_TE";
+ else
+ ### F: argcustomScriptTTL is not a time element
+ yell "ERROR:$fn:Invalid time element argument for custom script time-to-live."; showUsage; exit 1;
+ fi;
+ ## F: do not change scriptTTL_TE
+ fi;
+ vbm "STATUS:$fn:Starting magicParseCustomTTL() function.";
+} # Sets custom script or buffer TTL if specified
+magicParseLabel() {
+ # Desc: Parses -l option to set label
+ # In : optionLabel, argLabel
+ # Out: vars: label
+ # Depends: Bash 5.0.3, vbm(), yell()
+ local fn
+
+ # Save function name
+ fn="${FUNCNAME[0]}";
+
+ vbm "STATUS:$fn:Started magicParseLabel() function.";
+ # Do nothing if optionLabel not set to true.
+ if [[ ! "$optionLabel" = "true" ]]; then
+ vbm "STATUS:$fn:optionlabel not set to 'true'. Returning early.";
+ return;
+ fi;
+ # Set label if optionLabel is true
+ if [[ "$optionLabel" = "true" ]]; then
+ label="_""$argLabel";
+ vbm "STATUS:$fn:Set label:$label";
+ fi;
+ vbm "STATUS:$fn:Finished magicParseLabel() function.";
+} # Set label used in output file name
+magicParseProcessStrings() {
+ # Desc: Processes user-supplied process strings into process commands for appendFileTar().
+ # Usage: magicParseProcessStrings
+ # In : vars: optionProcString optionNoStoreRaw optionStoreRaw argRawFileExt
+ # arry: argProcStrings, argProcFileExts
+ # Out: arry: procStrings, procFileExts
+ # Depends Bash 5.0.3, yell(), vbm()
+ local fn rawFileExt