# Profile State Recovery From Standby

If a profile's state.db on the live app1 server is smaller than expected after migration, the standby box (app1-bu) may still have the full copy.

## Procedure

```bash
# 1. Check standby
ssh -i ~/.ssh/itpp-infra root@5.161.114.8 "ls -la ~/.hermes/profiles/<name>/state.db"

# 2. Pull from standby if larger
scp -i ~/.ssh/itpp-infra root@5.161.114.8:/root/.hermes/profiles/<name>/state.db \
  ~/.hermes/profiles/<name>/state.db.live

# 3. Swap on live (gateway continues running; profile gateway may need restart)
cd ~/.hermes/profiles/<name>
mv state.db state.db.partial
mv state.db.live state.db
chmod 644 state.db

# 4. Verify
python3 -c "
import sqlite3, datetime
db = sqlite3.connect('~/.hermes/profiles/<name>/state.db')
c = db.cursor()
c.execute('SELECT COUNT(*) FROM messages')
msgs = c.fetchone()[0]
c.execute('SELECT COUNT(DISTINCT session_id) FROM messages')
sessions = c.fetchone()[0]
c.execute('SELECT MIN(timestamp), MAX(timestamp) FROM messages')
dr = c.fetchone()
print(f'Messages: {msgs}, Sessions: {sessions}')
print(f'Date range: {datetime.datetime.fromtimestamp(dr[0])} -> {datetime.datetime.fromtimestamp(dr[1])}')
"

# 5. If the profile gateway was running, restart it to pick up the new state
XDG_RUNTIME_DIR=/run/user/0 busctl call --user org.freedesktop.systemd1 \
  /org/freedesktop/systemd1 org.freedesktop.systemd1.Manager RestartUnit \
  'ss' 'hermes-gateway-<profile>.service' 'replace'
```

## Why This Happens

The 15-min S3 live backup (`s3://hermes-vps-backups/live/profiles/<name>/state.db`) captures the *destination* server's state, which may be the partial copy. The *origin* server's (or standby's) copy has the full data. Always check the standby before declaring a migration complete.
