Skip to content

Commit ae20fe1

Browse files
committed
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.
1 parent f3c448c commit ae20fe1

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

cmd/golink/link-snapshot.json

Lines changed: 0 additions & 1 deletion
This file was deleted.

cmd/golink/main.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,7 @@ import (
88
"github.com/tailscale/golink"
99
)
1010

11-
//go:embed link-snapshot.json
12-
var lastSnapshot []byte
13-
1411
func main() {
15-
golink.LastSnapshot = lastSnapshot
1612
if err := golink.Run(); err != nil {
1713
log.Fatal(err)
1814
}

golink.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"log"
1717
"net/http"
1818
"net/url"
19+
"os"
1920
"path/filepath"
2021
"regexp"
2122
"sort"
@@ -33,6 +34,7 @@ var (
3334
verbose = flag.Bool("verbose", false, "be verbose")
3435
sqlitefile = flag.String("sqlitedb", "", "path of SQLite database to store links")
3536
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")
37+
snapshot = flag.String("snapshot", "", "file path of snapshot file")
3638
)
3739

3840
var stats struct {
@@ -76,6 +78,17 @@ func Run() error {
7678
return fmt.Errorf("NewSQLiteDB(%q): %w", *sqlitefile, err)
7779
}
7880

81+
if *snapshot != "" {
82+
if LastSnapshot != nil {
83+
log.Printf("LastSnapshot already set; ignoring --snapshot")
84+
} else {
85+
var err error
86+
LastSnapshot, err = os.ReadFile(*snapshot)
87+
if err != nil {
88+
log.Fatalf("error reading snapshot file %q: %v", *snapshot, err)
89+
}
90+
}
91+
}
7992
if err := restoreLastSnapshot(); err != nil {
8093
log.Printf("restoring snapshot: %v", err)
8194
}

0 commit comments

Comments
 (0)