2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-15 17:47:21 +00:00
restic/src/cmds/restic/cmd_backup.go

501 lines
12 KiB
Go
Raw Normal View History

2014-04-27 22:00:15 +00:00
package main
import (
2016-04-01 11:50:45 +00:00
"bufio"
2014-04-27 22:00:15 +00:00
"fmt"
"io"
2014-09-23 20:39:12 +00:00
"os"
2015-03-02 13:48:47 +00:00
"path/filepath"
"restic"
"strings"
"time"
2016-09-17 10:36:05 +00:00
"github.com/spf13/cobra"
2015-04-25 17:20:41 +00:00
2016-09-17 10:36:05 +00:00
"restic/archiver"
"restic/debug"
"restic/errors"
"restic/filter"
"restic/fs"
)
2015-04-25 17:20:41 +00:00
2016-09-17 10:36:05 +00:00
var cmdBackup = &cobra.Command{
Use: "backup [flags] FILE/DIR [FILE/DIR] ...",
Short: "create a new backup of files and/or directories",
Long: `
The "backup" command creates a new snapshot and saves the files and directories
given as the arguments.
`,
RunE: func(cmd *cobra.Command, args []string) error {
if backupOptions.Stdin && backupOptions.FilesFrom == "-" {
return errors.Fatal("cannot use both `--stdin` and `--files-from -`")
}
2016-09-17 10:36:05 +00:00
if backupOptions.Stdin {
return readBackupFromStdin(backupOptions, globalOptions, args)
}
2015-04-25 17:20:41 +00:00
2016-09-17 10:36:05 +00:00
return runBackup(backupOptions, globalOptions, args)
},
2015-04-25 17:20:41 +00:00
}
2016-09-17 10:36:05 +00:00
// BackupOptions bundles all options for the backup command.
type BackupOptions struct {
Parent string
Force bool
Excludes []string
ExcludeFiles []string
2016-09-17 10:36:05 +00:00
ExcludeOtherFS bool
Stdin bool
StdinFilename string
Tags []string
2017-02-10 18:37:33 +00:00
Hostname string
FilesFrom string
2015-04-25 17:20:41 +00:00
}
2016-09-17 10:36:05 +00:00
var backupOptions BackupOptions
2016-09-17 10:36:05 +00:00
func init() {
cmdRoot.AddCommand(cmdBackup)
2017-02-10 18:37:33 +00:00
hostname, err := os.Hostname()
if err != nil {
debug.Log("os.Hostname() returned err: %v", err)
hostname = ""
}
2016-09-17 10:36:05 +00:00
f := cmdBackup.Flags()
f.StringVar(&backupOptions.Parent, "parent", "", "use this parent snapshot (default: last snapshot in the repo that has the same target files/directories)")
f.BoolVarP(&backupOptions.Force, "force", "f", false, `force re-reading the target files/directories (overrides the "parent" flag)`)
f.StringSliceVarP(&backupOptions.Excludes, "exclude", "e", nil, "exclude a `pattern` (can be specified multiple times)")
f.StringSliceVar(&backupOptions.ExcludeFiles, "exclude-file", nil, "read exclude patterns from a `file` (can be specified multiple times)")
f.BoolVarP(&backupOptions.ExcludeOtherFS, "one-file-system", "x", false, "exclude other file systems")
2016-09-17 10:36:05 +00:00
f.BoolVar(&backupOptions.Stdin, "stdin", false, "read backup from stdin")
f.StringVar(&backupOptions.StdinFilename, "stdin-filename", "stdin", "file name to use when reading from stdin")
f.StringSliceVar(&backupOptions.Tags, "tag", nil, "add a `tag` for the new snapshot (can be specified multiple times)")
2017-02-10 18:37:33 +00:00
f.StringVar(&backupOptions.Hostname, "hostname", hostname, "set the `hostname` for the snapshot manually")
f.StringVar(&backupOptions.FilesFrom, "files-from", "", "read the files to backup from file (can be combined with file args)")
2014-12-07 15:30:52 +00:00
}
2016-09-17 10:36:05 +00:00
func newScanProgress(gopts GlobalOptions) *restic.Progress {
if gopts.Quiet {
2015-02-21 13:23:49 +00:00
return nil
}
p := restic.NewProgress()
2015-02-21 14:32:48 +00:00
p.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
if IsProcessBackground() {
return
}
PrintProgress("[%s] %d directories, %d files, %s", formatDuration(d), s.Dirs, s.Files, formatBytes(s.Bytes))
2015-02-21 14:32:48 +00:00
}
2015-02-21 14:32:48 +00:00
p.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
PrintProgress("scanned %d directories, %d files in %s\n", s.Dirs, s.Files, formatDuration(d))
2015-02-21 14:32:48 +00:00
}
return p
}
2016-09-17 10:36:05 +00:00
func newArchiveProgress(gopts GlobalOptions, todo restic.Stat) *restic.Progress {
if gopts.Quiet {
2015-02-21 13:23:49 +00:00
return nil
}
archiveProgress := restic.NewProgress()
2015-02-21 13:23:49 +00:00
var bps, eta uint64
itemsTodo := todo.Files + todo.Dirs
archiveProgress.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
if IsProcessBackground() {
return
}
2015-02-21 13:23:49 +00:00
sec := uint64(d / time.Second)
if todo.Bytes > 0 && sec > 0 && ticker {
bps = s.Bytes / sec
2015-03-16 19:20:53 +00:00
if s.Bytes >= todo.Bytes {
eta = 0
} else if bps > 0 {
2015-02-21 13:23:49 +00:00
eta = (todo.Bytes - s.Bytes) / bps
}
}
itemsDone := s.Files + s.Dirs
2015-03-16 19:20:53 +00:00
status1 := fmt.Sprintf("[%s] %s %s/s %s / %s %d / %d items %d errors ",
2015-04-24 23:39:32 +00:00
formatDuration(d),
2015-04-25 17:20:41 +00:00
formatPercent(s.Bytes, todo.Bytes),
2015-04-24 23:39:32 +00:00
formatBytes(bps),
formatBytes(s.Bytes), formatBytes(todo.Bytes),
itemsDone, itemsTodo,
s.Errors)
2015-04-24 23:39:32 +00:00
status2 := fmt.Sprintf("ETA %s ", formatSeconds(eta))
if w := stdoutTerminalWidth(); w > 0 {
2016-08-22 20:07:10 +00:00
maxlen := w - len(status2) - 1
if maxlen < 4 {
status1 = ""
} else if len(status1) > maxlen {
status1 = status1[:maxlen-4]
status1 += "... "
}
}
PrintProgress("%s%s", status1, status2)
2015-02-21 13:23:49 +00:00
}
archiveProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
2015-04-25 17:20:41 +00:00
fmt.Printf("\nduration: %s, %s\n", formatDuration(d), formatRate(todo.Bytes, d))
2015-02-21 13:23:49 +00:00
}
return archiveProgress
2015-02-16 22:44:26 +00:00
}
2016-09-17 10:36:05 +00:00
func newArchiveStdinProgress(gopts GlobalOptions) *restic.Progress {
if gopts.Quiet {
2016-05-10 19:51:56 +00:00
return nil
}
archiveProgress := restic.NewProgress()
2016-05-10 19:51:56 +00:00
var bps uint64
archiveProgress.OnUpdate = func(s restic.Stat, d time.Duration, ticker bool) {
if IsProcessBackground() {
return
}
2016-05-10 19:51:56 +00:00
sec := uint64(d / time.Second)
if s.Bytes > 0 && sec > 0 && ticker {
bps = s.Bytes / sec
}
status1 := fmt.Sprintf("[%s] %s %s/s", formatDuration(d),
formatBytes(s.Bytes),
formatBytes(bps))
if w := stdoutTerminalWidth(); w > 0 {
2016-05-10 19:51:56 +00:00
maxlen := w - len(status1)
if maxlen < 4 {
status1 = ""
} else if len(status1) > maxlen {
status1 = status1[:maxlen-4]
status1 += "... "
}
}
2016-09-13 19:01:29 +00:00
PrintProgress("%s", status1)
2016-05-10 19:51:56 +00:00
}
archiveProgress.OnDone = func(s restic.Stat, d time.Duration, ticker bool) {
fmt.Printf("\nduration: %s, %s\n", formatDuration(d), formatRate(s.Bytes, d))
}
return archiveProgress
}
// filterExisting returns a slice of all existing items, or an error if no
// items exist at all.
func filterExisting(items []string) (result []string, err error) {
for _, item := range items {
_, err := fs.Lstat(item)
if err != nil && os.IsNotExist(errors.Cause(err)) {
continue
}
result = append(result, item)
}
if len(result) == 0 {
2016-09-01 20:17:37 +00:00
return nil, errors.Fatal("all target directories/files do not exist")
}
return
}
// gatherDevices returns the set of unique device ids of the files and/or
// directory paths listed in "items".
func gatherDevices(items []string) (deviceMap map[string]uint64, err error) {
deviceMap = make(map[string]uint64)
for _, item := range items {
fi, err := fs.Lstat(item)
if err != nil {
return nil, err
}
id, err := fs.DeviceID(fi)
if err != nil {
return nil, err
}
deviceMap[item] = id
}
if len(deviceMap) == 0 {
return nil, errors.New("zero allowed devices")
}
return deviceMap, nil
}
2016-09-17 10:36:05 +00:00
func readBackupFromStdin(opts BackupOptions, gopts GlobalOptions, args []string) error {
2016-05-10 19:51:56 +00:00
if len(args) != 0 {
return errors.Fatal("when reading from stdin, no additional files can be specified")
2016-05-10 19:51:56 +00:00
}
if opts.StdinFilename == "" {
return errors.Fatal("filename for backup from stdin must not be empty")
}
if gopts.password == "" && gopts.PasswordFile == "" {
return errors.Fatal("unable to read password from stdin when data is to be read from stdin, use --password-file or $RESTIC_PASSWORD")
}
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
2016-05-10 19:51:56 +00:00
if err != nil {
return err
}
lock, err := lockRepo(repo)
defer unlockRepo(lock)
if err != nil {
return err
}
err = repo.LoadIndex()
if err != nil {
return err
}
2017-03-02 14:45:35 +00:00
r := &archiver.Reader{
Repository: repo,
Tags: opts.Tags,
Hostname: opts.Hostname,
}
_, id, err := r.Archive(opts.StdinFilename, os.Stdin, newArchiveStdinProgress(gopts))
2016-05-10 19:51:56 +00:00
if err != nil {
return err
}
Verbosef("archived as %v\n", id.Str())
2016-05-10 19:51:56 +00:00
return nil
}
// readFromFile will read all lines from the given filename and write them to a
// string array, if filename is empty readFromFile returns and empty string
// array. If filename is a dash (-), readFromFile will read the lines from
// the standard input.
func readLinesFromFile(filename string) ([]string, error) {
if filename == "" {
return nil, nil
}
var r io.Reader = os.Stdin
if filename != "-" {
f, err := os.Open(filename)
if err != nil {
return nil, err
}
defer f.Close()
r = f
}
var lines []string
scanner := bufio.NewScanner(r)
for scanner.Scan() {
line := scanner.Text()
if line == "" {
continue
}
lines = append(lines, line)
}
if err := scanner.Err(); err != nil {
return nil, err
}
return lines, nil
}
2016-09-17 10:36:05 +00:00
func runBackup(opts BackupOptions, gopts GlobalOptions, args []string) error {
if opts.FilesFrom == "-" && gopts.password == "" && gopts.PasswordFile == "" {
return errors.Fatal("no password; either use `--password-file` option or put the password into the RESTIC_PASSWORD environment variable")
}
fromfile, err := readLinesFromFile(opts.FilesFrom)
if err != nil {
return err
}
// merge files from files-from into normal args so we can reuse the normal
// args checks and have the ability to use both files-from and args at the
// same time
args = append(args, fromfile...)
2015-03-02 13:48:47 +00:00
if len(args) == 0 {
return errors.Fatal("wrong number of parameters")
2014-12-07 15:30:52 +00:00
}
target := make([]string, 0, len(args))
2015-03-02 13:48:47 +00:00
for _, d := range args {
if a, err := filepath.Abs(d); err == nil {
d = a
}
target = append(target, d)
}
target, err = filterExisting(target)
if err != nil {
return err
}
// allowed devices
var allowedDevs map[string]uint64
2016-09-17 10:36:05 +00:00
if opts.ExcludeOtherFS {
allowedDevs, err = gatherDevices(target)
if err != nil {
return err
}
2016-09-27 20:35:08 +00:00
debug.Log("allowed devices: %v\n", allowedDevs)
}
2016-09-17 10:36:05 +00:00
repo, err := OpenRepository(gopts)
2014-12-07 15:30:52 +00:00
if err != nil {
return err
2014-04-27 22:00:15 +00:00
}
lock, err := lockRepo(repo)
defer unlockRepo(lock)
2015-06-27 12:40:18 +00:00
if err != nil {
return err
}
err = repo.LoadIndex()
if err != nil {
return err
}
2016-09-01 14:04:29 +00:00
var parentSnapshotID *restic.ID
2014-11-30 21:34:21 +00:00
// Force using a parent
2016-09-17 10:36:05 +00:00
if !opts.Force && opts.Parent != "" {
id, err := restic.FindSnapshot(repo, opts.Parent)
2014-11-30 21:34:21 +00:00
if err != nil {
2016-09-17 10:36:05 +00:00
return errors.Fatalf("invalid id %q: %v", opts.Parent, err)
2014-11-30 21:34:21 +00:00
}
parentSnapshotID = &id
2014-11-30 21:34:21 +00:00
}
2014-04-27 22:00:15 +00:00
// Find last snapshot to set it as parent, if not already set
2016-09-17 10:36:05 +00:00
if !opts.Force && parentSnapshotID == nil {
id, err := restic.FindLatestSnapshot(repo, target, opts.Tags, opts.Hostname)
if err == nil {
parentSnapshotID = &id
} else if err != restic.ErrNoSnapshotFound {
return err
}
}
if parentSnapshotID != nil {
2016-09-17 10:36:05 +00:00
Verbosef("using parent snapshot %v\n", parentSnapshotID.Str())
}
2016-09-17 10:36:05 +00:00
Verbosef("scan %v\n", target)
2016-04-01 11:50:45 +00:00
// add patterns from file
if len(opts.ExcludeFiles) > 0 {
for _, filename := range opts.ExcludeFiles {
file, err := fs.Open(filename)
if err != nil {
Warnf("error reading exclude patterns: %v", err)
return nil
}
2016-04-01 11:50:45 +00:00
scanner := bufio.NewScanner(file)
for scanner.Scan() {
line := strings.TrimSpace(scanner.Text())
// ignore empty lines
if line == "" {
continue
}
// strip comments
if strings.HasPrefix(line, "#") {
continue
}
line = os.ExpandEnv(line)
opts.Excludes = append(opts.Excludes, line)
}
2016-04-01 11:50:45 +00:00
}
}
selectFilter := func(item string, fi os.FileInfo) bool {
2016-09-17 10:36:05 +00:00
matched, err := filter.List(opts.Excludes, item)
if err != nil {
2016-09-17 10:36:05 +00:00
Warnf("error for exclude pattern: %v", err)
}
if matched {
2016-09-27 20:35:08 +00:00
debug.Log("path %q excluded by a filter", item)
return false
}
2016-11-05 11:37:54 +00:00
if !opts.ExcludeOtherFS || fi == nil {
return true
}
id, err := fs.DeviceID(fi)
if err != nil {
// This should never happen because gatherDevices() would have
// errored out earlier. If it still does that's a reason to panic.
panic(err)
}
for dir := item; dir != ""; dir = filepath.Dir(dir) {
debug.Log("item %v, test dir %v", item, dir)
allowedID, ok := allowedDevs[dir]
if !ok {
continue
}
if allowedID != id {
debug.Log("path %q on disallowed device %d", item, id)
return false
}
return true
}
panic(fmt.Sprintf("item %v, device id %v not found, allowedDevs: %v", item, id, allowedDevs))
}
2016-09-17 10:36:05 +00:00
stat, err := archiver.Scan(target, selectFilter, newScanProgress(gopts))
if err != nil {
return err
}
2016-09-01 14:04:29 +00:00
arch := archiver.New(repo)
2016-09-17 10:36:05 +00:00
arch.Excludes = opts.Excludes
arch.SelectFilter = selectFilter
2014-11-23 11:05:43 +00:00
arch.Warn = func(dir string, fi os.FileInfo, err error) {
// TODO: make ignoring errors configurable
Warnf("%s\rwarning for %s: %v\n", ClearLine(), dir, err)
}
2017-02-10 18:37:33 +00:00
_, id, err := arch.Snapshot(newArchiveProgress(gopts, stat), target, opts.Tags, opts.Hostname, parentSnapshotID)
if err != nil {
2015-02-03 21:05:46 +00:00
return err
}
2016-09-17 10:36:05 +00:00
Verbosef("snapshot %s saved\n", id.Str())
2014-04-27 22:00:15 +00:00
return nil
}