vitorpy's Dotfiles

Add script to backup secrets directory to Bitwarden

vitorpy 5e6f1c32 bd0e6d7f

+81
+81
private_dot_config/private_arch/executable_backup-secrets-to-bitwarden.sh
··· 1 + #!/bin/bash 2 + set -e 3 + 4 + # Check if Bitwarden session is active 5 + if [ -z "$BW_SESSION" ]; then 6 + echo "ERROR: BW_SESSION not set. Run: export BW_SESSION=\$(bw unlock --raw)" 7 + exit 1 8 + fi 9 + 10 + # Check for jq 11 + if ! command -v jq &> /dev/null; then 12 + echo "ERROR: jq is required but not installed" 13 + exit 1 14 + fi 15 + 16 + SECRETS_DIR="$HOME/backup-secrets" 17 + 18 + if [ ! -d "$SECRETS_DIR" ]; then 19 + echo "ERROR: Directory $SECRETS_DIR does not exist" 20 + exit 1 21 + fi 22 + 23 + echo "==> Backing up secrets from $SECRETS_DIR to Bitwarden..." 24 + echo "" 25 + 26 + # Counter for stats 27 + total=0 28 + uploaded=0 29 + skipped=0 30 + 31 + # Loop through all files in the directory 32 + for file in "$SECRETS_DIR"/*; do 33 + # Skip if not a file 34 + if [ ! -f "$file" ]; then 35 + continue 36 + fi 37 + 38 + total=$((total + 1)) 39 + 40 + # Get filename without path 41 + filename=$(basename "$file") 42 + 43 + # Item name in Bitwarden 44 + item_name="Secret - $filename" 45 + 46 + echo "[$total] Processing: $filename" 47 + 48 + # Check if item already exists 49 + if bw get item "$item_name" --session "$BW_SESSION" &>/dev/null; then 50 + echo " ⏭ Already exists in Bitwarden, skipping" 51 + skipped=$((skipped + 1)) 52 + continue 53 + fi 54 + 55 + # Read file contents 56 + file_contents=$(cat "$file") 57 + 58 + # Create secure note in Bitwarden 59 + jq -n \ 60 + --arg name "$item_name" \ 61 + --arg notes "$file_contents" \ 62 + '{ 63 + organizationId: null, 64 + folderId: null, 65 + type: 2, 66 + name: $name, 67 + notes: $notes, 68 + secureNote: { 69 + type: 0 70 + } 71 + }' | bw encode | bw create item --session "$BW_SESSION" > /dev/null 72 + 73 + echo " ✓ Uploaded to Bitwarden" 74 + uploaded=$((uploaded + 1)) 75 + done 76 + 77 + echo "" 78 + echo "==> Backup complete!" 79 + echo " Total files: $total" 80 + echo " Uploaded: $uploaded" 81 + echo " Skipped (already exist): $skipped"