all: add flag for specifying snapshot file

We still support directly setting LastSnapshot to support embedding the
snapshot in the binary (like we do internally).  But this allows an
alternate way have restoring backups which doesn't require recompiling
binaries.
This commit is contained in:
Will Norris
2022-11-08 12:32:58 -08:00
parent f3c448c4dd
commit ae20fe110a
3 changed files with 13 additions and 5 deletions
-1
View File
@@ -1 +0,0 @@
-4
View File
@@ -8,11 +8,7 @@ import (
"github.com/tailscale/golink"
)
//go:embed link-snapshot.json
var lastSnapshot []byte
func main() {
golink.LastSnapshot = lastSnapshot
if err := golink.Run(); err != nil {
log.Fatal(err)
}
+13
View File
@@ -16,6 +16,7 @@ import (
"log"
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"sort"
@@ -33,6 +34,7 @@ var (
verbose = flag.Bool("verbose", false, "be verbose")
sqlitefile = flag.String("sqlitedb", "", "path of SQLite database to store links")
dev = flag.String("dev-listen", "", "if non-empty, listen on this addr and run in dev mode; auto-set sqlitedb if empty and don't use tsnet")
snapshot = flag.String("snapshot", "", "file path of snapshot file")
)
var stats struct {
@@ -76,6 +78,17 @@ func Run() error {
return fmt.Errorf("NewSQLiteDB(%q): %w", *sqlitefile, err)
}
if *snapshot != "" {
if LastSnapshot != nil {
log.Printf("LastSnapshot already set; ignoring --snapshot")
} else {
var err error
LastSnapshot, err = os.ReadFile(*snapshot)
if err != nil {
log.Fatalf("error reading snapshot file %q: %v", *snapshot, err)
}
}
}
if err := restoreLastSnapshot(); err != nil {
log.Printf("restoring snapshot: %v", err)
}