@@ -273,7 +273,11 @@ func (r *Restore) Snapshot(
273
273
return err
274
274
}
275
275
276
- err = r .restoreIndexes (ctx , oplogOption .nss , cloneNS )
276
+ if cloneNS .IsSpecified () {
277
+ err = r .restoreIndexes (ctx , []string {cloneNS .ToNS })
278
+ } else {
279
+ err = r .restoreIndexes (ctx , oplogOption .nss )
280
+ }
277
281
if err != nil {
278
282
return errors .Wrap (err , "restore indexes" )
279
283
}
@@ -439,7 +443,11 @@ func (r *Restore) PITR(
439
443
return err
440
444
}
441
445
442
- err = r .restoreIndexes (ctx , oplogOption .nss , cloneNS )
446
+ if cloneNS .IsSpecified () {
447
+ err = r .restoreIndexes (ctx , []string {cloneNS .ToNS })
448
+ } else {
449
+ err = r .restoreIndexes (ctx , oplogOption .nss )
450
+ }
443
451
if err != nil {
444
452
return errors .Wrap (err , "restore indexes" )
445
453
}
@@ -863,7 +871,7 @@ func (r *Restore) RunSnapshot(
863
871
return nil , err
864
872
}
865
873
866
- err = r .loadIndexesFrom (bytes .NewReader (data ))
874
+ err = r .loadIndexesFrom (bytes .NewReader (data ), cloneNS )
867
875
if err != nil {
868
876
return nil , errors .Wrap (err , "load indexes" )
869
877
}
@@ -1007,12 +1015,15 @@ func (r *Restore) restoreUsersAndRoles(ctx context.Context, nss []string) error
1007
1015
return nil
1008
1016
}
1009
1017
1010
- func (r * Restore ) loadIndexesFrom (rdr io.Reader ) error {
1018
+ func (r * Restore ) loadIndexesFrom (rdr io.Reader , cloneNS snapshot. CloneNS ) error {
1011
1019
meta , err := archive .ReadMetadata (rdr )
1012
1020
if err != nil {
1013
1021
return errors .Wrap (err , "read metadata" )
1014
1022
}
1015
1023
1024
+ fromDB , fromColl := cloneNS .SplitFromNS ()
1025
+ toDB , toColl := cloneNS .SplitToNS ()
1026
+
1016
1027
for _ , ns := range meta .Namespaces {
1017
1028
var md mongorestore.Metadata
1018
1029
err := bson .UnmarshalExtJSON ([]byte (ns .Metadata ), true , & md )
@@ -1021,7 +1032,11 @@ func (r *Restore) loadIndexesFrom(rdr io.Reader) error {
1021
1032
ns .Database , ns .Collection )
1022
1033
}
1023
1034
1024
- r .indexCatalog .AddIndexes (ns .Database , ns .Collection , md .Indexes )
1035
+ if cloneNS .IsSpecified () && ns .Database == fromDB && ns .Collection == fromColl {
1036
+ r .indexCatalog .AddIndexes (toDB , toColl , md .Indexes )
1037
+ } else {
1038
+ r .indexCatalog .AddIndexes (ns .Database , ns .Collection , md .Indexes )
1039
+ }
1025
1040
1026
1041
simple := true
1027
1042
if md .Options != nil {
@@ -1048,7 +1063,7 @@ func (r *Restore) loadIndexesFrom(rdr io.Reader) error {
1048
1063
return nil
1049
1064
}
1050
1065
1051
- func (r * Restore ) restoreIndexes (ctx context.Context , nss []string , cloneNS snapshot. CloneNS ) error {
1066
+ func (r * Restore ) restoreIndexes (ctx context.Context , nss []string ) error {
1052
1067
r .log .Debug ("building indexes up" )
1053
1068
1054
1069
isSelected := util .MakeSelectedPred (nss )
@@ -1073,32 +1088,24 @@ func (r *Restore) restoreIndexes(ctx context.Context, nss []string, cloneNS snap
1073
1088
}
1074
1089
1075
1090
var indexNames []string
1076
- var targetDB , targetColl string
1077
1091
for _ , index := range indexes {
1078
- if cloneNS .IsSpecified () && ns .String () == cloneNS .FromNS {
1079
- // override index's ns for the collection cloning
1080
- targetDB , targetColl = util .ParseNS (cloneNS .ToNS )
1081
- index .Options ["ns" ] = cloneNS .ToNS
1082
- } else {
1083
- targetDB , targetColl = ns .DB , ns .Collection
1084
- index .Options ["ns" ] = ns .DB + "." + ns .Collection
1085
- }
1092
+ index .Options ["ns" ] = ns .DB + "." + ns .Collection
1086
1093
indexNames = append (indexNames , index .Options ["name" ].(string ))
1087
1094
// remove the index version, forcing an update
1088
1095
delete (index .Options , "v" )
1089
1096
}
1090
1097
1091
1098
rawCommand := bson.D {
1092
- {"createIndexes" , targetColl },
1099
+ {"createIndexes" , ns . Collection },
1093
1100
{"indexes" , indexes },
1094
1101
{"ignoreUnknownIndexOptions" , true },
1095
1102
}
1096
1103
1097
1104
r .log .Info ("restoring indexes for %s.%s: %s" ,
1098
- targetDB , targetColl , strings .Join (indexNames , ", " ))
1099
- err := r .nodeConn .Database (targetDB ).RunCommand (ctx , rawCommand ).Err ()
1105
+ ns . DB , ns . Collection , strings .Join (indexNames , ", " ))
1106
+ err := r .nodeConn .Database (ns . DB ).RunCommand (ctx , rawCommand ).Err ()
1100
1107
if err != nil {
1101
- return errors .Wrapf (err , "createIndexes for %s.%s" , targetDB , targetColl )
1108
+ return errors .Wrapf (err , "createIndexes for %s.%s" , ns . DB , ns . Collection )
1102
1109
}
1103
1110
}
1104
1111
0 commit comments