Added error handling and logging

This commit is contained in:
Roger Gonzalez 2024-02-20 11:49:26 -03:00
parent 821567c515
commit c4f8684cc7
Signed by: rogs
GPG Key ID: C7ECE9C6C36EC2E6

View File

@ -1,6 +1,12 @@
#!/bin/sh #!/bin/sh
# Login to a custom Bitwarden instance # Check that the database password is set
if [ -z "$DATABASE_PASSWORD" ]; then
echo "DATABASE_PASSWORD is not set"
exit 1
fi
# If BITWARDEN_URL is not empty, set a custom Bitwarden instance
if [ "$BITWARDEN_URL" ]; then if [ "$BITWARDEN_URL" ]; then
echo "Connecting to Bitwarden instance at $BITWARDEN_URL" echo "Connecting to Bitwarden instance at $BITWARDEN_URL"
bw config server "$BITWARDEN_URL" bw config server "$BITWARDEN_URL"
@ -9,16 +15,30 @@ fi
BW_SESSION="$(bw login --raw)" BW_SESSION="$(bw login --raw)"
export BW_SESSION export BW_SESSION
if [ -z "$BW_SESSION" ]; then
echo "Failed to log in to Bitwarden"
exit 1
fi
# Set environment variables for the script # Set environment variables for the script
BW_PATH="$(which bw)" BW_PATH="$(which bw)"
export BW_PATH export BW_PATH
if [ -z "$DATABASE_NAME" ]; then
DATABASE_NAME="bitwarden.kdbx"
fi
DATABASE_PATH="/exports/$DATABASE_NAME" DATABASE_PATH="/exports/$DATABASE_NAME"
export DATABASE_PATH export DATABASE_PATH
# Convert the Bitwarden data to a KeePass file # Convert the Bitwarden data to a KeePass file
bw sync bw sync || { echo "Failed to sync Bitwarden data"; exit 1; }
python3 bitwarden-to-keepass.py echo "Generating KeePass file $DATABASE_PATH"
python3 bitwarden-to-keepass.py || { echo "Failed to convert to KeePass"; exit 1; }
bw lock bw lock
echo "KeePass file $DATABASE_NAME generated successfully" echo "KeePass file $DATABASE_PATH generated successfully"
# Log out of Bitwarden
bw logout