Butler 2.3.2: dauerhafter Vaultwarden-Refresh #3
1 changed files with 50 additions and 14 deletions
|
|
@ -1,20 +1,51 @@
|
||||||
#!/bin/bash
|
#!/bin/bash
|
||||||
# vault-sync.sh - Sync Vaultwarden items to Butler cache volume
|
# vault-sync.sh - Sync Vaultwarden items to Butler's persistent cache volume.
|
||||||
# Run via cron: */30 * * * * /app-config/homelab-butler/vault-sync.sh
|
# Runtime credentials live in .vault-sync.env (mode 0600, never in Git).
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
|
|
||||||
export BW_PASSWORD="8yRG5LADfoTLHdC1Oj"
|
ENV_FILE="${VAULT_SYNC_ENV:-/app-config/homelab-butler/.vault-sync.env}"
|
||||||
CACHE_DIR=$(sudo docker inspect homelab-butler --format '{{range .Mounts}}{{if eq .Destination "/data/vault-cache"}}{{.Source}}{{end}}{{end}}' 2>/dev/null)
|
if [[ ! -r "$ENV_FILE" ]]; then
|
||||||
|
echo "vault-sync: protected environment file missing or unreadable" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
[ -z "$CACHE_DIR" ] && echo "Butler container not found" && exit 1
|
set -a
|
||||||
|
# shellcheck disable=SC1090
|
||||||
|
source "$ENV_FILE"
|
||||||
|
set +a
|
||||||
|
|
||||||
|
: "${BW_CLIENTID:?BW_CLIENTID missing}"
|
||||||
|
: "${BW_CLIENTSECRET:?BW_CLIENTSECRET missing}"
|
||||||
|
if [[ -z "${BW_PASSWORD:-}" && -n "${BW_MASTER_PASSWORD:-}" ]]; then
|
||||||
|
export BW_PASSWORD="$BW_MASTER_PASSWORD"
|
||||||
|
fi
|
||||||
|
: "${BW_PASSWORD:?BW_PASSWORD missing}"
|
||||||
|
|
||||||
|
CACHE_DIR=$(sudo docker inspect homelab-butler --format '{{range .Mounts}}{{if eq .Destination "/data/vault-cache"}}{{.Source}}{{end}}{{end}}' 2>/dev/null)
|
||||||
|
if [[ -z "$CACHE_DIR" ]]; then
|
||||||
|
echo "vault-sync: Butler cache volume not found" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
BW_STATE=$(bw status 2>/dev/null | python3 -c 'import json,sys; print(json.load(sys.stdin).get("status", "unknown"))')
|
||||||
|
if [[ "$BW_STATE" == "unauthenticated" ]]; then
|
||||||
|
bw login --apikey --raw >/dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
SESSION=$(bw unlock --passwordenv BW_PASSWORD --raw 2>/dev/null)
|
SESSION=$(bw unlock --passwordenv BW_PASSWORD --raw 2>/dev/null)
|
||||||
[ -z "$SESSION" ] && echo "Vault unlock failed" && exit 1
|
if [[ -z "$SESSION" ]]; then
|
||||||
|
echo "vault-sync: Vault unlock failed" >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
bw sync --session "$SESSION" >/dev/null 2>&1
|
bw sync --session "$SESSION" >/dev/null
|
||||||
|
|
||||||
|
bw list items --session "$SESSION" | sudo python3 -c "
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import re
|
||||||
|
import sys
|
||||||
|
|
||||||
bw list items --session "$SESSION" 2>/dev/null | sudo python3 -c "
|
|
||||||
import sys, json, os
|
|
||||||
items = json.load(sys.stdin)
|
items = json.load(sys.stdin)
|
||||||
cache_dir = '$CACHE_DIR'
|
cache_dir = '$CACHE_DIR'
|
||||||
os.makedirs(cache_dir, exist_ok=True)
|
os.makedirs(cache_dir, exist_ok=True)
|
||||||
|
|
@ -22,10 +53,15 @@ count = 0
|
||||||
for item in items:
|
for item in items:
|
||||||
name = item.get('name', '')
|
name = item.get('name', '')
|
||||||
notes = item.get('notes') or ''
|
notes = item.get('notes') or ''
|
||||||
if name and notes:
|
safe = re.sub(r'[^a-z0-9._-]+', '-', name.lower()).strip('.-')
|
||||||
safe = name.lower().replace(' ', '-')
|
if not safe or not notes:
|
||||||
with open(f'{cache_dir}/{safe}', 'w') as f:
|
continue
|
||||||
f.write(notes.strip())
|
path = os.path.join(cache_dir, safe)
|
||||||
|
tmp = path + '.tmp'
|
||||||
|
with open(tmp, 'w') as handle:
|
||||||
|
handle.write(notes.strip())
|
||||||
|
os.chmod(tmp, 0o600)
|
||||||
|
os.replace(tmp, path)
|
||||||
count += 1
|
count += 1
|
||||||
print(f'vault-sync: {count} items written')
|
print(f'vault-sync: {count} items written')
|
||||||
"
|
"
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue