2014-08-04 18:47:04 +00:00
|
|
|
package khepri
|
|
|
|
|
|
|
|
import (
|
2014-08-11 20:47:24 +00:00
|
|
|
"fmt"
|
2014-08-04 18:47:04 +00:00
|
|
|
"os"
|
|
|
|
"os/user"
|
2014-09-23 20:39:12 +00:00
|
|
|
"path/filepath"
|
2014-08-04 18:47:04 +00:00
|
|
|
"time"
|
2014-09-23 20:39:12 +00:00
|
|
|
|
|
|
|
"github.com/fd0/khepri/backend"
|
2014-08-04 18:47:04 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Snapshot struct {
|
2014-09-23 20:39:12 +00:00
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Content backend.ID `json:"content"`
|
|
|
|
StorageMap *StorageMap `json:"map"`
|
|
|
|
Dir string `json:"dir"`
|
|
|
|
Hostname string `json:"hostname,omitempty"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
UID string `json:"uid,omitempty"`
|
|
|
|
GID string `json:"gid,omitempty"`
|
|
|
|
|
|
|
|
id backend.ID // plaintext ID, used during restore
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 20:46:14 +00:00
|
|
|
func NewSnapshot(dir string) *Snapshot {
|
2014-09-23 20:39:12 +00:00
|
|
|
d, err := filepath.Abs(dir)
|
|
|
|
if err != nil {
|
|
|
|
d = dir
|
|
|
|
}
|
|
|
|
|
2014-08-04 18:47:04 +00:00
|
|
|
sn := &Snapshot{
|
2014-09-23 20:39:12 +00:00
|
|
|
Dir: d,
|
2014-08-04 18:47:04 +00: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
|
|
|
|
sn.UID = usr.Uid
|
|
|
|
sn.GID = usr.Gid
|
|
|
|
}
|
|
|
|
|
|
|
|
return sn
|
|
|
|
}
|
|
|
|
|
2014-09-23 20:39:12 +00:00
|
|
|
func LoadSnapshot(ch *ContentHandler, id backend.ID) (*Snapshot, error) {
|
2014-08-04 20:46:14 +00:00
|
|
|
sn := &Snapshot{}
|
2014-09-23 20:39:12 +00:00
|
|
|
err := ch.LoadJSON(backend.Snapshot, id, sn)
|
2014-08-04 20:46:14 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sn, nil
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
2014-08-11 20:47:24 +00:00
|
|
|
|
|
|
|
func (sn *Snapshot) String() string {
|
2014-09-23 20:39:12 +00:00
|
|
|
return fmt.Sprintf("<Snapshot %q at %s>", sn.Dir, sn.Time)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|