2024-03-15 11:42:22 +01:00
|
|
|
// Copyright 2022 - offen.software <hioffen@posteo.de>
|
2022-08-18 10:11:13 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2024-02-16 15:35:42 +01:00
|
|
|
"github.com/offen/docker-volume-backup/internal/errwrap"
|
2022-08-18 08:52:09 +02:00
|
|
|
"github.com/offen/docker-volume-backup/internal/storage"
|
|
|
|
"github.com/pkg/sftp"
|
|
|
|
"golang.org/x/crypto/ssh"
|
|
|
|
)
|
|
|
|
|
|
|
|
type sshStorage struct {
|
|
|
|
*storage.StorageBackend
|
|
|
|
client *ssh.Client
|
|
|
|
sftpClient *sftp.Client
|
|
|
|
hostName string
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
// Config allows to configure a SSH backend.
|
|
|
|
type Config struct {
|
|
|
|
HostName string
|
|
|
|
Port string
|
|
|
|
User string
|
|
|
|
Password string
|
|
|
|
IdentityFile string
|
|
|
|
IdentityPassphrase string
|
|
|
|
RemotePath string
|
|
|
|
}
|
2022-08-18 08:52:09 +02:00
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
// NewStorageBackend creates and initializes a new SSH storage backend.
|
|
|
|
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
|
2022-08-18 08:52:09 +02:00
|
|
|
var authMethods []ssh.AuthMethod
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
if opts.Password != "" {
|
|
|
|
authMethods = append(authMethods, ssh.Password(opts.Password))
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
if _, err := os.Stat(opts.IdentityFile); err == nil {
|
2023-08-27 18:14:55 +02:00
|
|
|
key, err := os.ReadFile(opts.IdentityFile)
|
2022-08-18 08:52:09 +02:00
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(nil, "error reading the private key")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var signer ssh.Signer
|
2022-08-18 10:11:13 +02:00
|
|
|
if opts.IdentityPassphrase != "" {
|
|
|
|
signer, err = ssh.ParsePrivateKeyWithPassphrase(key, []byte(opts.IdentityPassphrase))
|
2022-08-18 08:52:09 +02:00
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(nil, "error parsing the encrypted private key")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
authMethods = append(authMethods, ssh.PublicKeys(signer))
|
|
|
|
} else {
|
|
|
|
signer, err = ssh.ParsePrivateKey(key)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(nil, "error parsing the private key")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
authMethods = append(authMethods, ssh.PublicKeys(signer))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sshClientConfig := &ssh.ClientConfig{
|
2022-08-18 10:11:13 +02:00
|
|
|
User: opts.User,
|
2022-08-18 08:52:09 +02:00
|
|
|
Auth: authMethods,
|
|
|
|
HostKeyCallback: ssh.InsecureIgnoreHostKey(),
|
|
|
|
}
|
2022-08-18 10:11:13 +02:00
|
|
|
sshClient, err := ssh.Dial("tcp", fmt.Sprintf("%s:%s", opts.HostName, opts.Port), sshClientConfig)
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error creating ssh client")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
_, _, err = sshClient.SendRequest("keepalive", false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-05 21:38:33 +01:00
|
|
|
sftpClient, err := sftp.NewClient(sshClient,
|
|
|
|
sftp.UseConcurrentReads(true),
|
|
|
|
sftp.UseConcurrentWrites(true),
|
|
|
|
sftp.MaxConcurrentRequestsPerFile(64),
|
|
|
|
)
|
2022-08-18 08:52:09 +02:00
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error creating sftp client")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return &sshStorage{
|
|
|
|
StorageBackend: &storage.StorageBackend{
|
2022-08-18 10:11:13 +02:00
|
|
|
DestinationPath: opts.RemotePath,
|
2022-08-18 08:52:09 +02:00
|
|
|
Log: logFunc,
|
|
|
|
},
|
|
|
|
client: sshClient,
|
|
|
|
sftpClient: sftpClient,
|
2022-08-18 10:11:13 +02:00
|
|
|
hostName: opts.HostName,
|
2022-08-18 08:52:09 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the storage backend
|
|
|
|
func (b *sshStorage) Name() string {
|
|
|
|
return "SSH"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies the given file to the SSH storage backend.
|
|
|
|
func (b *sshStorage) Copy(file string) error {
|
|
|
|
source, err := os.Open(file)
|
|
|
|
_, name := path.Split(file)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, " error reading the file to be uploaded")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := b.sftpClient.Create(filepath.Join(b.DestinationPath, name))
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error creating file")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
|
2023-12-05 21:38:33 +01:00
|
|
|
chunk := make([]byte, 1e9)
|
2022-08-18 08:52:09 +02:00
|
|
|
for {
|
|
|
|
num, err := source.Read(chunk)
|
|
|
|
if err == io.EOF {
|
|
|
|
tot, err := destination.Write(chunk[:num])
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error uploading the file")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if tot != len(chunk[:num]) {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, "failed to write stream")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error uploading the file")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tot, err := destination.Write(chunk[:num])
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error uploading the file")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if tot != len(chunk[:num]) {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(nil, "failed to write stream")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-25 12:44:43 +02:00
|
|
|
b.Log(storage.LogLevelInfo, b.Name(), "Uploaded a copy of backup `%s` to '%s' at path '%s'.", file, b.hostName, b.DestinationPath)
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune rotates away backups according to the configuration and provided deadline for the SSH storage backend.
|
|
|
|
func (b *sshStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
|
|
|
candidates, err := b.sftpClient.ReadDir(b.DestinationPath)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(err, "error reading directory")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
var matches []string
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
if !strings.HasPrefix(candidate.Name(), pruningPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if candidate.ModTime().Before(deadline) {
|
|
|
|
matches = append(matches, candidate.Name())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := &storage.PruneStats{
|
|
|
|
Total: uint(len(candidates)),
|
|
|
|
Pruned: uint(len(matches)),
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:19:44 +01:00
|
|
|
pruneErr := b.DoPrune(b.Name(), len(matches), len(candidates), deadline, func() error {
|
2022-08-18 08:52:09 +02:00
|
|
|
for _, match := range matches {
|
|
|
|
if err := b.sftpClient.Remove(filepath.Join(b.DestinationPath, match)); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error removing file")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2023-11-04 12:19:44 +01:00
|
|
|
})
|
2022-08-18 08:52:09 +02:00
|
|
|
|
2023-11-04 12:19:44 +01:00
|
|
|
return stats, pruneErr
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|