package logic import ( "context" "fmt" "time" "mbase/pkg/descr" "mbase/pkg/mbctl" "go.yaml.in/yaml/v4" ) func (lg *Logic) GetDump(ctx context.Context, accountID string, params *mbctl.GetDumpParams) (*mbctl.GetDumpResult, error) { var err error res := &mbctl.GetDumpResult{} grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase) if err != nil { return res, err } if !grantExists { err := fmt.Errorf("Operation not allowed for the user") return res, err } lg.WaitRestoring() lg.WaitDumping() lg.DumpingSemUp() defer lg.DumpingSemDown() listAccounts, err := lg.db.CompletedListAccounts(ctx) if err != nil { return res, err } listGrants, err := lg.db.ListGrants(ctx) if err != nil { return res, err } lg.DumpingSemDown() dump := descr.Dump{ Timestamp: time.Now().Format(time.RFC3339), Accounts: listAccounts, Grants: listGrants, } dumpBytes, err := yaml.Marshal(dump) if err != nil { return res, err } res.Dump = string(dumpBytes) return res, err } func (lg *Logic) RestoreDump(ctx context.Context, accountID string, params *mbctl.RestoreDumpParams) (*mbctl.RestoreDumpResult, error) { var err error res := &mbctl.RestoreDumpResult{} grantExists, _, err := lg.db.GetGrant(ctx, accountID, descr.GrantModifyDatabase) if err != nil { return res, err } if !grantExists { err := fmt.Errorf("Operation not allowed for the user") return res, err } lg.WaitDumping() lg.WaitRestoring() var dump descr.Dump err = yaml.Unmarshal([]byte(params.Dump), &dump) if err != nil { return res, err } lg.RestoringSemUp() defer lg.RestoringSemDown() if params.DeleteAllRecords { err = lg.db.CleanDatabase(ctx) if err != nil { return res, err } } for _, account := range dump.Accounts { lg.log.Infof("Insert account %s", account.Username) err = lg.db.InsertAccount(ctx, &account) if err != nil { lg.log.Errorf("Insert account error: %v", err) } } for _, grant := range dump.Grants { lg.log.Infof("Insert grant %s for account %d", grant.Operation, grant.AccountID) err = lg.db.InsertGrant(ctx, &grant) if err != nil { lg.log.Errorf("Insert account error: %v", err) } } return res, err }