2016-08-31 22:39:36 +02:00
|
|
|
package archiver
|
2016-05-08 21:50:27 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io"
|
2016-08-31 22:39:36 +02:00
|
|
|
"restic"
|
2016-05-08 21:50:27 +02:00
|
|
|
"restic/debug"
|
|
|
|
"time"
|
|
|
|
|
2016-09-01 22:17:37 +02:00
|
|
|
"restic/errors"
|
2016-09-03 20:11:10 +02:00
|
|
|
|
|
|
|
"github.com/restic/chunker"
|
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-09-13 20:12:55 +02:00
|
|
|
func ArchiveReader(repo restic.Repository, p *restic.Progress, rd io.Reader, name string, tags []string) (*restic.Snapshot, restic.ID, error) {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("start archiving %s", name)
|
2016-09-13 20:12:55 +02:00
|
|
|
sn, err := restic.NewSnapshot([]string{name}, tags)
|
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
|
|
|
|
2017-02-04 16:38:33 +01:00
|
|
|
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) {
|
2016-09-03 20:11:10 +02:00
|
|
|
_, err := repo.SaveBlob(restic.DataBlob, chunk.Data, id)
|
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
|
|
|
}
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("saved blob %v (%d bytes)\n", id.Str(), chunk.Length)
|
2016-05-08 21:50:27 +02:00
|
|
|
} else {
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("blob %v already saved in the repo\n", id.Str())
|
2016-05-08 21:50:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2016-09-03 20:55:22 +02:00
|
|
|
treeID, err := repo.SaveTree(tree)
|
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
|
|
|
}
|
|
|
|
sn.Tree = &treeID
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("tree saved as %v", treeID.Str())
|
2016-05-08 21:50:27 +02:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-09-27 22:35:08 +02:00
|
|
|
debug.Log("snapshot saved as %v", id.Str())
|
2016-05-08 21:50:27 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|