smugglr manual / 03 cli reference

CLI reference

smugglr sync (with --discover), mesh, stash and retrieve, and exec: their flags, the places they read and write, the config file, and the exit codes.

## contents

01 Synopsis

# five commands

smugglr sync <source> <target> [flags]
smugglr mesh <place> --key-file <path> [--sync-mode stop|last]
smugglr mesh <place> --key-file <path> --replay <change hash>
smugglr mesh keygen --key-file <path>
smugglr stash <place> <dir> --key-file <path> --node <name> [--target <name>]... [--whole]
smugglr retrieve <place> <blob or dir> --key-file <path> --node <name> [--crossed-off <name>]... [--sync-mode stop|last] [--json]
smugglr retrieve <place> --key-file <path> --node <name> --replay <change hash> [--sync-mode stop|last] [--json]
smugglr mesh remove <node> --key-file <path>
smugglr exec <local> <place> --version <v> --up|--down --expect <fingerprint>
smugglr exec <database-or-place> --read
smugglr --version

02 Flags

# smugglr sync, all optional

flagwhat it does
--dryrunPlans the run and prints the record. Writes nothing.
--jsonPrints the record as JSON.
--sync-type direct|meshHow the places connect; direct is the default. mesh takes one place and is smugglr mesh. hub and multi are refused as not built yet.
--sync-mode <mode>What happens to a row changed on both sides: stop (the default), source, target or last. See Sync modes.
--table <name>Limits the run to that table. Repeat it to name more tables. The shape check still covers every table both places hold; ignore a table in the config to leave it out of the check.
--watch [seconds]Runs again every N seconds, 60 by default, with one record per run, until stopped. --watch 0 is a usage error.
--discoverWrites nothing. Says what each place is and what a sync between them would meet, first the findings a sync could not get past. Exits 3 when a sync would stop, 0 when it could go ahead.
$ smugglr sync laptop/shop.db server/shop.db --watch 0 2>&1 | head -1
--watch 0: give a number of seconds above 0

--discover prints, for each place, its identity (for D1, the account and the database’s uuid), its ledger, its tables with rows and bytes estimated from a sample of 1,000 rows, the copy order, self-references, triggers, views, indexes and ignored tables. A finding comes first and the exit code is 3:

$ smugglr sync laptop/shop.db server/shop.db --discover
blocking: the shapes differ: The table audit has different columns in the two places: who.
source: file ./laptop/shop.db
  ledger: none
  table audit: 0 rows, about 0 bytes, estimated from a sample of 0 rows
  table products: 3 rows, about 62 bytes, estimated from a sample of 3 rows
  copy order: products, audit
  self-references: none
  triggers: none
  views: none
  indexes: none
  ignored: none
target: file ./server/shop.db
  ledger: none
  table audit: 0 rows, about 0 bytes, estimated from a sample of 0 rows
  table products: 0 rows, about 0 bytes, estimated from a sample of 0 rows
  copy order: products, audit
  self-references: none
  triggers: none
  views: none
  indexes: none
  ignored: none
shapes: differ
$ echo $?
3

When nothing blocks, the same report ends shapes: the same and the exit code is 0. With --json the report carries identity, ledger, blocking and shapes_match. A sync that stops with exit 3 or 5 ends its stderr with the --discover command for the same two places.

$ smugglr sync laptop/shop.db server/shop.db --discover
source: file ./laptop/shop.db
  ledger: none
  table products: 3 rows, about 62 bytes, estimated from a sample of 3 rows
  copy order: products
  self-references: none
  triggers: none
  views: none
  indexes: none
  ignored: none
target: file ./server/shop.db
  ledger: none
  table products: 0 rows, about 0 bytes, estimated from a sample of 0 rows
  copy order: products
  self-references: none
  triggers: none
  views: none
  indexes: none
  ignored: none
shapes: the same
$ echo $?
0

A sync writes a parent’s rows before its children’s, so a place that enforces foreign keys, as D1 always does, takes them. smugglr --version and -V print the version and exit 0:

$ smugglr --version
smugglr 0.6.4

03 Sync modes

# --sync-mode

