2
2
mirror of https://github.com/octoleo/restic.git synced 2024-11-27 07:16:40 +00:00
restic/internal/backend/sftp/sftp.go

505 lines
11 KiB
Go
Raw Normal View History

2015-03-28 10:50:23 +00:00
package sftp
2014-10-04 17:20:15 +00:00
import (
"bufio"
2017-06-03 15:39:57 +00:00
"context"
2014-10-04 17:20:15 +00:00
"fmt"
"io"
"os"
"os/exec"
"path"
"time"
2014-10-04 17:20:15 +00:00
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/errors"
2017-07-24 15:42:25 +00:00
"github.com/restic/restic/internal/restic"
2017-07-23 12:21:03 +00:00
"github.com/restic/restic/internal/backend"
"github.com/restic/restic/internal/debug"
"github.com/pkg/sftp"
2014-10-04 17:20:15 +00:00
)
2016-01-24 19:23:50 +00:00
// SFTP is a backend in a directory accessed via SFTP.
2014-10-04 17:20:15 +00:00
type SFTP struct {
c *sftp.Client
p string
2014-10-04 17:20:15 +00:00
cmd *exec.Cmd
result <-chan error
2017-04-10 20:40:24 +00:00
backend.Layout
2017-04-10 20:41:06 +00:00
Config
2014-10-04 17:20:15 +00:00
}
2016-08-31 20:51:35 +00:00
var _ restic.Backend = &SFTP{}
2017-04-10 20:40:24 +00:00
const defaultLayout = "default"
2018-01-17 22:14:47 +00:00
func startClient(program string, args ...string) (*SFTP, error) {
2017-04-03 19:05:42 +00:00
debug.Log("start client %v %v", program, args)
2014-10-04 17:20:15 +00:00
// Connect to a remote host and request the sftp subsystem via the 'ssh'
// command. This assumes that passwordless login is correctly configured.
cmd := exec.Command(program, args...)
// prefix the errors with the program name
stderr, err := cmd.StderrPipe()
if err != nil {
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "cmd.StderrPipe")
}
go func() {
sc := bufio.NewScanner(stderr)
for sc.Scan() {
fmt.Fprintf(os.Stderr, "subprocess %v: %v\n", program, sc.Text())
}
}()
2014-10-04 17:20:15 +00:00
// get stdin and stdout
wr, err := cmd.StdinPipe()
if err != nil {
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "cmd.StdinPipe")
2014-10-04 17:20:15 +00:00
}
rd, err := cmd.StdoutPipe()
if err != nil {
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "cmd.StdoutPipe")
2014-10-04 17:20:15 +00:00
}
bg, err := backend.StartForeground(cmd)
if err != nil {
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "cmd.Start")
2014-10-04 17:20:15 +00:00
}
// wait in a different goroutine
ch := make(chan error, 1)
go func() {
err := cmd.Wait()
2016-09-27 20:35:08 +00:00
debug.Log("ssh command exited, err %v", err)
for {
ch <- errors.Wrap(err, "ssh command exited")
}
}()
2014-10-04 17:20:15 +00:00
// open the SFTP session
client, err := sftp.NewClientPipe(rd, wr)
if err != nil {
return nil, errors.Errorf("unable to start the sftp session, error: %v", err)
2014-10-04 17:20:15 +00:00
}
err = bg()
if err != nil {
return nil, errors.Wrap(err, "bg")
}
return &SFTP{c: client, cmd: cmd, result: ch}, nil
2014-10-04 17:20:15 +00:00
}
// clientError returns an error if the client has exited. Otherwise, nil is
// returned immediately.
func (r *SFTP) clientError() error {
select {
case err := <-r.result:
2016-09-27 20:35:08 +00:00
debug.Log("client has exited with err %v", err)
return err
default:
}
return nil
}
2017-04-10 20:41:20 +00:00
// Open opens an sftp backend as described by the config by running
// "ssh" with the appropriate arguments (or cfg.Command, if set). The function
// preExec is run just before, postExec just after starting a program.
2018-01-17 22:14:47 +00:00
func Open(cfg Config) (*SFTP, error) {
2017-04-10 20:41:20 +00:00
debug.Log("open backend with config %#v", cfg)
cmd, args, err := buildSSHCommand(cfg)
if err != nil {
return nil, err
}
2018-01-17 22:14:47 +00:00
sftp, err := startClient(cmd, args...)
2014-10-04 17:20:15 +00:00
if err != nil {
2016-09-27 20:35:08 +00:00
debug.Log("unable to start program: %v", err)
2014-10-04 17:20:15 +00:00
return nil, err
}
sftp.Layout, err = backend.ParseLayout(sftp, cfg.Layout, defaultLayout, cfg.Path)
2017-04-10 20:40:24 +00:00
if err != nil {
return nil, err
}
debug.Log("layout: %v\n", sftp.Layout)
2017-04-10 20:41:20 +00:00
sftp.Config = cfg
sftp.p = cfg.Path
2014-10-04 17:20:15 +00:00
return sftp, nil
}
2017-07-16 13:10:06 +00:00
func (r *SFTP) mkdirAllDataSubdirs() error {
for _, d := range r.Paths() {
err := r.c.MkdirAll(d)
2017-07-16 13:10:06 +00:00
if err != nil {
return err
}
}
return nil
}
2017-04-10 20:41:20 +00:00
// Join combines path components with slashes (according to the sftp spec).
func (r *SFTP) Join(p ...string) string {
return path.Join(p...)
}
// ReadDir returns the entries for a directory.
func (r *SFTP) ReadDir(dir string) ([]os.FileInfo, error) {
fi, err := r.c.ReadDir(dir)
// sftp client does not specify dir name on error, so add it here
err = errors.Wrapf(err, "(%v)", dir)
return fi, err
2017-04-10 20:41:20 +00:00
}
// IsNotExist returns true if the error is caused by a not existing file.
func (r *SFTP) IsNotExist(err error) bool {
err = errors.Cause(err)
if os.IsNotExist(err) {
return true
}
statusError, ok := err.(*sftp.StatusError)
if !ok {
return false
}
return statusError.Error() == `sftp: "No such file" (SSH_FX_NO_SUCH_FILE)`
}
2017-04-10 20:41:20 +00:00
func buildSSHCommand(cfg Config) (cmd string, args []string, err error) {
if cfg.Command != "" {
2018-03-13 19:50:37 +00:00
args, err := backend.SplitShellStrings(cfg.Command)
if err != nil {
return "", nil, err
}
return args[0], args[1:], nil
2017-04-10 20:41:20 +00:00
}
cmd = "ssh"
host, port := cfg.Host, cfg.Port
args = []string{host}
if port != "" {
args = append(args, "-p", port)
}
2015-12-28 17:22:19 +00:00
if cfg.User != "" {
args = append(args, "-l")
args = append(args, cfg.User)
}
args = append(args, "-s")
args = append(args, "sftp")
2017-04-10 20:41:20 +00:00
return cmd, args, nil
2015-12-28 17:22:19 +00:00
}
// Create creates an sftp backend as described by the config by running "ssh"
// with the appropriate arguments (or cfg.Command, if set). The function
// preExec is run just before, postExec just after starting a program.
2018-01-17 22:14:47 +00:00
func Create(cfg Config) (*SFTP, error) {
2017-04-10 20:41:20 +00:00
cmd, args, err := buildSSHCommand(cfg)
if err != nil {
return nil, err
2017-04-03 19:05:42 +00:00
}
2018-01-17 22:14:47 +00:00
sftp, err := startClient(cmd, args...)
2017-04-03 19:05:42 +00:00
if err != nil {
2017-04-10 20:41:20 +00:00
debug.Log("unable to start program: %v", err)
2017-04-03 19:05:42 +00:00
return nil, err
}
sftp.Layout, err = backend.ParseLayout(sftp, cfg.Layout, defaultLayout, cfg.Path)
2014-10-04 17:20:15 +00:00
if err != nil {
return nil, err
}
2015-05-04 18:39:45 +00:00
// test if config file already exists
2017-04-10 20:41:20 +00:00
_, err = sftp.c.Lstat(Join(cfg.Path, backend.Paths.Config))
2014-10-04 17:20:15 +00:00
if err == nil {
return nil, errors.New("config file already exists")
}
2017-04-19 16:56:01 +00:00
// create paths for data and refs
2017-07-16 13:10:06 +00:00
if err = sftp.mkdirAllDataSubdirs(); err != nil {
return nil, err
2014-10-04 17:20:15 +00:00
}
err = sftp.Close()
2014-10-04 17:20:15 +00:00
if err != nil {
2016-08-29 19:54:50 +00:00
return nil, errors.Wrap(err, "Close")
2014-10-04 17:20:15 +00:00
}
2014-10-07 21:19:26 +00:00
// open backend
2018-01-17 22:14:47 +00:00
return Open(cfg)
2015-12-28 17:22:19 +00:00
}
2014-10-04 17:20:15 +00:00
// Location returns this backend's location (the directory name).
func (r *SFTP) Location() string {
return r.p
}
// Join joins the given paths and cleans them afterwards. This always uses
// forward slashes, which is required by sftp.
func Join(parts ...string) string {
return path.Clean(path.Join(parts...))
}
2016-01-24 00:15:35 +00:00
// Save stores data in the backend at the handle.
func (r *SFTP) Save(ctx context.Context, h restic.Handle, rd restic.RewindReader) error {
2017-04-10 20:40:24 +00:00
debug.Log("Save %v", h)
if err := r.clientError(); err != nil {
return err
}
2016-01-24 00:15:35 +00:00
if err := h.Valid(); err != nil {
return err
}
2017-04-10 20:40:24 +00:00
filename := r.Filename(h)
// create new file
f, err := r.c.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY)
if r.IsNotExist(err) {
// error is caused by a missing directory, try to create it
mkdirErr := r.c.MkdirAll(r.Dirname(h))
if mkdirErr != nil {
debug.Log("error creating dir %v: %v", r.Dirname(h), mkdirErr)
} else {
// try again
f, err = r.c.OpenFile(filename, os.O_CREATE|os.O_EXCL|os.O_WRONLY)
}
2017-04-10 20:40:24 +00:00
}
if err != nil {
return errors.Wrap(err, "OpenFile")
2017-04-10 20:40:24 +00:00
}
// save data
_, err = io.Copy(f, rd)
2017-04-10 20:40:24 +00:00
if err != nil {
2017-05-11 19:53:57 +00:00
_ = f.Close()
return errors.Wrap(err, "Write")
}
err = f.Close()
if err != nil {
return errors.Wrap(err, "Close")
2017-04-10 20:40:24 +00:00
}
return errors.Wrap(r.c.Chmod(filename, backend.Modes.File), "Chmod")
2016-01-24 00:15:35 +00:00
}
// Load runs fn with a reader that yields the contents of the file at h at the
// given offset.
func (r *SFTP) Load(ctx context.Context, h restic.Handle, length int, offset int64, fn func(rd io.Reader) error) error {
return backend.DefaultLoad(ctx, h, length, offset, r.openReader, fn)
}
func (r *SFTP) openReader(ctx context.Context, h restic.Handle, length int, offset int64) (io.ReadCloser, error) {
2017-01-23 17:11:10 +00:00
debug.Log("Load %v, length %v, offset %v", h, length, offset)
2017-01-22 21:01:12 +00:00
if err := h.Valid(); err != nil {
return nil, err
}
if offset < 0 {
return nil, errors.New("offset is negative")
}
f, err := r.c.Open(r.Filename(h))
2017-01-22 21:01:12 +00:00
if err != nil {
return nil, err
}
if offset > 0 {
_, err = f.Seek(offset, 0)
if err != nil {
_ = f.Close()
2017-01-22 21:01:12 +00:00
return nil, err
}
}
if length > 0 {
return backend.LimitReadCloser(f, int64(length)), nil
}
return f, nil
}
2016-01-23 22:27:58 +00:00
// Stat returns information about a blob.
2017-06-03 15:39:57 +00:00
func (r *SFTP) Stat(ctx context.Context, h restic.Handle) (restic.FileInfo, error) {
debug.Log("Stat(%v)", h)
if err := r.clientError(); err != nil {
2016-08-31 20:51:35 +00:00
return restic.FileInfo{}, err
}
2016-01-23 22:27:58 +00:00
if err := h.Valid(); err != nil {
2016-08-31 20:51:35 +00:00
return restic.FileInfo{}, err
2016-01-23 22:27:58 +00:00
}
fi, err := r.c.Lstat(r.Filename(h))
2016-01-23 22:27:58 +00:00
if err != nil {
2016-08-31 20:51:35 +00:00
return restic.FileInfo{}, errors.Wrap(err, "Lstat")
2016-01-23 22:27:58 +00:00
}
return restic.FileInfo{Size: fi.Size(), Name: h.Name}, nil
2016-01-23 22:27:58 +00:00
}
2015-03-28 10:50:23 +00:00
// Test returns true if a blob of the given type and name exists in the backend.
2017-06-03 15:39:57 +00:00
func (r *SFTP) Test(ctx context.Context, h restic.Handle) (bool, error) {
debug.Log("Test(%v)", h)
if err := r.clientError(); err != nil {
return false, err
}
_, err := r.c.Lstat(r.Filename(h))
if os.IsNotExist(errors.Cause(err)) {
return false, nil
}
if err != nil {
2016-08-29 19:54:50 +00:00
return false, errors.Wrap(err, "Lstat")
2014-10-04 17:20:15 +00:00
}
return true, nil
2014-10-04 17:20:15 +00:00
}
2015-03-28 10:50:23 +00:00
// Remove removes the content stored at name.
2017-06-03 15:39:57 +00:00
func (r *SFTP) Remove(ctx context.Context, h restic.Handle) error {
debug.Log("Remove(%v)", h)
if err := r.clientError(); err != nil {
return err
}
return r.c.Remove(r.Filename(h))
2014-10-04 17:20:15 +00:00
}
// List runs fn for each file in the backend which has the type t. When an
// error occurs (or fn returns an error), List stops and returns it.
func (r *SFTP) List(ctx context.Context, t restic.FileType, fn func(restic.FileInfo) error) error {
2017-04-10 21:31:13 +00:00
debug.Log("List %v", t)
basedir, subdirs := r.Basedir(t)
walker := r.c.Walk(basedir)
for walker.Step() {
if walker.Err() != nil {
if r.IsNotExist(walker.Err()) {
debug.Log("ignoring non-existing directory")
return nil
}
return walker.Err()
}
if walker.Path() == basedir {
continue
}
if walker.Stat().IsDir() && !subdirs {
walker.SkipDir()
continue
}
fi := walker.Stat()
if !fi.Mode().IsRegular() {
continue
}
debug.Log("send %v\n", path.Base(walker.Path()))
rfi := restic.FileInfo{
Name: path.Base(walker.Path()),
Size: fi.Size(),
}
if ctx.Err() != nil {
return ctx.Err()
2014-10-04 17:20:15 +00:00
}
2015-03-28 10:50:23 +00:00
err := fn(rfi)
if err != nil {
return err
}
if ctx.Err() != nil {
return ctx.Err()
}
}
2014-10-04 17:20:15 +00:00
return ctx.Err()
2014-10-04 17:20:15 +00:00
}
var closeTimeout = 2 * time.Second
2014-10-04 17:20:15 +00:00
// Close closes the sftp connection and terminates the underlying command.
2016-01-24 19:23:50 +00:00
func (r *SFTP) Close() error {
debug.Log("Close")
2016-01-24 19:23:50 +00:00
if r == nil {
return nil
}
err := r.c.Close()
2016-09-27 20:35:08 +00:00
debug.Log("Close returned error %v", err)
// wait for closeTimeout before killing the process
select {
case err := <-r.result:
return err
case <-time.After(closeTimeout):
}
2016-01-24 19:23:50 +00:00
if err := r.cmd.Process.Kill(); err != nil {
return err
}
// get the error, but ignore it
<-r.result
return nil
2014-10-04 17:20:15 +00:00
}
2017-10-14 14:08:15 +00:00
func (r *SFTP) deleteRecursive(name string) error {
entries, err := r.ReadDir(name)
2017-10-14 14:08:15 +00:00
if err != nil {
return errors.Wrap(err, "ReadDir")
}
for _, fi := range entries {
itemName := r.Join(name, fi.Name())
if fi.IsDir() {
err := r.deleteRecursive(itemName)
if err != nil {
return errors.Wrap(err, "ReadDir")
}
err = r.c.RemoveDirectory(itemName)
if err != nil {
return errors.Wrap(err, "RemoveDirectory")
}
continue
}
err := r.c.Remove(itemName)
if err != nil {
return errors.Wrap(err, "ReadDir")
}
}
return nil
}
// Delete removes all data in the backend.
func (r *SFTP) Delete(context.Context) error {
2017-10-14 14:08:15 +00:00
return r.deleteRecursive(r.p)
}