-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: automatic backup/restore of db from s3
- Loading branch information
Runar Kristoffersen
committed
Sep 24, 2022
1 parent
06657a4
commit e9696d1
Showing
12 changed files
with
677 additions
and
27 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package backup | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
) | ||
|
||
func TestCompress(t *testing.T) { | ||
type args struct { | ||
b []byte | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want int | ||
wantErr bool | ||
}{ | ||
// TODO: Add test cases. | ||
{ | ||
"Compress and decress text", | ||
args{ | ||
b: []byte("Foo"), | ||
}, | ||
3, | ||
false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
|
||
w := &bytes.Buffer{} | ||
r := bytes.NewReader(tt.args.b) | ||
got, err := Compress(r, w) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("Compress() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if got != tt.want { | ||
t.Errorf("Compress() = '%v', want '%v'", got, tt.want) | ||
} | ||
rw := &bytes.Buffer{} | ||
rw.Write(w.Bytes()) | ||
ww := &bytes.Buffer{} | ||
n, err := Decompress(rw, ww) | ||
|
||
if n != len(tt.args.b) { | ||
t.Errorf("Decompress() n = '%v', want '%v'", n, len(tt.args.b)) | ||
} | ||
|
||
if ww.String() != string(tt.args.b) { | ||
t.Errorf("Decompress() = '%v', want '%v'", ww.String(), string(tt.args.b)) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package bboltStorage | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/dustin/go-humanize" | ||
bolt "go.etcd.io/bbolt" | ||
) | ||
|
||
// Backups the database to a writer. | ||
// Note that bbolt seems to always change the file (statistics?), | ||
// so creating a hash of the the database does not | ||
func (s *BBolter) Backup(w io.Writer) (int64, error) { | ||
var originalSize int64 = -1 | ||
var compactSize int64 = -1 | ||
s.DB.View(func(tx *bolt.Tx) error { | ||
originalSize = tx.Size() | ||
return nil | ||
}) | ||
s.l.Info().Msg("Creating backup of database, by first compacting the current database to a new temporary database") | ||
compactPath := "__compact-backup.bbolt" | ||
if fileExists(compactPath) { | ||
if err := os.Remove(compactPath); err != nil { | ||
return 0, err | ||
} | ||
} | ||
defer os.Remove(compactPath) | ||
|
||
compactDb, err := s.copyCompact(compactPath) | ||
if compactDb != nil { | ||
defer compactDb.Close() | ||
} | ||
if err != nil { | ||
return 0, err | ||
} | ||
var written int64 | ||
err = compactDb.View((func(tx *bolt.Tx) error { | ||
compactSize = tx.Size() | ||
n, err := tx.WriteTo(w) | ||
written = n | ||
return err | ||
})) | ||
if err != nil { | ||
return written, err | ||
} | ||
if written == 0 { | ||
err = fmt.Errorf("The data written was of zero size") | ||
return written, err | ||
} | ||
s.l.Debug(). | ||
Str("originalSize", humanize.Bytes(uint64(originalSize))). | ||
Str("compactSize", humanize.Bytes(uint64(compactSize))). | ||
Str("percentage", fmt.Sprintf("%2f", float64(compactSize)/float64(originalSize)*100)). | ||
Msg("Compact-size-difference") | ||
return written, err | ||
} | ||
|
||
func fileExists(filePath string) bool { | ||
_, error := os.Stat(filePath) | ||
return !errors.Is(error, os.ErrNotExist) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.