modea row changed on both sides
stopThe default. The row stops and is named.
sourceThe source’s copy is kept; each overwritten row is named.
targetThe target’s copy is kept; each overwritten row is named.
lastThe copy whose steering column is further on is kept. Needs a steering column set in the config.

Without a steering column, last is refused before anything is read:

$ smugglr sync laptop/shop.db server/shop.db --sync-mode last | head -1
--sync-mode last needs a steering column for: products. Name one in config, or use --sync-mode stop|source|target.

With steering = "updated_at" in the config, the preview and the run agree:

$ smugglr sync laptop/shop.db server/shop.db --sync-mode last --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 last
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

04 Places

# where a sync reads and writes

addressplacecredentials
./shop.dbA local SQLite file, by path. file:// also works.none
http://…, https://…A service that speaks HTTP-SQL.none sent: smugglr adds no credentials of its own to the request
d1://<database-name>A Cloudflare D1 database.CLOUDFLARE_ACCOUNT_ID and CLOUDFLARE_API_TOKEN, read from the environment; a missing one is refused by name, with exit 2. The token needs D1 edit permission.

smugglr keeps no secrets and has no environment of its own; it reads the two D1 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

Other database engines (postgres://, for example) are refused: smugglr keeps SQLite in step with SQLite. Every table needs a primary key; a table without one stops the run with exit 3. SQLite’s sqlite_* tables are always ignored, and so are D1’s _cf_* tables.

05 Stash and retrieve

# smugglr stash, smugglr retrieve

flagwhat it does
--key-file <path>The shared key that signs and verifies changes: 64 hex digits, or 32 bytes. Required.
--node <name>This device. Required.
--target <name>stash only. A device the blob is for; repeatable.
--wholestash only. Stash every row and the graveyard, not what changed since the last stash. A place’s first stash is whole.
--crossed-off <name>retrieve only. A device the app has crossed off the blob; repeatable.
--sync-mode stop|lastretrieve only. What happens to a row changed here and in a blob: stop (the default) names it, last keeps the copy whose steering column is further on. source and target are refused.
--replay <change hash>retrieve only. Takes no blob: decides the change the dropped log holds under that hash again, with a fresh sunset, overriding its drop. An unknown hash is a usage error, exit 2.
--jsonretrieve only. Prints the record as JSON.

stash writes one new blob into the directory and prints its path. retrieve applies one blob, or every blob in a directory, in any order.

$ 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

A retrieve names each change it drops with its change hash. --replay decides that change again:

$ smugglr retrieve b/shop.db blobs/1794465236862-6f18f17957e7fe1361529ad7089abd20.blob --key-file shop.key --node phone
smugglr sync: done
  dropped: notes n1, its key is in the graveyard, change 779adba9b26675911c2fe7172c5a69909fc06567a0b05ba999065b89a6d3bfb7
  targets remaining: 
$ smugglr retrieve b/shop.db --key-file shop.key --node phone --replay 779adba9b26675911c2fe7172c5a69909fc06567a0b05ba999065b89a6d3bfb7
smugglr sync: done
  notes: moved, 1 to target, 0 to source, 0 deleted in target, 0 deleted in source, 0 overwritten in target, 0 overwritten in source
$ echo $?
0

stash and retrieve are multi: a person’s own devices kept in step through blobs the app stores, where the devices are not online together. The blobs, the device list and the keys are the app’s. --sync-type multi is not how to run it and is refused as not built.

06 Exec

# smugglr exec, for migratr

smugglr exec runs a migration’s statements on a place for migratr, so that a schema change can reach a place such as D1 through smugglr. migratr 0.3.0 calls it from migratr dev, up --smuggl and down --smuggl; smugglr --help does not name it.

commandwhat it does
smugglr exec <local> <place> --version <v> --up|--down --expect <fingerprint>Reads a JSON array of statements on stdin and runs them on the place in one all-or-nothing batch with the version’s ledger row, or its removal for --down. It runs only when the batch’s SHA-256 equals the up or down hash the local database recorded for that version, and the place’s structural fingerprint is the one --expect names. A down batch needs the local database to have run the down first. It prints the place’s fingerprint after.
smugglr exec <database-or-place> --readPrints the schema, the ledger and the fingerprint, as one JSON object. It takes no query.
$ smugglr exec laptop/shop.db --read
{"fingerprint":"7dad90733bcedca3542b783a01852777ad8b05664eabdefe9cfe69dfd2a84d78","ledger":[],"schema":[{"name":"products","sql":"CREATE TABLE products (sku text primary key, name text not null, price integer not null)","tbl_name":"products","type":"table"},{"name":"sqlite_autoindex_products_1","sql":null,"tbl_name":"products","type":"index"},{"name":"audit","sql":"CREATE TABLE audit (id text primary key, who text)","tbl_name":"audit","type":"table"},{"name":"sqlite_autoindex_audit_1","sql":null,"tbl_name":"audit","type":"index"}]}

A D1 place and a file in the same shape have the same fingerprint. A refusal exits 3 and leaves the place as it was; a failing statement exits 1 with nothing changed; a usage error exits 2.

07 Mesh

# smugglr mesh

commandwhat it does
smugglr mesh <place> --key-file <path> [--sync-mode stop|last]Keeps <place> in step with every node on the local network that holds the same key, until stopped. smugglr sync <place> --sync-type mesh takes the same flags and is the same command.
smugglr mesh keygen --key-file <path>Appends a new key to the key file, making the file when there is none, and prints the key’s id.
smugglr mesh remove <node> --key-file <path>Tells the mesh a node is gone for good, so its peers stop owing it changes. A node heard again is back.
flagwhat it does
--key-file <path>The shared key: one base64 key per line, newest last; a line starting with # is a note. Required. On unix only its owner may read it (chmod 600); a looser file is refused.
--sync-mode stop|lastWhat happens to a row changed here and in a change. source and target are refused: they are for two places.
--replay <change hash>Decides the change the dropped log holds under that hash again, with a fresh sunset, overriding its drop; prints what came of it and exits. An unknown hash is a usage error, exit 2.
$ smugglr mesh keygen --key-file ./mesh.key
smugglr mesh: wrote key 90efbbd2a018fbfd to ./mesh.key
$ chmod 644 ./loose.key
$ smugglr mesh ./puck/node.db --key-file ./loose.key
./loose.key: the key file is readable by its group or others; chmod 600 ./loose.key fixes it
$ echo $?
2

A node signs and seals with the newest key and also accepts the one before it. The previous key is not retired in 0.6.4. No key is printed anywhere; a key is named by its id.

A node is named by its machine’s name (hostname, or COMPUTERNAME on Windows), and every node needs its own: a node that finds its name taken by a node already running says so and does not join, and exits 3. It broadcasts to find peers and sends to them by unicast; on a network with no broadcast, the peers setting names the addresses to announce to. A change too large for one datagram crosses in numbered parts, each answered and resent until the row arrives whole.

$ 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
$ sqlite3 ./node.db "insert into notes values('big', <30,000 characters>);"   # on puck
$ sqlite3 ./node.db "select id, length(body) from notes where id='big'"   # on rzr
big|30000
# rzr, last line of output
smugglr mesh: applied 1 change to notes from puck, key 90efbbd2a018fbfd

A change a node drops is named in the record with its change hash and kept in a dropped log. Here puck deletes a note and writes it again; rzr drops the second insert, then --replay decides it:

$ sqlite3 ./node.db "insert into notes values('n1','back again');"   # on puck, after the delete above
$ 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
smugglr mesh: dropped a change to notes ["'n1'"] from puck: its key is in the graveyard (change 0083b034bb6a5a025a8f0c2d0694d17e76a2e26f209d4e6101ab874446b8b92d)
$ smugglr mesh ./node.db --key-file ../mesh.key --replay 0083b034bb6a5a025a8f0c2d0694d17e76a2e26f209d4e6101ab874446b8b92d   # on rzr
smugglr mesh: applied 1 change to notes from puck, key 90efbbd2a018fbfd
smugglr mesh: replayed change 0083b034bb6a5a025a8f0c2d0694d17e76a2e26f209d4e6101ab874446b8b92d
$ echo $?
0
$ sqlite3 ./node.db "select * from notes"   # on rzr
n1|back again
settingdefaultwhat it does
port31415The UDP port a node broadcasts and listens on.
peersnoneAddresses to announce to: "host" or "host:port", the mesh port when none is given.
backpressure6 MBThe most the outbox of unacknowledged changes holds, in bytes or a size such as "6 MB". At the line the node takes no new changes in until acknowledgements bring it down.
graveyard12 hoursSeconds a change is carried; sets each change’s sunset.
mesh_wire8 MBThe line past which a node that is catching up takes a stash from a peer, which then travels past this line.
stash_max64 MBThe largest database a node offers a stash of, since a stash is held in memory at about 16 times its size.

The mesh is designed and tested for up to 50 nodes. A node away longer than the sunset prints the stash and retrieve commands that catch it up and keeps running; when an awake peer holds the rows, it takes a stash from that peer over the mesh. A node that restarts can be taken by the others for a second machine under its name; that is a known issue in 0.6.3 and 0.6.4 (#684), fixed after 0.6.4.

08 Config

# $XDG_CONFIG_HOME/smugglr/config.toml

smugglr reads config.toml from its config directory: $XDG_CONFIG_HOME/smugglr, else $HOME/.config/smugglr. The same directory holds smugglr’s memory of each pair of places. A setting smugglr does not read is left alone; a file that is not TOML, or a setting it cannot use, exits 2.

keywhat it sets
ignoreTables to ignore, as names or patterns with * (any run of characters) and ? (any one), matched without regard to case.
steeringThe steering column, by name, for all tables. A table can set its own under [tables.<name>]. --sync-mode last needs it.
max_wireThe most a first sync may send, in bytes or in units such as "250 MB" (decimal). 100 MB when unset.
graveyardSeconds a change is carried and a delete’s tombstone kept, for stash and retrieve. 12 hours by default.
steering = "updated_at"
ignore = ["audit", "cache_*"]
max_wire = "250 MB"

[tables.orders]
steering = "modified"

09 Exit codes

# scripts branch on these

codemeaning
0Done, or a preview.
2Usage error: a bad flag or place, a missing D1 variable, a config smugglr cannot read, a table without a steering column under --sync-mode last, or a key file or --node that stash or retrieve needs, a key file the mesh refuses, or a mesh given more than one place.
3Stopped before writing anything: a row changed in both places, a shape that differs, a table without a primary key, a --discover that finds a sync would stop, a mesh node whose name is taken, or an exec that refuses.
4Wrote something and is not done: a table stopped after another moved, a row read back different, or the run was cut off. Run it again; a stopped table stops again until its rows are settled.
5Too much for the wire. Nothing was sent.
$ 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>

Exit 5 above: the record names the stash and retrieve commands to move the rows, with the flags they need. The code comes from what the run did. A crash cannot say whether it wrote, so it exits 4: running again is the next step either way.

10 The record

# printed by every run

Every run prints a record of what it did; --json prints it as JSON. Get started shows how to read it. A stopped table names its rows in the record:

$ smugglr sync laptop/shop.db server/shop.db --json
{
  "status": "stopped",
  "first_sync": false,
  "deletes_seen": true,
  "tables": {
    "products": {
      "status": "stopped",
      "rows": [
        "sku-0412"
      ],
      "why": "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."
    }
  },
  "ignored": []
}
To see what the places are and what a sync meets: smugglr sync laptop/shop.db server/shop.db --discover

A shape difference is carried under shape:

$ smugglr sync laptop/shop.db server/shop.db --json
{
  "status": "stopped",
  "first_sync": false,
  "deletes_seen": false,
  "tables": {},
  "ignored": [],
  "shape": {
    "difference": "columns",
    "table": "audit",
    "columns": [
      {
        "name": "who",
        "source": {
          "position": 1,
          "type": "TEXT"
        },
        "target": null
      }
    ],
    "why": "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

11 Not in 0.6.4

# refused, not built

--sync-type hub and multi are not built. The CLI says so and exits 2; do not plan around them.

$ smugglr sync laptop/shop.db server/shop.db --sync-type hub 2>&1 | head -1
--sync-type hub is not built yet
  • Retiring the previous mesh key. A node accepts the key before the newest with no end date; nothing removes it but taking it out of the key file.