2014-12-05 15:45:49 -05:00
|
|
|
package restic
|
2014-08-04 14:47:04 -04:00
|
|
|
|
|
|
|
|
import (
|
2014-08-11 16:47:24 -04:00
|
|
|
"fmt"
|
2014-08-04 14:47:04 -04:00
|
|
|
"os"
|
|
|
|
|
"os/user"
|
2014-09-23 16:39:12 -04:00
|
|
|
"path/filepath"
|
2014-12-21 11:20:49 -05:00
|
|
|
"strconv"
|
2014-08-04 14:47:04 -04:00
|
|
|
"time"
|
2014-09-23 16:39:12 -04:00
|
|
|
|
2014-12-05 15:45:49 -05:00
|
|
|
"github.com/restic/restic/backend"
|
2014-08-04 14:47:04 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Snapshot struct {
|
2014-11-21 15:21:44 -05:00
|
|
|
Time time.Time `json:"time"`
|
2014-11-30 16:34:21 -05:00
|
|
|
Parent backend.ID `json:"parent,omitempty"`
|
2014-12-07 08:14:07 -05:00
|
|
|
Tree backend.ID `json:"tree"`
|
2014-11-23 16:26:01 -05:00
|
|
|
Map backend.ID `json:"map"`
|
2014-11-21 15:21:44 -05:00
|
|
|
Dir string `json:"dir"`
|
|
|
|
|
Hostname string `json:"hostname,omitempty"`
|
|
|
|
|
Username string `json:"username,omitempty"`
|
2014-12-21 11:20:49 -05:00
|
|
|
UID uint32 `json:"uid,omitempty"`
|
|
|
|
|
GID uint32 `json:"gid,omitempty"`
|
2014-09-23 16:39:12 -04:00
|
|
|
|
|
|
|
|
id backend.ID // plaintext ID, used during restore
|
2014-11-23 16:26:01 -05:00
|
|
|
bl *BlobList
|
2014-08-04 14:47:04 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-21 11:20:49 -05:00
|
|
|
func NewSnapshot(dir string) (*Snapshot, error) {
|
2014-09-23 16:39:12 -04:00
|
|
|
d, err := filepath.Abs(dir)
|
|
|
|
|
if err != nil {
|
|
|
|
|
d = dir
|
|
|
|
|
}
|
|
|
|
|
|
2014-08-04 14:47:04 -04:00
|
|
|
sn := &Snapshot{
|
2014-09-23 16:39:12 -04:00
|
|
|
Dir: d,
|
2014-08-04 14:47:04 -04:00
|
|
|
Time: time.Now(),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
hn, err := os.Hostname()
|
|
|
|
|
if err == nil {
|
|
|
|
|
sn.Hostname = hn
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
usr, err := user.Current()
|
|
|
|
|
if err == nil {
|
|
|
|
|
sn.Username = usr.Username
|
2014-12-21 11:20:49 -05:00
|
|
|
uid, err := strconv.ParseInt(usr.Uid, 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
sn.UID = uint32(uid)
|
|
|
|
|
|
|
|
|
|
gid, err := strconv.ParseInt(usr.Gid, 10, 32)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
sn.GID = uint32(gid)
|
2014-08-04 14:47:04 -04:00
|
|
|
}
|
|
|
|
|
|
2014-12-21 11:20:49 -05:00
|
|
|
return sn, nil
|
2014-08-04 14:47:04 -04:00
|
|
|
}
|
|
|
|
|
|
2014-09-23 16:39:12 -04:00
|
|
|
func LoadSnapshot(ch *ContentHandler, id backend.ID) (*Snapshot, error) {
|
2014-11-24 15:12:32 -05:00
|
|
|
sn := &Snapshot{id: id}
|
2014-09-23 16:39:12 -04:00
|
|
|
err := ch.LoadJSON(backend.Snapshot, id, sn)
|
2014-08-04 16:46:14 -04:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return sn, nil
|
2014-08-04 14:47:04 -04:00
|
|
|
}
|
2014-08-11 16:47:24 -04:00
|
|
|
|
2014-11-24 15:12:32 -05:00
|
|
|
func (sn Snapshot) String() string {
|
2014-09-23 16:39:12 -04:00
|
|
|
return fmt.Sprintf("<Snapshot %q at %s>", sn.Dir, sn.Time)
|
2014-08-11 16:47:24 -04:00
|
|
|
}
|
2014-11-24 15:12:32 -05:00
|
|
|
|
|
|
|
|
func (sn Snapshot) ID() backend.ID {
|
|
|
|
|
return sn.id
|
|
|
|
|
}
|