Last active
May 20, 2025 10:13
-
-
Save kipavy/cefd01021460029b63d3a7a31477ac81 to your computer and use it in GitHub Desktop.
Export Bitwarden Vault as Encrypted ZIP & Upload to Gdrive
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/bin/bash | |
| # REPLACE WITH YOUR CREDENTIALS | |
| BW_EMAIL="" | |
| BW_PASSWORD="" | |
| GDRIVE_FOLDER_ID="" | |
| EXPORT_FILE="bitwarden_export.json" | |
| TIMESTAMP=$(date +"%Y%m%d%H%M%S") | |
| ZIP_FILE="bitwarden_export_$TIMESTAMP.zip" | |
| # if BW_EMAIL or BW_PASSWORD or GDRIVE_FOLDER_ID is empty, exit | |
| if [[ -z $BW_EMAIL || -z $BW_PASSWORD || -z $GDRIVE_FOLDER_ID ]] | |
| then | |
| echo "ERROR: Please fill in BW_EMAIL, BW_PASSWORD, and GDRIVE_FOLDER_ID." | |
| exit 1 | |
| fi | |
| # Install dependencies | |
| sudo apt-get update | |
| sudo apt-get install -y zip | |
| # Check if Node.js is installed | |
| if ! command -v node &> /dev/null; then | |
| echo "Node.js not found. Installing via nvm..." | |
| # Install nvm if not already present | |
| export NVM_DIR="$HOME/.nvm" | |
| if [ ! -d "$NVM_DIR" ]; then | |
| curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.0/install.sh | bash | |
| fi | |
| # Load nvm (works regardless of shell) | |
| export NVM_DIR="$HOME/.nvm" | |
| [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" | |
| # Install Node.js version 22 | |
| nvm install 22 | |
| fi | |
| npm install -g @bitwarden/cli | |
| install_gdrive() { | |
| if ! command -v gdrive &> /dev/null | |
| then | |
| curl -LO https://github.com/glotlabs/gdrive/releases/latest/download/gdrive_linux-x64.tar.gz | |
| tar xvf gdrive_linux-x64.tar.gz | |
| sudo install gdrive /usr/local/bin/gdrive | |
| rm gdrive_linux-x64.tar.gz gdrive | |
| echo "gdrive installed successfully to /usr/local/bin/gdrive" | |
| fi | |
| } | |
| install_gdrive | |
| bw_status() { | |
| bw status 2>/dev/null | grep -oP '"status":\s*"\K[^"]+' | |
| } | |
| #Login user if not already authenticated | |
| if [[ $(bw_status) == "unauthenticated" ]] | |
| then | |
| echo "Performing login..." | |
| bw login $BW_EMAIL $BW_PASSWORD --method 0 | |
| fi | |
| if [[ $(bw_status) == "unauthenticated" ]] | |
| then | |
| echo "ERROR: Failed to authenticate." | |
| echo | |
| exit 1 | |
| fi | |
| #Unlock the vault | |
| session_key=$(bw unlock $BW_PASSWORD --raw 2>/dev/null) | |
| #Verify that unlock succeeded | |
| if [[ $session_key == "" ]] | |
| then | |
| echo "ERROR: Failed to authenticate." | |
| echo | |
| exit 1 | |
| else | |
| echo "Login successful." | |
| echo | |
| fi | |
| #Export the session key as an env variable (needed by BW CLI) | |
| export BW_SESSION="$session_key" | |
| # Export Bitwarden vault | |
| bw export --format json --output $EXPORT_FILE 2>/dev/null | |
| # Zip the exported file with a password | |
| zip -P $BW_PASSWORD $ZIP_FILE $EXPORT_FILE | |
| if ! gdrive account current &> /dev/null | |
| then | |
| echo "gdrive not authenticated. Please authenticate." | |
| gdrive account add | |
| fi | |
| gdrive files list --query "'$GDRIVE_FOLDER_ID' in parents" | grep bitwarden_export | awk '{print $1}' | xargs -n 1 gdrive files delete | |
| gdrive files upload --parent $GDRIVE_FOLDER_ID $ZIP_FILE | |
| # Clean up | |
| rm $EXPORT_FILE | |
| rm $ZIP_FILE | |
| # Lock Bitwarden vault | |
| bw lock --session $BW_SESSION 2>/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment