: bharatvaj/scripts

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
#!/bin/sh
# Copy Pi agent model/provider tokens from this machine to another host.
# Usage: copy-pi-agent-model-tokens.sh [ssh-host]

set -eu

host="$1"
[ -z "$host" ] && { echo "Usage $0 <host>"; exit 1; }
src_dir=${PI_AGENT_DIR:-$HOME/.pi/agent}
files="auth.json models-store.json"

for f in $files; do
    if [ ! -f "$src_dir/$f" ]; then
        echo "missing: $src_dir/$f" >&2
        exit 1
    fi
done

# shellcheck disable=SC2088
remote_dir='~/.pi/agent'
stamp=$(date +%Y%m%d-%H%M%S)

ssh "$host" "mkdir -p $remote_dir && chmod 700 ~/.pi ~/.pi/agent && for f in $files; do [ ! -e $remote_dir/\$f ] || cp -p $remote_dir/\$f $remote_dir/\$f.bak-$stamp; done"

# Stream a tar archive so token contents never appear in argv, logs, or temp files.
(
    cd "$src_dir"
    tar cf - $files
) | ssh "$host" "cd $remote_dir && tar xf - && chmod 600 $files"

echo "copied Pi agent token files to $host:$remote_dir"
echo "remote backups, if any, are named *.bak-$stamp"