Skip to content

Commit

Permalink
feat:shoutrrr enabled with NOTIFICATION_URL variable.
Browse files Browse the repository at this point in the history
The container will send a notification when an error occurs.
  • Loading branch information
0neTx committed Feb 18, 2024
1 parent a4582c7 commit 2f4de84
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 28 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Expand Up @@ -41,4 +41,5 @@ export/
.vscode/
.secrets
docker-compose.prod.yml
docker-compose.*.yml
docker-compose.*.yml
.infisical.json
27 changes: 10 additions & 17 deletions bw_export.sh
Expand Up @@ -18,9 +18,7 @@
# could not be exported.

# Constant and global variables

BITWARDENCLI_APPDATA_DIR="${BITWARDENCLI_APPDATA_DIR:-"$(pwd)"}"

params_validated=0
Yellow='\033[0;33m' # Yellow
IYellow='\033[0;93m' # Yellow
Expand Down Expand Up @@ -62,7 +60,7 @@ fi
# bw login #(follow the prompts);
if [[ -z "${BW_URL_SERVER}" ]]; then
echo -e -n "$Cyan" # set text = yellow
echo -e "\nInfo: BW_SERVER enviroment not provided."
echo -e "\nInfo: BW_URL_SERVER enviroment not provided."

echo -n "$(date '+%F %T') If you have your own Bitwarden or Vaulwarden server, set in the environment variable BW_URL_SERVER its url address. "
echo -n "$(date '+%F %T') Example: https://skynet-vw.server.com"
Expand Down Expand Up @@ -123,10 +121,6 @@ fi
#Set Organization ID (if applicable)
if [[ -z "${BW_ORGANIZATIONS_LIST}" ]]; then
echo -e "\n$(date '+%F %T') ${Cyan} BW_ORGANIZATIONS_LIST enviroment not provided. All detected organizations will be exported. "
echo -e "${Cyan} If you want to make a backup of specific organizations, set one or more organizations separated by comma"
echo -e "${Cyan} To obtain your organization_id value, open a terminal and type:"
echo -e "${Cyan} bw login #(follow the prompts); bw list organizations | jq -r '.[0] | .id'"
echo -e "${Cyan} Example: cada13d7-5418-37ed-981b-be822121c593,cada13d7-5418-37ed-981b-be82219879878979,cada13d7-5418-37ed-981b-be822121c5435"
else
organization_list="${BW_ORGANIZATIONS_LIST}"
fi
Expand Down Expand Up @@ -163,37 +157,36 @@ echo

if [[ $bw_url_server != "" && $bw_url_server != *"bitwarden.com" ]]; then
echo "$(date '+%F %T') Setting custom server..."
bw config server "$bw_url_server" --quiet --nointeraction
bw config server "$bw_url_server" --nointeraction
echo
fi

BW_CLIENTID=$client_id
BW_CLIENTSECRET=$client_secret

#Login user if not already authenticated
if [[ $(bw status | jq -r .status) == "unauthenticated" ]]; then
echo "$(date '+%F %T') Performing login..."
bw login --apikey --method 0 --quiet --nointeraction
fi
if [[ $(bw status | jq -r .status) == "unauthenticated" ]]; then
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Failed to authenticate."
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Failed to login."
echo
exit 1
fi

#Unlock the vault
echo "$(date '+%F %T') Unlocking the vault..."
session_key=$(bw unlock "$bw_password" --raw)

#Verify that unlock succeeded
if [[ $session_key == "" ]]; then
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Failed to authenticate."

if [[ $session_key == "" ]] || [[ " $session_key" == "0" ]]; then
echo -e "\n$(date '+%F %T') ${IYellow}ERROR: Failed to unlock vault with BW_PASSWORD."
exit 1
else
echo "$(date '+%F %T') Login successful."
echo "$(date '+%F %T') Vault unlocked."
fi
#Export the session key as an env variable (needed by BW CLI)
export BW_SESSION="$session_key"
echo

#Check if the user has decided to enter a password or save unencrypted
if [[ $password1 == "" ]]; then
Expand Down Expand Up @@ -309,7 +302,7 @@ if [ -n "${KEEP_LAST_BACKUPS}" ]; then
echo -e "$(date '+%F %T') ${Cyan}Info: Max Nº backups: $keep_backups"
if [[ $actual_num_backups -gt $keep_backups ]]; then
for F in $(find "$save_folder" -path "*-bw-export" -type d | sort | head -"$(("$actual_num_backups" - "$keep_backups"))"); do
echo -e "\n$(date '+%F %T') ${Blue} Deleting exported vault:$F"
echo -e "$(date '+%F %T') ${Blue} Deleting exported vault:$F"
rm -rf "$F"
done
fi
Expand All @@ -319,7 +312,7 @@ if [ -n "${KEEP_LAST_BACKUPS}" ]; then
echo -e "\n$(date '+%F %T') ${Cyan}Info: Nº backups: $actual_num_backups"
echo -e "$(date '+%F %T') ${Cyan}Info: Max Nº backups: $keep_backups"
for F in $(find "$save_folder_attachments" -path "*-bw-export" -type d | sort | head -$(("$actual_num_backups" - "$keep_backups"))); do
echo -e "\n$(date '+%F %T') ${Blue} Deleting exported attachment:$F"
echo -e "$(date '+%F %T') ${Blue} Deleting exported attachment:$F"
rm -rf "$F"
done
fi
Expand Down
39 changes: 29 additions & 10 deletions entrypoint.sh
Expand Up @@ -3,24 +3,43 @@

Yellow='\033[0;33m' # Yellow
Cyan='\033[0;36m' # Cyan

send_notification () {
set -xe
echo -e "\n${Cyan}Notification enabled: $NOTIFICATION_URL "
/app/shoutrrr send -m "$(tail -n 15 "$logfile")" -u "$NOTIFICATION_URL"
}

#Base Command
command="su bitwarden -c /app/bw_export.sh "
if [[ -n "${FILE_LOG}" ]]; then
echo -e "\n${Cyan}Output log enabled: $FILE_LOG "
command="$command 2>&1 | tee $FILE_LOG"
fi
logfile=/app/bw-export-logfile.log

if [[ -n "${INFISICAL_TOKEN}" ]]; then

if [[ -n "${INFISICAL_PATH}" ]]; then
infisicalpath="--path=${INFISICAL_PATH}"
fi

echo -e "\n${Yellow}Infisical enabled! "
infisical run "$infisicalpath" --command="echo Mi clinete es:$BW_CLIENTID"
returncode=$?
if [[ -n "${NOTIFICATION_URL}" ]] && [[ "$returncode" -ne "0" ]]; then
send_notification
exit 1
fi
command="infisical run $infisicalpath --command='$command'"
fi
eval $command
if [[ -n "${NOTIFICATION_URL}" ]]; then
echo -e "\n${Cyan}Notification enabled: $NOTIFICATION_URL "
/app/shoutrrr send -m "$(cat $FILE_LOG)" -u "$NOTIFICATION_URL"

if [[ -n "${FILE_LOG}" ]]; then
echo -e "\n${Cyan}Output log enabled: $FILE_LOG "
logfile=$FILE_LOG
fi

command="$command 2>&1 | tee $logfile"
set -o pipefail
eval "$command"
return=$?
echo Return Code: $return

if [[ -n "${NOTIFICATION_URL}" ]] && [[ "$return" -ne "0" ]]; then
send_notification
fi

0 comments on commit 2f4de84

Please sign in to comment.