Add Bitwarden backup and restore for secretctl stores

This commit is contained in:
Ziad Arafat committed 2026-12-02 10:36:38 -07:00
1 parent 4b6d1a36fc
commit 9fdd63a6eb
2 files changed
+70 -1

No files matched your search

+25
View File
@@ -119,6 +119,31 @@ bootstrap, open a new terminal to pick up the installed tools.
details, and [Git authentication](docs/git-auth.md) for the separate Git
credential sessions.
#### Back up and restore the secretctl store
With `bw` unlocked on the intended vault, run this on the source machine:
```bash
dot secrets push-secretctl
```
This saves all entries in the secretctl store as one encrypted secure note
named `dotfiles/secretctl`. Repeating the command replaces that snapshot.
On the destination machine, unlock the same vault and restore the snapshot:
```bash
dot secrets pull-secretctl
secretctl check
```
The default file is `~/secrets.txt` (or `$SECRETCTL_FILE`, if set). Restore
creates a validated `0600` file and refuses to replace an existing file.
To restore separately for review, run
`dot secrets pull-secretctl "$HOME/secrets-restored.txt"` and validate it
with `secretctl --file "$HOME/secrets-restored.txt" check`.
These commands keep the secretctl entries separate from `dot secrets pull`
and its shell-sourced `secrets.env`.
After importing your secrets:
```bash
+45 -1
View File
@@ -538,6 +538,47 @@ secrets_render() {
# Do not write a plaintext ~/.git-credentials or modify an existing one.
}
# Keep the secretctl snapshot out of secrets.env, which is shell-sourced.
secrets_push_secretctl() (
[ "$#" -le 1 ] || die "usage: dot secrets push-secretctl [SOURCE]"
have secretctl || die "secretctl is required"
local src="${1:-${SECRETCTL_FILE:-$HOME/secrets.txt}}" keys key stage jq_bin
keys="$(secretctl --file "$src" list)" || die "Could not list secrets"
[ -n "$keys" ] || die "No secrets to upload"
jq_bin="$(command -v jq)" || die "jq is required"
bw_ready
bw sync >/dev/null
umask 077
stage="$(mktemp)"
trap 'rm -f -- "$stage"' EXIT
while IFS= read -r key; do
secretctl --file "$src" get "$key" -- "$jq_bin" -jn --arg key "$key" 'env[$key]' |
secretctl --file "$stage" add "$key" --allow-empty >/dev/null ||
die "Could not export secretctl entry $key"
done <<<"$keys"
bw_put_note "$BW_PREFIX/secretctl" "$stage"
)
secrets_pull_secretctl() (
[ "$#" -le 1 ] || die "usage: dot secrets pull-secretctl [DESTINATION]"
have secretctl || die "secretctl is required"
local dest="${1:-${SECRETCTL_FILE:-$HOME/secrets.txt}}" stage
if [ -e "$dest" ] || [ -L "$dest" ]; then
die "$dest already exists; choose a new destination"
fi
bw_ready
bw sync >/dev/null
umask 077
stage="$(mktemp -- "$(dirname -- "$dest")/.secretctl-import.XXXXXXXX")"
trap 'rm -f -- "$stage"' EXIT
bw_find "$BW_PREFIX/secretctl" |
jq -je 'select(type == "object" and .type == 2) | .notes | select(type == "string" and length > 0)' >"$stage" ||
die "No valid secretctl secure note in this vault"
secretctl --file "$stage" check >/dev/null 2>&1 || die "Vault snapshot failed secretctl validation"
ln -T -- "$stage" "$dest" || die "Could not create $dest without overwriting"
ok "Secretctl file restored at $dest (0600)"
)
cmd_secrets() {
local sub="${1:-}"; shift || true
case "$sub" in
@@ -547,7 +588,9 @@ cmd_secrets() {
add) secrets_add "$@" ;;
set) secrets_set "$@" ;;
render) secrets_render ;;
*) die "usage: dot secrets status|push [--wireguard]|pull [--wireguard]|add FILE...|set KEY [VALUE]|render" ;;
push-secretctl) secrets_push_secretctl "$@" ;;
pull-secretctl) secrets_pull_secretctl "$@" ;;
*) die "usage: dot secrets status|push [--wireguard]|pull [--wireguard]|add FILE...|set KEY [VALUE]|render|push-secretctl [SOURCE]|pull-secretctl [DESTINATION]" ;;
esac
}
@@ -795,6 +838,7 @@ dot — portable dev environment
dot nix install Nix if missing
dot apps [--native] profile apps (native on Fedora containers), mise tools, Claude Code
dot secrets status|push|pull|add|set|render Bitwarden-backed tokens, keys, files, WireGuard
dot secrets push-secretctl|pull-secretctl Back up or restore a secretctl file
dot defguard [--force] preconfigure the Defguard VPN app (one-time token minted with DEFGUARD_API_TOKEN)
dot wallpapers [--push] sort wallpapers into light/dark (or clone them)
dot sounds [build|test|notify] Portal 2 sound theme: build it, play every sound, or fire a test notification