Backup/restore settings

This commit is contained in:
Manfred Touron 2017-11-19 00:53:32 +01:00
parent f33326db4d
commit 62aea661cc

View file

@ -225,6 +225,9 @@ GLOBAL OPTIONS:
if err := db.Find(&config.ACLs).Error; err != nil { if err := db.Find(&config.ACLs).Error; err != nil {
return err return err
} }
if err := db.Find(&config.Settings).Error; err != nil {
return err
}
config.Date = time.Now() config.Date = time.Now()
enc := json.NewEncoder(s) enc := json.NewEncoder(s)
if c.Bool("indent") { if c.Bool("indent") {
@ -255,6 +258,7 @@ GLOBAL OPTIONS:
fmt.Fprintf(s, "* %d UserGroups\n", len(config.UserGroups)) fmt.Fprintf(s, "* %d UserGroups\n", len(config.UserGroups))
fmt.Fprintf(s, "* %d Userkeys\n", len(config.UserKeys)) fmt.Fprintf(s, "* %d Userkeys\n", len(config.UserKeys))
fmt.Fprintf(s, "* %d Users\n", len(config.Users)) fmt.Fprintf(s, "* %d Users\n", len(config.Users))
fmt.Fprintf(s, "* %d Settings\n", len(config.Settings))
if !c.Bool("confirm") { if !c.Bool("confirm") {
fmt.Fprintf(s, "restore will erase and replace everything in the database.\nIf you are ok, add the '--confirm' to the restore command\n") fmt.Fprintf(s, "restore will erase and replace everything in the database.\nIf you are ok, add the '--confirm' to the restore command\n")
@ -264,7 +268,7 @@ GLOBAL OPTIONS:
tx := db.Begin() tx := db.Begin()
// FIXME: do everything in a transaction // FIXME: do everything in a transaction
for _, tableName := range []string{"hosts", "users", "acls", "host_groups", "user_groups", "ssh_keys", "user_keys"} { for _, tableName := range []string{"hosts", "users", "acls", "host_groups", "user_groups", "ssh_keys", "user_keys", "settings"} {
if err := tx.Exec(fmt.Sprintf("DELETE FROM %s;", tableName)).Error; err != nil { if err := tx.Exec(fmt.Sprintf("DELETE FROM %s;", tableName)).Error; err != nil {
tx.Rollback() tx.Rollback()
return err return err
@ -312,6 +316,12 @@ GLOBAL OPTIONS:
return err return err
} }
} }
for _, setting := range config.Settings {
if err := tx.Create(&setting).Error; err != nil {
tx.Rollback()
return err
}
}
if err := tx.Commit().Error; err != nil { if err := tx.Commit().Error; err != nil {
return err return err