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