litestream icon indicating copy to clipboard operation
litestream copied to clipboard

Add `snapshot` CLI command (or similar)

Open kevinlang opened this issue 3 years ago • 3 comments

First of all, thanks for creating this wonderful utility. I've long been a fan of SQLite3 and deeply appreciate this tool that makes using SQLite3 much more resilient!

I think it would be useful to have a snapshot CLI command for synchronously (without starting a server) creating a snapshot and pushing it to the replication target.

My rationale is that if I were to run litestream as a sidecar in Nomad via its lifecycle stanza, I would like to hook into poststop to ensure the database is fully backed up before terminating the job fully finalizes. As far as I can tell, litestream by default pushes to S3 every 10 seconds, so when the container (and its sidecar) are stopped, the database may not be fully backed up at that point in time. So in that poststop hook I could use this snapshot command to make sure everything is backed up.

I think this would also help with running it in Kubernetes, as you could use that litestream command in the preStop lifestyle hook.

Additionally, I don't think it needs to be a snapshot in the litestream sense. Simply ensuring all the WAL is replicated would be nice too. I can see that as being better in a sense, as it presumably is much faster than creating a snapshot :). Maybe have an option to specify whether to do WAL replication or a full snapshot?

Cheers,

kevinlang avatar Apr 14 '21 22:04 kevinlang

hey @kevinlang! Thanks for submitting an issue. I like the idea of having a manual snapshot command. I'll have to think about how to add that since there's no direct communication between the CLI & the replication process. Maybe an API via a unix socket?

As for flushing, I added that to a recent PR (https://github.com/benbjohnson/litestream/pull/132) so Litestream should sync to the replica(s) when it receives a signal to close.

benbjohnson avatar Apr 14 '21 22:04 benbjohnson

#132 looks awesome! That alone is sufficient for me for now.

In my specific scenario with Nomad, by the time I hit the poststop hook, the replication process would no longer be running (as the sidecar would've been shut down). So it would be useful for it handle the case where the replication process is currently running, as you say, but also the case where no process is currently running.

kevinlang avatar Apr 14 '21 22:04 kevinlang

I'd like to offer users of my service the ability to request a snapshot on demand. Since I'm running litestream replicate already it would be really convenient if I could do that by telling Litestream "do a snapshot right now".

I understand that the design for this isn't obvious since the Litestream process is constantly running. A few options:

  1. A unix signal - bit weird though!
  2. Litestream can run an HTTP server to expose Prometheus metrics - the addr: ":9090" option in the configuration does that https://litestream.io/reference/config/#metrics - so this server could perhaps have a POST API that can be used to request a snapshot, maybe protected by a shared secret that needs to be sent in an Authorization: xxx header?
  3. As mentioned above, a unix socket mechanism of some sort

Option 2 is my favourite.

simonw avatar Mar 02 '24 18:03 simonw