smugglr manual / 01 get started

Get started

Install smugglr, run a first sync between two SQLite files, and read what it tells you.

## contents

01 Install

# one binary

The GitHub release for tag v0.6.4 carries one archive per target: smugglr-<target>.tar.gz for x86_64-unknown-linux-gnu, aarch64-unknown-linux-gnu, x86_64-apple-darwin and aarch64-apple-darwin, and smugglr-x86_64-pc-windows-msvc.zip for Windows. Download the one for your machine from the releases page, which shows each archive's SHA-256.

On Linux and macOS, x86_64 or aarch64, one line fetches the latest release's binary into ~/.local/bin/smugglr:

curl -fsSL https://smugglr.dev/install | bash

02 Your first sync

# source first, target second

Name two places. The first is the source; the second is the target. --dryrun plans the run, prints the record, and writes nothing.

$ smugglr sync laptop/shop.db server/shop.db --dryrun
smugglr sync: preview
  first sync: a row on one side only is copied as new; deletes are not seen
  weight: 3 rows, 62 B in 1 table
  products: would move, 3 to target, 0 to source, 0 deleted in target, 0 deleted in source, 0 overwritten in target, 0 overwritten in source

The run prints the same record with the same counts. The first sync line says these two places have never been synced: a row on one side only is copied as new, and deletes are not seen. weight is what the run will carry.

$ smugglr sync laptop/shop.db server/shop.db
smugglr sync: done
  first sync: a row on one side only is copied as new; deletes are not seen
  weight: 3 rows, 62 B in 1 table
  products: moved, 3 to target, 0 to source, 0 deleted in target, 0 deleted in source, 0 overwritten in target, 0 overwritten in source
$ echo $?
0

Run it again. Nothing changed, so nothing moves.

$ smugglr sync laptop/shop.db server/shop.db
smugglr sync: done
  products: in step

Every block on this page was pasted from a run of the release binary, in a scratch directory, on local files; the mesh blocks come from two nodes on one machine.

03 After the first sync

# edits, inserts and deletes cross both ways

Between the runs above, a row was edited and another deleted in laptop/shop.db, and a row was inserted in server/shop.db. The preview names what would move; the run, here as JSON, moves exactly that. A delete is seen now because smugglr remembers the last sync.

$ smugglr sync laptop/shop.db server/shop.db --dryrun
smugglr sync: preview
  products: would move, 1 to target, 1 to source, 1 deleted in target, 0 deleted in source, 0 overwritten in target, 0 overwritten in source
    deleted in target: sku-0233
$ smugglr sync laptop/shop.db server/shop.db --json
{
  "status": "done",
  "first_sync": false,
  "deletes_seen": true,
  "tables": {
    "products": {
      "status": "moved",
      "to_target": 1,
      "to_source": 1,
      "deleted_in_target": [
        "sku-0233"
      ],
      "deleted_in_source": [],
      "overwritten_in_target": [],
      "overwritten_in_source": []
    }
  },
  "ignored": []
}

The next run finds the two files in step.

$ smugglr sync laptop/shop.db server/shop.db
smugglr sync: done
  products: in step

04 Read the record

# in step, moved or stopped

table statuswhat it says
in stepThe table has nothing to move.
movedRows crossed. The counts say how many, each way, and each deleted or overwritten row is named.
stoppedThe table names the rows it stopped on. The other tables still move.
run statuswhat it says
preview--dryrun. Nothing was written.
doneEvery table is in step or moved.
stoppedA table stopped, or the two places have different shapes. Nothing was written.
stopped in partA table stopped after another table moved. Exit code 4.
too muchA first sync heavier than max_wire. Nothing was sent.

In JSON the record carries status, first_sync, deletes_seen, tables and ignored. Why a row moves or stops is in How smugglr decides.

05 A row changed in both places

# stop, then say which copy wins

Here sku-0412 was edited in both files after the last sync. smugglr stops rather than choose, names the row, and exits 3.

$ smugglr sync laptop/shop.db server/shop.db
smugglr sync: stopped
  products: stopped
    rows: sku-0412
    These rows changed in both places since the last sync. smugglr stops rather than choose. Rerun with --sync-mode source or target to keep one place's copy, or name a steering column and use --sync-mode last.
