From 4c2f80b64eb8e85fc81a806b38b900c90069d407 Mon Sep 17 00:00:00 2001 From: fer Date: Wed, 6 Mar 2024 15:47:35 +0100 Subject: [PATCH 1/4] bitwarden.sh compress s_date e_date --- bitwarden.sh | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/bitwarden.sh b/bitwarden.sh index 936bc4dc..5eca92e0 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -123,6 +123,68 @@ function checkOutputDirNotExists() { fi } +validateDateFormat() { + if ! [[ $1 =~ ^[0-9]{4}[0-9]{2}[0-9]{2}$ ]]; then + echo "Error: $2 date format is invalid. Please use YYYYMMDD." + exit 1 + fi +} + +validateDateOrder() { + if [[ $(date -d "$1") > $(date -d "$2") ]]; then + echo "Error: start date ($1) must be earlier than end date ($2)." + exit 1 + fi +} + +function compressLogs() { + OUTPUT=$1 + START_DATE=$2 + END_DATE=$3 + LOG_DIR=$OUTPUT/logs + RELATIVE_PATH=${LOG_DIR#$(pwd)/} + tempfile=$(mktemp) + + # Validate start date format + if [ -n "$START_DATE" ]; then + validateDateFormat "$START_DATE" "start" + if [ -z "$END_DATE" ]; then + echo "Error: an end date is required when an start date is provided." + exit 1 + fi + fi + + # Validate end date format and order + if [ -n "$END_DATE" ]; then + validateDateFormat "$END_DATE" "end" + validateDateOrder "$START_DATE" "$END_DATE" + fi + + if [ -n "$START_DATE" ] && [ -n "$END_DATE" ]; then + + OUTPUT_FILE="bitwarden-logs-${START_DATE}-to-${END_DATE}.tar.gz" + + if [[ "$START_DATE" == "$END_DATE" ]]; then + OUTPUT_FILE="bitwarden-logs-${START_DATE}.tar.gz" + fi + + for d in $(seq $(date -d "$START_DATE" "+%Y%m%d") $(date -d "$END_DATE" "+%Y%m%d")); do + # Find and list files matching the date in the filename and modification time, append to tempfile + find $RELATIVE_PATH \( -type f -name "*$d*.txt" -o -name "*.log" -newermt "$START_DATE" ! -newermt "$END_DATE" \) -exec bash -c 'echo "${1#./}" >> "$2"' _ {} "$tempfile" \; + done + + echo "Compressing logs from $START_DATE to $END_DATE ..." + else + OUTPUT_FILE="bitwarden-logs-all.tar.gz" + find $RELATIVE_PATH -type f -exec bash -c 'echo "${1#./}" >> "$2"' bash {} "$tempfile" \; + echo "Compressing all logs..." + fi + + tar -czvf "$OUTPUT_FILE" -T "$tempfile" + echo "Logs compressed into $(pwd $OUTPUT_FILE)/$OUTPUT_FILE" + rm $tempfile +} + function listCommands() { cat << EOT Available commands: @@ -139,6 +201,7 @@ updateconf uninstall renewcert rebuild +compresslogs help See more at https://bitwarden.com/help/article/install-on-premise/#script-commands-reference @@ -195,6 +258,10 @@ case $1 in checkOutputDirExists $SCRIPTS_DIR/run.sh uninstall $OUTPUT ;; + "compresslogs") + # checkOutputDirExists + compressLogs $OUTPUT $2 $3 + ;; "help") listCommands ;; From b3e8d7098ecbe385d1320fd7ef8ab8464668140c Mon Sep 17 00:00:00 2001 From: fer Date: Wed, 6 Mar 2024 15:51:56 +0100 Subject: [PATCH 2/4] removed commented checkOutputDirExists --- bitwarden.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bitwarden.sh b/bitwarden.sh index 5eca92e0..fc9621a0 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -259,7 +259,7 @@ case $1 in $SCRIPTS_DIR/run.sh uninstall $OUTPUT ;; "compresslogs") - # checkOutputDirExists + checkOutputDirExists compressLogs $OUTPUT $2 $3 ;; "help") From 20d78b43b92991364088033cc6623a603e7eb9d9 Mon Sep 17 00:00:00 2001 From: fer Date: Wed, 6 Mar 2024 18:39:26 +0100 Subject: [PATCH 3/4] Update bitwarden.sh Co-authored-by: Vince Grassia <593223+vgrassia@users.noreply.github.com> --- bitwarden.sh | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/bitwarden.sh b/bitwarden.sh index fc9621a0..4e3a551f 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -123,28 +123,26 @@ function checkOutputDirNotExists() { fi } -validateDateFormat() { - if ! [[ $1 =~ ^[0-9]{4}[0-9]{2}[0-9]{2}$ ]]; then - echo "Error: $2 date format is invalid. Please use YYYYMMDD." - exit 1 - fi -} - -validateDateOrder() { - if [[ $(date -d "$1") > $(date -d "$2") ]]; then - echo "Error: start date ($1) must be earlier than end date ($2)." - exit 1 - fi -} - function compressLogs() { - OUTPUT=$1 + LOG_DIR=${1#$(pwd)/}/logs START_DATE=$2 END_DATE=$3 - LOG_DIR=$OUTPUT/logs - RELATIVE_PATH=${LOG_DIR#$(pwd)/} tempfile=$(mktemp) + function validateDateFormat() { + if ! [[ $1 =~ ^[0-9]{8}$ ]]; then + echo "Error: $2 date format is invalid. Please use YYYYMMDD." + exit 1 + fi + } + + function validateDateOrder() { + if [[ $(date -d "$1" +%s) > $(date -d "$2" +%s) ]]; then + echo "Error: start date ($1) must be earlier than end date ($2)." + exit 1 + fi + } + # Validate start date format if [ -n "$START_DATE" ]; then validateDateFormat "$START_DATE" "start" @@ -170,13 +168,13 @@ function compressLogs() { for d in $(seq $(date -d "$START_DATE" "+%Y%m%d") $(date -d "$END_DATE" "+%Y%m%d")); do # Find and list files matching the date in the filename and modification time, append to tempfile - find $RELATIVE_PATH \( -type f -name "*$d*.txt" -o -name "*.log" -newermt "$START_DATE" ! -newermt "$END_DATE" \) -exec bash -c 'echo "${1#./}" >> "$2"' _ {} "$tempfile" \; + find $LOG_DIR \( -type f -name "*$d*.txt" -o -name "*.log" -newermt "$START_DATE" ! -newermt "$END_DATE" \) -exec bash -c 'echo "${1#./}" >> "$2"' _ {} "$tempfile" \; done echo "Compressing logs from $START_DATE to $END_DATE ..." else OUTPUT_FILE="bitwarden-logs-all.tar.gz" - find $RELATIVE_PATH -type f -exec bash -c 'echo "${1#./}" >> "$2"' bash {} "$tempfile" \; + find $LOG_DIR -type f -exec bash -c 'echo "${1#./}" >> "$2"' bash {} "$tempfile" \; echo "Compressing all logs..." fi From d8c5c1369728b14b9d5fde57330bebfa302b50ce Mon Sep 17 00:00:00 2001 From: fer Date: Fri, 8 Mar 2024 17:23:23 +0100 Subject: [PATCH 4/4] added tabs to internal function (validateDateFormat) --- bitwarden.sh | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/bitwarden.sh b/bitwarden.sh index 4e3a551f..2362503e 100755 --- a/bitwarden.sh +++ b/bitwarden.sh @@ -130,10 +130,10 @@ function compressLogs() { tempfile=$(mktemp) function validateDateFormat() { - if ! [[ $1 =~ ^[0-9]{8}$ ]]; then - echo "Error: $2 date format is invalid. Please use YYYYMMDD." - exit 1 - fi + if ! [[ $1 =~ ^[0-9]{8}$ ]]; then + echo "Error: $2 date format is invalid. Please use YYYYMMDD." + exit 1 + fi } function validateDateOrder() {