Skip to content

Commit

Permalink
More details in backup errors
Browse files Browse the repository at this point in the history
  • Loading branch information
AlinaNova21 committed Nov 27, 2019
1 parent 5ad985d commit 37e45f4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 21 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ require (
github.com/mattn/go-tty v0.0.0-20190424173100-523744f04859 // indirect
github.com/otiai10/copy v1.0.1
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 // indirect
github.com/pkg/errors v0.8.1
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 // indirect
github.com/stretchr/testify v1.3.0 // indirect
github.com/tcnksm/go-latest v0.0.0-20170313132115-e3007ae9052e
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95 h1:+OLn68pqasWca0z5ry
github.com/otiai10/curr v0.0.0-20150429015615-9b4961190c95/go.mod h1:9qAhocn7zKJG+0mI8eUu6xqkFDYS2kb2saOteoSB3cE=
github.com/otiai10/mint v1.2.3 h1:PsrRBmrxR68kyNu6YlqYHbNlItc5vOkuS6LBEsNttVA=
github.com/otiai10/mint v1.2.3/go.mod h1:YnfyPNhBvnY8bW4SGQHCs/aAFhkgySlMZbrF5U0bOVw=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942 h1:A7GG7zcGjl3jqAqGPmcNjd/D9hzL95SuoOQAaFNdLU0=
github.com/pkg/term v0.0.0-20190109203006-aa71e9d9e942/go.mod h1:eCbImbZ95eXtAUIbLAuAVnBnwf83mjf6QIVH8SHYwqQ=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
Expand Down
17 changes: 11 additions & 6 deletions recovery/files.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package recovery

import "io/ioutil"
import (
"io/ioutil"
"os"
"path/filepath"

import "path/filepath"

import "os"
"github.com/pkg/errors"
)

type fileData map[string][]byte

Expand Down Expand Up @@ -43,10 +45,13 @@ func (r *Recovery) filesRestore(data fileData) error {
dir := filepath.Dir(file)
if dir != "" {
if err := os.MkdirAll(dir, 0755); err != nil {
return err
return errors.Wrapf(err, "failed to mkdir %s", dir)
}
}
ioutil.WriteFile(file, bytes, 0644)
err := ioutil.WriteFile(file, bytes, 0644)
if err != nil {
return errors.Wrapf(err, "failed to write file %s", file)
}
}
return nil
}
19 changes: 8 additions & 11 deletions recovery/mongo.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package recovery
import (
"context"
"fmt"
"log"
"time"

"github.com/pkg/errors"
"go.mongodb.org/mongo-driver/bson"
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
Expand All @@ -28,7 +28,7 @@ func (r *Recovery) getMongoClient(ctx context.Context) (*mongo.Client, *mongo.Da
}
client, err := mongo.Connect(ctx, options.Client().ApplyURI(fmt.Sprintf("mongodb://%s:%s", host, port)))
if err != nil {
return nil, nil, err
return nil, nil, errors.Wrap(err, "failed to connect")
}
db := client.Database(database)
return client, db, nil
Expand All @@ -39,18 +39,18 @@ func (r *Recovery) mongoBackup() (mongoData, error) {
defer cancel()
client, db, err := r.getMongoClient(ctx)
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get client")
}
defer client.Disconnect(ctx)
data := mongoData{}
cols, err := db.ListCollectionNames(ctx, bson.M{})
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to list collections")
}
for _, name := range cols {
cur, err := db.Collection(name).Find(ctx, bson.M{})
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to get records from collection %s", name)
}
arr := make([]bson.Raw, 0)
defer cur.Close(ctx)
Expand All @@ -69,14 +69,12 @@ func (r *Recovery) mongoRestore(data mongoData) error {
defer cancel()
client, db, err := r.getMongoClient(ctx)
if err != nil {
log.Print("client")
return err
return errors.Wrap(err, "failed to get client")
}
defer client.Disconnect(ctx)
err = db.Drop(ctx)
if err != nil {
log.Print("drop")
return err
return errors.Wrap(err, "failed to drop db")
}
for name, docs := range data {
data := make([]interface{}, len(docs))
Expand All @@ -85,8 +83,7 @@ func (r *Recovery) mongoRestore(data mongoData) error {
}
_, err := db.Collection(name).InsertMany(ctx, data)
if err != nil {
log.Print("insert", data, docs, name)
return err
return errors.Wrapf(err, "failed to insert into collection %s", name)
}
}
return nil
Expand Down
9 changes: 5 additions & 4 deletions recovery/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"time"

"github.com/go-redis/redis/v7"
"github.com/pkg/errors"
)

type redisData map[string]redisValue
Expand Down Expand Up @@ -35,13 +36,13 @@ func (r *Recovery) redisBackup() (redisData, error) {
client := r.getRedisClient()
keys, err := client.Keys("*").Result()
if err != nil {
return nil, err
return nil, errors.Wrap(err, "failed to get keys")
}
col := redisData{}
for _, key := range keys {
value, err := client.Dump(key).Result()
if err != nil {
return nil, err
return nil, errors.Wrapf(err, "failed to dump key %s", key)
}
ttl := client.TTL(key).Val()
col[key] = redisValue{
Expand All @@ -55,11 +56,11 @@ func (r *Recovery) redisBackup() (redisData, error) {
func (r *Recovery) redisRestore(data redisData) error {
client := r.getRedisClient()
if err := client.FlushAll().Err(); err != nil {
return err
return errors.Wrap(err, "failed to flush")
}
for k, v := range data {
if err := client.RestoreReplace(k, v.TTL, v.Value).Err(); err != nil {
return err
return errors.Wrapf(err, "failed to restore key %s", k)
}
}
return nil
Expand Down

0 comments on commit 37e45f4

Please sign in to comment.