To see what the places are and what a sync meets: smugglr sync laptop/shop.db server/shop.db --discover
$ echo $?
3

A stop in one table does not hold the others back. When another table moved first, the run is stopped in part and exits 4 instead.

$ smugglr sync a/shop.db b/shop.db
smugglr sync: stopped in part
  orders: moved, 1 to target, 0 to source, 0 deleted in target, 0 deleted in source, 0 overwritten in target, 0 overwritten in source
  products: stopped
    rows: sku-0412
    These rows changed in both places since the last sync. smugglr stops rather than choose. Rerun with --sync-mode source or target to keep one place's copy, or name a steering column and use --sync-mode last.
$ echo $?
4

--sync-mode source keeps the source’s copy and target the target’s. Each overwritten row is named in the preview and again in the run.

$ smugglr sync laptop/shop.db server/shop.db --sync-mode source --dryrun
smugglr sync: preview
  products: would move, 1 to target, 0 to source, 0 deleted in target, 0 deleted in source, 1 overwritten in target, 0 overwritten in source
    overwritten in target: sku-0412
$ smugglr sync laptop/shop.db server/shop.db --sync-mode source
smugglr sync: done
  products: moved, 1 to target, 0 to source, 0 deleted in target, 0 deleted in source, 1 overwritten in target, 0 overwritten in source
    overwritten in target: sku-0412

--sync-mode last keeps the copy whose steering column is further on. Name the column in the config first; without it the run is refused with exit 2 (see the CLI reference).

06 A hosted place

# Cloudflare D1

A D1 place is d1://<database-name>. smugglr reads the account and token from CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN; the token needs D1 edit permission. A missing variable is refused by name, with exit 2.

$ smugglr sync laptop/shop.db d1://shop 2>&1 | head -1
d1://shop needs CLOUDFLARE_ACCOUNT_ID, and it is not set

smugglr keeps no secrets and has no environment of its own. It reads the two variables from the environment it runs in; set them however you keep them:

CLOUDFLARE_ACCOUNT_ID=<account-id> CLOUDFLARE_API_TOKEN=<token> smugglr sync ./shop.db d1://shop --dryrun

Tables a sync should not touch in a shared database go in the config’s ignore list. smugglr checks that both places have the same shape before it reads a row, and a table the two places hold differently stops the run:

$ smugglr sync laptop/shop.db server/shop.db
smugglr sync: stopped
  shape: The table audit has different columns in the two places: who. smugglr stopped before reading any row and wrote nothing. smugglr never changes a shape. Give both places the same shape, then run again.
To see what the places are and what a sync meets: smugglr sync laptop/shop.db server/shop.db --discover
$ echo $?
3

Ignore the table, and the check skips it. D1’s own _cf_* tables are ignored without being listed.

# $XDG_CONFIG_HOME/smugglr/config.toml
ignore = ["audit"]
$ smugglr sync laptop/shop.db server/shop.db
smugglr sync: done
  ignored: audit
  products: in step

--table does not narrow the shape check: every table both places hold is checked, so a mismatch in a table you did not name still stops the run.

07 A large first sync

# stash, then retrieve

A first sync moves every row. For a large one, smugglr stash writes the rows into a signed blob in a directory, and smugglr retrieve applies the blob at the other place, so the move can go by any route that carries a file. Both sides share one key file holding 64 hex digits, and each names itself with --node.

openssl rand -hex 32 > shop.key
$ smugglr stash a/shop.db blobs --key-file shop.key --node laptop --whole
blobs/1794465236714-8780be7058f005966fdf839f996c43d1.blob
$ smugglr retrieve b/shop.db blobs --key-file shop.key --node phone
smugglr sync: done
  products: moved, 3 to target, 0 to source, 0 deleted in target, 0 deleted in source, 0 overwritten in target, 0 overwritten in source
  targets remaining: 
$ echo $?
0

Retrieving the same blob again changes nothing.

$ smugglr retrieve b/shop.db blobs --key-file shop.key --node phone
smugglr sync: done
  targets remaining: 

A sync between those two places afterwards still counts as a first sync, so it will not see deletes, but the rows already match and none move.

$ smugglr sync a/shop.db b/shop.db
smugglr sync: done
  first sync: a row on one side only is copied as new; deletes are not seen
  weight: 0 rows, 0 B in 0 tables
  products: in step

