2022-08-18 10:11:13 +02:00
|
|
|
// Copyright 2022 - Offen Authors <hioffen@posteo.de>
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
package ssh
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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 {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, errors.New("NewStorageBackend: 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 {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, errors.New("NewStorageBackend: 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 {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, errors.New("NewStorageBackend: 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 {
|
2023-08-25 12:44:43 +02:00
|
|
|
return nil, fmt.Errorf("NewStorageBackend: error creating ssh client: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
_, _, err = sshClient.SendRequest("keepalive", false, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sftpClient, err := sftp.NewClient(sshClient)
|
|
|
|
if err != nil {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, fmt.Errorf("NewStorageBackend: error creating sftp client: %w", err)
|
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 {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Copy: error reading the file to be uploaded: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
defer source.Close()
|
|
|
|
|
|
|
|
destination, err := b.sftpClient.Create(filepath.Join(b.DestinationPath, name))
|
|
|
|
if err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Copy: error creating file: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
defer destination.Close()
|
|
|
|
|
|
|
|
chunk := make([]byte, 1000000)
|
|
|
|
for {
|
|
|
|
num, err := source.Read(chunk)
|
|
|
|
if err == io.EOF {
|
|
|
|
tot, err := destination.Write(chunk[:num])
|
|
|
|
if err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Copy: error uploading the file: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if tot != len(chunk[:num]) {
|
2022-08-18 10:11:13 +02:00
|
|
|
return errors.New("(*sshStorage).Copy: failed to write stream")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Copy: error uploading the file: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tot, err := destination.Write(chunk[:num])
|
|
|
|
if err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Copy: error uploading the file: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if tot != len(chunk[:num]) {
|
2022-08-18 10:11:13 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Copy: 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 {
|
2023-08-25 12:44:43 +02:00
|
|
|
return nil, fmt.Errorf("(*sshStorage).Prune: error reading directory: %w", err)
|
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 {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*sshStorage).Prune: error removing file: %w", err)
|
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
|
|
|
}
|