Skip to content

Commit

Permalink
Add an automatic backup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff R. Allen committed Jun 20, 2018
1 parent c63658c commit 165068b
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
1 change: 1 addition & 0 deletions evoting/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -860,6 +860,7 @@ func new(context *onet.Context) (onet.Service, error) {
}
}
if write {
db.Backup()
log.LLvlf1(" writing block %x", b.Hash)
db.StoreStompFL(b)
}
Expand Down
30 changes: 30 additions & 0 deletions skipchain/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"encoding/hex"
"errors"
"fmt"
"os"
"strconv"
"time"

Expand Down Expand Up @@ -905,3 +906,32 @@ func (db *SkipBlockDB) GetAllSkipchains() (map[string]*SkipBlock, error) {
}
return gen, nil
}

// Backup creates a "before-evoting-5" backup, if it does not already exist.
func (db *SkipBlockDB) Backup() {
// This is sneaky: it turns out that BoltDB exposes the path name
// via String(). So instead of trying to find the pathname from onet,
// we can just ask BoltDB for it.
path := db.DB.String() // returns something like DB<"/tmp/foo.db">
path = path[4 : len(path)-2]
path += ".before-evoting-5"
if _, err := os.Stat(path); err == nil {
// It already exists: nothing to do.
return
}

log.LLvl1("Backing up DB to", path)
err := db.View(func(tx *bolt.Tx) error {
w, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0400)
if err != nil {
return err
}
defer w.Close()

_, err = tx.WriteTo(w)
return err
})
if err != nil {
log.Error("error while backing up:", err)
}
}

0 comments on commit 165068b

Please sign in to comment.