mirror of
https://github.com/hrfee/jfa-go.git
synced 2024-12-22 00:50:12 +00:00
backups: add -restore cli argument
This commit is contained in:
parent
1bb83c88d9
commit
ff73c72b0e
4
args.go
4
args.go
@ -23,6 +23,7 @@ func (app *appContext) loadArgs(firstCall bool) {
|
|||||||
HOST = flag.String("host", "", "alternate address to host web ui on.")
|
HOST = flag.String("host", "", "alternate address to host web ui on.")
|
||||||
PORT = flag.Int("port", 0, "alternate port to host web ui on.")
|
PORT = flag.Int("port", 0, "alternate port to host web ui on.")
|
||||||
flag.IntVar(PORT, "p", 0, "SHORTHAND")
|
flag.IntVar(PORT, "p", 0, "SHORTHAND")
|
||||||
|
_LOADBAK = flag.String("restore", "", "path to database backup to restore.")
|
||||||
DEBUG = flag.Bool("debug", false, "Enables debug logging.")
|
DEBUG = flag.Bool("debug", false, "Enables debug logging.")
|
||||||
PPROF = flag.Bool("pprof", false, "Exposes pprof profiler on /debug/pprof.")
|
PPROF = flag.Bool("pprof", false, "Exposes pprof profiler on /debug/pprof.")
|
||||||
SWAGGER = flag.Bool("swagger", false, "Enable swagger at /swagger/index.html")
|
SWAGGER = flag.Bool("swagger", false, "Enable swagger at /swagger/index.html")
|
||||||
@ -41,6 +42,9 @@ func (app *appContext) loadArgs(firstCall bool) {
|
|||||||
if *PPROF {
|
if *PPROF {
|
||||||
os.Setenv("PPROF", "1")
|
os.Setenv("PPROF", "1")
|
||||||
}
|
}
|
||||||
|
if *_LOADBAK != "" {
|
||||||
|
LOADBAK = *_LOADBAK
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if os.Getenv("SWAGGER") == "1" {
|
if os.Getenv("SWAGGER") == "1" {
|
||||||
|
27
daemon.go
27
daemon.go
@ -203,6 +203,33 @@ func (app *appContext) makeBackup() (fileDetails CreateBackupDTO) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (app *appContext) loadPendingBackup() {
|
||||||
|
if LOADBAK == "" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
oldPath := filepath.Join(app.dataPath, "db-pre-"+filepath.Base(LOADBAK))
|
||||||
|
app.info.Printf("Moving existing database to \"%s\"\n", oldPath)
|
||||||
|
err := os.Rename(app.storage.db_path, oldPath)
|
||||||
|
if err != nil {
|
||||||
|
app.err.Fatalf("Failed to move existing database: %v\n", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
app.ConnectDB()
|
||||||
|
defer app.storage.db.Close()
|
||||||
|
|
||||||
|
f, err := os.Open(LOADBAK)
|
||||||
|
if err != nil {
|
||||||
|
app.err.Fatalf("Failed to open backup file \"%s\": %v\n", LOADBAK, err)
|
||||||
|
}
|
||||||
|
err = app.storage.db.Badger().Load(f, 256)
|
||||||
|
f.Close()
|
||||||
|
if err != nil {
|
||||||
|
app.err.Fatalf("Failed to restore backup file \"%s\": %v\n", LOADBAK, err)
|
||||||
|
}
|
||||||
|
app.info.Printf("Restored backup \"%s\".", LOADBAK)
|
||||||
|
LOADBAK = ""
|
||||||
|
}
|
||||||
|
|
||||||
func (app *appContext) clearActivities() {
|
func (app *appContext) clearActivities() {
|
||||||
app.debug.Println("Housekeeping: Cleaning up Activity log...")
|
app.debug.Println("Housekeeping: Cleaning up Activity log...")
|
||||||
keepCount := app.config.Section("activity_log").Key("keep_n_records").MustInt(1000)
|
keepCount := app.config.Section("activity_log").Key("keep_n_records").MustInt(1000)
|
||||||
|
4
main.go
4
main.go
@ -56,6 +56,8 @@ var (
|
|||||||
commit string
|
commit string
|
||||||
buildTimeUnix string
|
buildTimeUnix string
|
||||||
builtBy string
|
builtBy string
|
||||||
|
_LOADBAK *string
|
||||||
|
LOADBAK = ""
|
||||||
)
|
)
|
||||||
|
|
||||||
var temp = func() string {
|
var temp = func() string {
|
||||||
@ -355,8 +357,10 @@ func start(asDaemon, firstCall bool) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
app.storage.db_path = filepath.Join(app.dataPath, "db")
|
app.storage.db_path = filepath.Join(app.dataPath, "db")
|
||||||
|
app.loadPendingBackup()
|
||||||
app.ConnectDB()
|
app.ConnectDB()
|
||||||
defer app.storage.db.Close()
|
defer app.storage.db.Close()
|
||||||
|
|
||||||
// Read config-base for settings on web.
|
// Read config-base for settings on web.
|
||||||
app.configBasePath = "config-base.json"
|
app.configBasePath = "config-base.json"
|
||||||
configBase, _ := fs.ReadFile(localFS, app.configBasePath)
|
configBase, _ := fs.ReadFile(localFS, app.configBasePath)
|
||||||
|
Loading…
Reference in New Issue
Block a user