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 webdav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2022-08-18 10:11:13 +02:00
|
|
|
"fmt"
|
2022-08-18 08:52:09 +02:00
|
|
|
"io/fs"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/offen/docker-volume-backup/internal/storage"
|
|
|
|
"github.com/studio-b12/gowebdav"
|
|
|
|
)
|
|
|
|
|
|
|
|
type webDavStorage struct {
|
|
|
|
*storage.StorageBackend
|
|
|
|
client *gowebdav.Client
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
// Config allows to configure a WebDAV storage backend.
|
|
|
|
type Config struct {
|
|
|
|
URL string
|
|
|
|
RemotePath string
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
URLInsecure bool
|
|
|
|
}
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
// NewStorageBackend creates and initializes a new WebDav storage backend.
|
2022-08-18 10:11:13 +02:00
|
|
|
func NewStorageBackend(opts Config, logFunc storage.Log) (storage.Backend, error) {
|
|
|
|
if opts.Username == "" || opts.Password == "" {
|
|
|
|
return nil, errors.New("NewStorageBackend: WEBDAV_URL is defined, but no credentials were provided")
|
2022-08-18 08:52:09 +02:00
|
|
|
} else {
|
2022-08-18 10:11:13 +02:00
|
|
|
webdavClient := gowebdav.NewClient(opts.URL, opts.Username, opts.Password)
|
2022-08-18 08:52:09 +02:00
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
if opts.URLInsecure {
|
2022-08-18 08:52:09 +02:00
|
|
|
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
|
|
|
|
if !ok {
|
2022-08-18 10:11:13 +02:00
|
|
|
return nil, errors.New("NewStorageBackend: unexpected error when asserting type for http.DefaultTransport")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
webdavTransport := defaultTransport.Clone()
|
2022-08-18 10:11:13 +02:00
|
|
|
webdavTransport.TLSClientConfig.InsecureSkipVerify = opts.URLInsecure
|
2022-08-18 08:52:09 +02:00
|
|
|
webdavClient.SetTransport(webdavTransport)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &webDavStorage{
|
|
|
|
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: webdavClient,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name returns the name of the storage backend
|
|
|
|
func (b *webDavStorage) Name() string {
|
2022-08-18 10:11:13 +02:00
|
|
|
return "WebDAV"
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies the given file to the WebDav storage backend.
|
|
|
|
func (b *webDavStorage) Copy(file string) error {
|
|
|
|
_, name := path.Split(file)
|
|
|
|
if err := b.client.MkdirAll(b.DestinationPath, 0644); err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*webDavStorage).Copy: error creating directory '%s' on server: %w", b.DestinationPath, err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
2023-01-16 08:28:29 +01:00
|
|
|
|
|
|
|
r, err := os.Open(file)
|
|
|
|
if err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*webDavStorage).Copy: error opening the file to be uploaded: %w", err)
|
2023-01-16 08:28:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := b.client.WriteStream(filepath.Join(b.DestinationPath, name), r, 0644); err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*webDavStorage).Copy: error uploading the file: %w", err)
|
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.url, 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 WebDav storage backend.
|
|
|
|
func (b *webDavStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
|
|
|
candidates, err := b.client.ReadDir(b.DestinationPath)
|
|
|
|
if err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return nil, fmt.Errorf("(*webDavStorage).Prune: error looking up candidates from remote storage: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
var matches []fs.FileInfo
|
|
|
|
var lenCandidates int
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
if !strings.HasPrefix(candidate.Name(), pruningPrefix) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
lenCandidates++
|
|
|
|
if candidate.ModTime().Before(deadline) {
|
|
|
|
matches = append(matches, candidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stats := &storage.PruneStats{
|
|
|
|
Total: uint(lenCandidates),
|
|
|
|
Pruned: uint(len(matches)),
|
|
|
|
}
|
|
|
|
|
2023-11-04 12:19:44 +01:00
|
|
|
pruneErr := b.DoPrune(b.Name(), len(matches), lenCandidates, deadline, func() error {
|
2022-08-18 08:52:09 +02:00
|
|
|
for _, match := range matches {
|
|
|
|
if err := b.client.Remove(filepath.Join(b.DestinationPath, match.Name())); err != nil {
|
2023-08-25 12:44:43 +02:00
|
|
|
return fmt.Errorf("(*webDavStorage).Prune: error removing file: %w", err)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
2023-11-04 12:19:44 +01:00
|
|
|
})
|
|
|
|
return stats, pruneErr
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|