2016-08-31 22:39:36 +02:00
|
|
|
package archiver
|
2016-05-08 21:50:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2016-08-31 22:39:36 +02:00
|
|
|
"restic"
|
2016-05-08 21:50:27 +02:00
|
|
|
"restic/debug"
|
|
|
|
"time"
|
|
|
|
|
2016-08-29 19:18:57 +02:00
|
|
|
"github.com/pkg/errors"
|
2016-05-08 21:50:27 +02:00
|
|
|
"github.com/restic/chunker"
|
|
|
|
)
|
|
|
|
|
|
|
|
// saveTreeJSON stores a tree in the repository.
|
2016-08-31 22:39:36 +02:00
|
|
|
func saveTreeJSON(repo restic.Repository, item interface{}) (restic.ID, error) {
|
2016-05-08 21:50:27 +02:00
|
|
|
data, err := json.Marshal(item)
|
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return restic.ID{}, errors.Wrap(err, "")
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
data = append(data, '\n')
|
|
|
|
|
|
|
|
// check if tree has been saved before
|
2016-08-31 22:39:36 +02:00
|
|
|
id := restic.Hash(data)
|
|
|
|
if repo.Index().Has(id, restic.TreeBlob) {
|
2016-05-08 21:50:27 +02:00
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
return repo.SaveJSON(restic.TreeBlob, item)
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ArchiveReader reads from the reader and archives the data. Returned is the
|
|
|
|
// resulting snapshot and its ID.
|
2016-08-31 22:39:36 +02:00
|
|
|
func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, name string) (*restic.Snapshot, restic.ID, error) {
|
2016-05-08 21:50:27 +02:00
|
|
|
debug.Log("ArchiveReader", "start archiving %s", name)
|
2016-08-31 22:39:36 +02:00
|
|
|
sn, err := restic.NewSnapshot([]string{name})
|
2016-05-08 21:50:27 +02:00
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, err
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
p.Start()
|
|
|
|
defer p.Done()
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
chnker := chunker.New(rd, repo.Config().ChunkerPolynomial)
|
2016-05-08 21:50:27 +02:00
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
var ids restic.IDs
|
2016-05-08 22:21:16 +02:00
|
|
|
var fileSize uint64
|
2016-05-08 21:50:27 +02:00
|
|
|
|
|
|
|
for {
|
|
|
|
chunk, err := chnker.Next(getBuf())
|
2016-08-29 19:18:57 +02:00
|
|
|
if errors.Cause(err) == io.EOF {
|
2016-05-08 21:50:27 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, errors.Wrap(err, "chunker.Next()")
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
id := restic.Hash(chunk.Data)
|
2016-05-08 21:50:27 +02:00
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
if !repo.Index().Has(id, restic.DataBlob) {
|
|
|
|
_, err := repo.SaveAndEncrypt(restic.DataBlob, chunk.Data, nil)
|
2016-05-08 21:50:27 +02:00
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, err
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
debug.Log("ArchiveReader", "saved blob %v (%d bytes)\n", id.Str(), chunk.Length)
|
|
|
|
} else {
|
|
|
|
debug.Log("ArchiveReader", "blob %v already saved in the repo\n", id.Str())
|
|
|
|
}
|
|
|
|
|
|
|
|
freeBuf(chunk.Data)
|
|
|
|
|
|
|
|
ids = append(ids, id)
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
p.Report(restic.Stat{Bytes: uint64(chunk.Length)})
|
2016-05-08 22:21:16 +02:00
|
|
|
fileSize += uint64(chunk.Length)
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
tree := &restic.Tree{
|
|
|
|
Nodes: []*restic.Node{
|
|
|
|
&restic.Node{
|
2016-05-08 21:50:27 +02:00
|
|
|
Name: name,
|
|
|
|
AccessTime: time.Now(),
|
|
|
|
ModTime: time.Now(),
|
2016-09-01 21:20:03 +02:00
|
|
|
Type: "file",
|
2016-05-08 21:50:27 +02:00
|
|
|
Mode: 0644,
|
2016-05-08 22:21:16 +02:00
|
|
|
Size: fileSize,
|
2016-05-08 21:50:27 +02:00
|
|
|
UID: sn.UID,
|
|
|
|
GID: sn.GID,
|
|
|
|
User: sn.Username,
|
|
|
|
Content: ids,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
treeID, err := saveTreeJSON(repo, tree)
|
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, err
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
sn.Tree = &treeID
|
|
|
|
debug.Log("ArchiveReader", "tree saved as %v", treeID.Str())
|
|
|
|
|
2016-08-31 22:39:36 +02:00
|
|
|
id, err := repo.SaveJSONUnpacked(restic.SnapshotFile, sn)
|
2016-05-08 21:50:27 +02:00
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, err
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
debug.Log("ArchiveReader", "snapshot saved as %v", id.Str())
|
|
|
|
|
|
|
|
err = repo.Flush()
|
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, err
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
err = repo.SaveIndex()
|
|
|
|
if err != nil {
|
2016-08-31 22:39:36 +02:00
|
|
|
return nil, restic.ID{}, err
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return sn, id, nil
|
|
|
|
}
|