2014-12-05 20:45:49 +00:00
|
|
|
package restic
|
2014-08-04 18:47:04 +00:00
|
|
|
|
|
|
|
import (
|
2017-06-04 09:16:55 +00:00
|
|
|
"context"
|
2014-08-11 20:47:24 +00:00
|
|
|
"fmt"
|
2015-04-29 21:35:02 +00:00
|
|
|
"os/user"
|
2014-09-23 20:39:12 +00:00
|
|
|
"path/filepath"
|
2017-07-09 07:47:41 +00:00
|
|
|
"restic/debug"
|
2014-08-04 18:47:04 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2016-05-08 20:38:38 +00:00
|
|
|
// Snapshot is the state of a resource at one point in time.
|
2014-08-04 18:47:04 +00:00
|
|
|
type Snapshot struct {
|
2016-08-31 18:29:54 +00:00
|
|
|
Time time.Time `json:"time"`
|
|
|
|
Parent *ID `json:"parent,omitempty"`
|
|
|
|
Tree *ID `json:"tree"`
|
|
|
|
Paths []string `json:"paths"`
|
|
|
|
Hostname string `json:"hostname,omitempty"`
|
|
|
|
Username string `json:"username,omitempty"`
|
|
|
|
UID uint32 `json:"uid,omitempty"`
|
|
|
|
GID uint32 `json:"gid,omitempty"`
|
|
|
|
Excludes []string `json:"excludes,omitempty"`
|
2016-09-13 18:12:55 +00:00
|
|
|
Tags []string `json:"tags,omitempty"`
|
2017-03-05 16:51:57 +00:00
|
|
|
Original *ID `json:"original,omitempty"`
|
2016-08-31 18:29:54 +00:00
|
|
|
|
|
|
|
id *ID // plaintext ID, used during restore
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 20:38:38 +00:00
|
|
|
// NewSnapshot returns an initialized snapshot struct for the current user and
|
|
|
|
// time.
|
2017-02-10 18:37:33 +00:00
|
|
|
func NewSnapshot(paths []string, tags []string, hostname string) (*Snapshot, error) {
|
2015-03-02 13:48:47 +00:00
|
|
|
for i, path := range paths {
|
|
|
|
if p, err := filepath.Abs(path); err != nil {
|
|
|
|
paths[i] = p
|
|
|
|
}
|
2014-09-23 20:39:12 +00:00
|
|
|
}
|
|
|
|
|
2014-08-04 18:47:04 +00:00
|
|
|
sn := &Snapshot{
|
2017-02-10 18:37:33 +00:00
|
|
|
Paths: paths,
|
|
|
|
Time: time.Now(),
|
|
|
|
Tags: tags,
|
|
|
|
Hostname: hostname,
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2017-02-10 18:37:33 +00:00
|
|
|
err := sn.fillUserInfo()
|
2015-02-03 21:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2014-12-21 16:20:49 +00:00
|
|
|
return sn, nil
|
2014-08-04 18:47:04 +00:00
|
|
|
}
|
|
|
|
|
2016-05-08 20:38:38 +00:00
|
|
|
// LoadSnapshot loads the snapshot with the id and returns it.
|
2017-06-04 09:16:55 +00:00
|
|
|
func LoadSnapshot(ctx context.Context, repo Repository, id ID) (*Snapshot, error) {
|
2015-07-25 15:05:45 +00:00
|
|
|
sn := &Snapshot{id: &id}
|
2017-06-04 09:16:55 +00:00
|
|
|
err := repo.LoadJSONUnpacked(ctx, SnapshotFile, 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
|
|
|
|
2016-05-08 20:38:38 +00:00
|
|
|
// LoadAllSnapshots returns a list of all snapshots in the repo.
|
2017-06-04 09:16:55 +00:00
|
|
|
func LoadAllSnapshots(ctx context.Context, repo Repository) (snapshots []*Snapshot, err error) {
|
|
|
|
for id := range repo.List(ctx, SnapshotFile) {
|
|
|
|
sn, err := LoadSnapshot(ctx, repo, id)
|
2016-04-10 14:51:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
snapshots = append(snapshots, sn)
|
|
|
|
}
|
2017-03-05 16:43:18 +00:00
|
|
|
return
|
2016-04-10 14:51:46 +00:00
|
|
|
}
|
|
|
|
|
2014-11-24 20:12:32 +00:00
|
|
|
func (sn Snapshot) String() string {
|
2016-08-19 18:50:52 +00:00
|
|
|
return fmt.Sprintf("<Snapshot %s of %v at %s by %s@%s>",
|
|
|
|
sn.id.Str(), sn.Paths, sn.Time, sn.Username, sn.Hostname)
|
2014-08-11 20:47:24 +00:00
|
|
|
}
|
2014-11-24 20:12:32 +00:00
|
|
|
|
2017-02-08 23:43:10 +00:00
|
|
|
// ID returns the snapshot's ID.
|
2016-08-31 18:29:54 +00:00
|
|
|
func (sn Snapshot) ID() *ID {
|
2014-11-24 20:12:32 +00:00
|
|
|
return sn.id
|
|
|
|
}
|
2015-04-29 21:35:02 +00:00
|
|
|
|
|
|
|
func (sn *Snapshot) fillUserInfo() error {
|
|
|
|
usr, err := user.Current()
|
|
|
|
if err != nil {
|
2015-05-14 21:13:00 +00:00
|
|
|
return nil
|
2015-04-29 21:35:02 +00:00
|
|
|
}
|
|
|
|
sn.Username = usr.Username
|
2015-05-14 21:13:00 +00:00
|
|
|
|
2015-08-16 11:16:02 +00:00
|
|
|
// set userid and groupid
|
|
|
|
sn.UID, sn.GID, err = uidGidInt(*usr)
|
|
|
|
return err
|
2015-04-29 21:35:02 +00:00
|
|
|
}
|
2015-05-17 18:48:59 +00:00
|
|
|
|
2017-03-05 16:43:18 +00:00
|
|
|
// AddTags adds the given tags to the snapshots tags, preventing duplicates.
|
|
|
|
// It returns true if any changes were made.
|
|
|
|
func (sn *Snapshot) AddTags(addTags []string) (changed bool) {
|
|
|
|
nextTag:
|
|
|
|
for _, add := range addTags {
|
|
|
|
for _, tag := range sn.Tags {
|
|
|
|
if tag == add {
|
|
|
|
continue nextTag
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sn.Tags = append(sn.Tags, add)
|
|
|
|
changed = true
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveTags removes the given tags from the snapshots tags and
|
|
|
|
// returns true if any changes were made.
|
|
|
|
func (sn *Snapshot) RemoveTags(removeTags []string) (changed bool) {
|
|
|
|
for _, remove := range removeTags {
|
|
|
|
for i, tag := range sn.Tags {
|
|
|
|
if tag == remove {
|
|
|
|
// https://github.com/golang/go/wiki/SliceTricks
|
|
|
|
sn.Tags[i] = sn.Tags[len(sn.Tags)-1]
|
|
|
|
sn.Tags[len(sn.Tags)-1] = ""
|
|
|
|
sn.Tags = sn.Tags[:len(sn.Tags)-1]
|
|
|
|
|
|
|
|
changed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-07-07 01:19:06 +00:00
|
|
|
func (sn *Snapshot) hasTag(tag string) bool {
|
|
|
|
for _, snTag := range sn.Tags {
|
|
|
|
if tag == snTag {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:24:02 +00:00
|
|
|
// HasTags returns true if the snapshot has all the tags in l.
|
|
|
|
func (sn *Snapshot) HasTags(l []string) bool {
|
|
|
|
for _, tag := range l {
|
|
|
|
if !sn.hasTag(tag) {
|
|
|
|
return false
|
2016-09-13 18:13:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:24:02 +00:00
|
|
|
return true
|
2016-09-13 18:13:04 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 07:47:41 +00:00
|
|
|
// HasTagList returns true if the snapshot satisfies at least one TagList,
|
|
|
|
// so there is a TagList in l for which all tags are included in sn.
|
|
|
|
func (sn *Snapshot) HasTagList(l []TagList) bool {
|
|
|
|
debug.Log("testing snapshot with tags %v against list: %v", sn.Tags, l)
|
|
|
|
|
|
|
|
if len(l) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tags := range l {
|
|
|
|
if sn.HasTags(tags) {
|
|
|
|
debug.Log(" snapshot satisfies %v", tags, l)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-07 01:19:06 +00:00
|
|
|
func (sn *Snapshot) hasPath(path string) bool {
|
|
|
|
for _, snPath := range sn.Paths {
|
|
|
|
if path == snPath {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2017-07-09 07:24:02 +00:00
|
|
|
// HasPaths returns true if the snapshot has all of the paths.
|
2017-03-06 01:21:58 +00:00
|
|
|
func (sn *Snapshot) HasPaths(paths []string) bool {
|
|
|
|
for _, path := range paths {
|
2017-07-09 07:24:02 +00:00
|
|
|
if !sn.hasPath(path) {
|
|
|
|
return false
|
2016-05-10 19:51:56 +00:00
|
|
|
}
|
2016-05-10 19:23:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-09 07:24:02 +00:00
|
|
|
return true
|
2017-03-06 01:21:58 +00:00
|
|
|
}
|
|
|
|
|
2017-06-18 11:05:47 +00:00
|
|
|
// Snapshots is a list of snapshots.
|
|
|
|
type Snapshots []*Snapshot
|
|
|
|
|
|
|
|
// Len returns the number of snapshots in sn.
|
|
|
|
func (sn Snapshots) Len() int {
|
|
|
|
return len(sn)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Less returns true iff the ith snapshot has been made after the jth.
|
|
|
|
func (sn Snapshots) Less(i, j int) bool {
|
|
|
|
return sn[i].Time.After(sn[j].Time)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Swap exchanges the two snapshots.
|
|
|
|
func (sn Snapshots) Swap(i, j int) {
|
|
|
|
sn[i], sn[j] = sn[j], sn[i]
|
|
|
|
}
|