vitorpy's Dotfiles
at main 81 lines 1.8 kB view raw
1#!/bin/bash 2set -e 3 4# Check if Bitwarden session is active 5if [ -z "$BW_SESSION" ]; then 6 echo "ERROR: BW_SESSION not set. Run: export BW_SESSION=\$(bw unlock --raw)" 7 exit 1 8fi 9 10# Check for jq 11if ! command -v jq &> /dev/null; then 12 echo "ERROR: jq is required but not installed" 13 exit 1 14fi 15 16SECRETS_DIR="$HOME/backup-secrets" 17 18if [ ! -d "$SECRETS_DIR" ]; then 19 echo "ERROR: Directory $SECRETS_DIR does not exist" 20 exit 1 21fi 22 23echo "==> Backing up secrets from $SECRETS_DIR to Bitwarden..." 24echo "" 25 26# Counter for stats 27total=0 28uploaded=0 29skipped=0 30 31# Loop through all files in the directory 32for 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)) 75done 76 77echo "" 78echo "==> Backup complete!" 79echo " Total files: $total" 80echo " Uploaded: $uploaded" 81echo " Skipped (already exist): $skipped"