smugglr does not decide for you when a first sync is large. Set max_wire in the config and a first sync heavier than that stops with exit 5 and names stash and retrieve. Here max_wire = "10 B" trips on two small rows; the run exits 5 and sends nothing. The command in the record needs the key file and node name from above, and a directory for the blobs.

$ smugglr sync ./laptop/shop.db ./server/shop.db | sed "s#$PWD#.#g"
To see what the places are and what a sync meets: smugglr sync ./laptop/shop.db ./server/shop.db --discover
smugglr sync: too much
  first sync: a row on one side only is copied as new; deletes are not seen
  Too much for one sync: 2 rows, 40 B in 1 table.
  Move it with: smugglr stash ./laptop/shop.db <dir> --key-file <path> --node <name> --whole, then smugglr retrieve ./server/shop.db <dir> --key-file <path> --node <name>

08 A mesh of nodes

# smugglr mesh

On a local network where nobody is in charge, smugglr mesh keeps one database in step with every other node that holds the same key. Each change goes out as it happens, signed and sealed under the key, and is resent until each peer has decided it. One key file serves the whole mesh; keygen writes it, and you copy it to every node.

$ smugglr mesh keygen --key-file ./mesh.key
smugglr mesh: wrote key 90efbbd2a018fbfd to ./mesh.key

A node announces itself by broadcast and talks to the peers it hears by unicast. On a network with no broadcast (WireGuard, Tailscale, a cloud network) name the peers in the config instead. A node is named by its machine’s name, so every node needs its own. The nodes below share one machine, each with its own port, its own config and a one-line hostname script ahead of the real one on its path:

# puck
port = 31401
peers = ["127.0.0.1:31402"]

# rzr
port = 31402
peers = ["127.0.0.1:31401"]
graveyard = 3

graveyard = 3 on rzr is for the sunset demonstration below; leave it out for a real mesh.

$ smugglr mesh ./node.db --key-file ../mesh.key   # on puck
smugglr mesh: node puck on UDP port 31401, key 90efbbd2a018fbfd
$ smugglr mesh ./node.db --key-file ../mesh.key   # on rzr
smugglr mesh: node rzr on UDP port 31402, key 90efbbd2a018fbfd

A row written on puck, by any program, reaches rzr with no sync run. A delete crosses the same way.

$ sqlite3 ./node.db "insert into notes values('n1','first note');"   # on puck
$ sqlite3 ./node.db "select * from notes"   # on rzr
n1|first note
# rzr, output so far
smugglr mesh: node rzr on UDP port 31402, key 90efbbd2a018fbfd
smugglr mesh: applied 1 change to notes from puck, key 90efbbd2a018fbfd
$ sqlite3 ./node.db "delete from notes where id='n1'"   # on puck
$ sqlite3 ./node.db "select count(*) from notes"   # on rzr
0
# rzr, output so far
smugglr mesh: node rzr on UDP port 31402, key 90efbbd2a018fbfd
smugglr mesh: applied 1 change to notes from puck, key 90efbbd2a018fbfd
smugglr mesh: applied 1 change to notes from puck, key 90efbbd2a018fbfd

A node’s first run notes the rows already in its database and sends none of them. Use stash and retrieve to bring a new node up to date. A node that starts after being away longer than the sunset (the graveyard setting, 12 hours by default) says so and names the commands that catch it up. When an awake peer holds the rows, it also takes a stash from that peer over the mesh.

$ smugglr mesh ./node.db --key-file ../mesh.key   # on rzr, restarted after being stopped longer than its graveyard (3 seconds); the scratch directory is shown as .
smugglr mesh: node rzr on UDP port 31402, key 90efbbd2a018fbfd
smugglr mesh: this node has been away 6 seconds, longer than the sunset (3 seconds); changes made meanwhile may not reach it. Catch up with: smugglr stash <a peer's place> <dir> --key-file <path> --node <name> --whole, then smugglr retrieve ./node.db <dir> --key-file <path> --node rzr
smugglr mesh: taking a stash of 31096 bytes from puck
smugglr mesh: caught up by a stash from puck: 2 rows, retrieve Done

The CLI reference lists the mesh flags and settings, and How smugglr decides says how a change travels.

09 Next

# two more pages