Skip to content

Commit 3b86e06

Browse files
committed
allow restore from old format, fix #1238
Signed-off-by: Slach <[email protected]>
1 parent d4793b4 commit 3b86e06

File tree

2 files changed

+25
-7
lines changed

2 files changed

+25
-7
lines changed

pkg/backup/restore.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -573,8 +573,14 @@ func (b *Backuper) restoreRBACResolveAllConflicts(ctx context.Context, backupNam
573573
data := keeper.DumpNode{}
574574
jsonErr := json.Unmarshal([]byte(line), &data)
575575
if jsonErr != nil {
576-
log.Error().Msgf("can't %s json.Unmarshal error: %v line: %s", fPath, line, jsonErr)
577-
continue
576+
//convert from old format
577+
dataString := keeper.DumpNodeString{}
578+
if jsonErr = json.Unmarshal([]byte(line), &dataString); jsonErr != nil {
579+
log.Error().Msgf("can't %s json.Unmarshal error: %v line: %s", fPath, line, jsonErr)
580+
continue
581+
}
582+
data.Path = dataString.Path
583+
data.Value = []byte(dataString.Value)
578584
}
579585
if strings.HasPrefix(data.Path, "uuid/") {
580586
if resolveErr := b.resolveRBACConflictIfExist(ctx, string(data.Value), accessPath, version, k, replicatedUserDirectories, dropExists); resolveErr != nil {

pkg/keeper/keeper.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ import (
55
"context"
66
"encoding/json"
77
"fmt"
8-
"github.com/antchfx/xmlquery"
9-
"github.com/rs/zerolog"
10-
"github.com/rs/zerolog/log"
118
"os"
129
"path"
1310
"strconv"
1411
"strings"
1512
"time"
1613

14+
"github.com/antchfx/xmlquery"
15+
"github.com/rs/zerolog"
16+
"github.com/rs/zerolog/log"
17+
1718
"github.com/Altinity/clickhouse-backup/v2/pkg/clickhouse"
1819
"github.com/go-zookeeper/zk"
1920
)
@@ -42,6 +43,12 @@ type DumpNode struct {
4243
Value []byte `json:"value"` // json encodes/decodes as base64 automatically
4344
}
4445

46+
// old format used during restore
47+
type DumpNodeString struct {
48+
Path string `json:"path"`
49+
Value string `json:"value"`
50+
}
51+
4552
type Keeper struct {
4653
conn *zk.Conn
4754
root string
@@ -197,8 +204,13 @@ func (k *Keeper) Restore(dumpFile, prefix string) error {
197204
scanner := bufio.NewScanner(f)
198205
for scanner.Scan() {
199206
node := DumpNode{}
200-
if err = json.Unmarshal(scanner.Bytes(), &node); err != nil {
201-
return err
207+
binaryData := scanner.Bytes()
208+
if err = json.Unmarshal(binaryData, &node); err != nil {
209+
//convert from old format
210+
nodeString := DumpNodeString{}
211+
if stringUnmarshalErr := json.Unmarshal(binaryData, &nodeString); stringUnmarshalErr != nil {
212+
return fmt.Errorf("k.Restore can't read data binaryErr=%v, stringErr=%v", err, stringUnmarshalErr)
213+
}
202214
}
203215
node.Path = path.Join(prefix, node.Path)
204216
version := int32(0)

0 commit comments

Comments
 (0)