mirror of
https://github.com/offen/docker-volume-backup.git
synced 2024-11-10 00:30:29 +01:00
279844ccfb
* Added abstract helper interface and implemented it for all storage backends * Moved storage client initializations also to helper classes * Fixed ssh init issue * Moved script parameter to helper struct to simplify script init. * Created sub modules. Enhanced abstract implementation. * Fixed config issue * Fixed declaration issues. Added config to interface. * Added StorageProviders to unify all backends. * Cleanup, optimizations, comments. * Applied discussed changes. See description. Moved modules to internal packages. Replaced StoragePool with slice. Moved conditional for init of storage backends back to script. * Fix docker build issue * Fixed accidentally removed local copy condition. * Delete .gitignore * Renaming/changes according to review Renamed Init functions and interface. Replaced config object with specific config values. Init func returns interface instead of struct. Removed custom import names where possible. * Fixed auto-complete error. * Combined copy instructions into one layer. * Added logging func for storages. * Introduced logging func for errors too. * Missed an error message * Moved config back to main. Optimized prune stats handling. * Move stats back to main package * Code doc stuff * Apply changes from #136 * Replace name field with function. * Changed receiver names from stg to b. * Renamed LogFuncDef to Log * Removed redundant package name. * Renamed storagePool to storages. * Simplified creation of new storage backend. * Added initialization for storage stats map. * Invert .dockerignore patterns. * Fix package typo
109 lines
3.3 KiB
Go
109 lines
3.3 KiB
Go
package webdav
|
|
|
|
import (
|
|
"errors"
|
|
"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
|
|
}
|
|
|
|
// NewStorageBackend creates and initializes a new WebDav storage backend.
|
|
func NewStorageBackend(url string, remotePath string, username string, password string, urlInsecure bool,
|
|
logFunc storage.Log) (storage.Backend, error) {
|
|
|
|
if username == "" || password == "" {
|
|
return nil, errors.New("newScript: WEBDAV_URL is defined, but no credentials were provided")
|
|
} else {
|
|
webdavClient := gowebdav.NewClient(url, username, password)
|
|
|
|
if urlInsecure {
|
|
defaultTransport, ok := http.DefaultTransport.(*http.Transport)
|
|
if !ok {
|
|
return nil, errors.New("newScript: unexpected error when asserting type for http.DefaultTransport")
|
|
}
|
|
webdavTransport := defaultTransport.Clone()
|
|
webdavTransport.TLSClientConfig.InsecureSkipVerify = urlInsecure
|
|
webdavClient.SetTransport(webdavTransport)
|
|
}
|
|
|
|
return &webDavStorage{
|
|
StorageBackend: &storage.StorageBackend{
|
|
DestinationPath: remotePath,
|
|
Log: logFunc,
|
|
},
|
|
client: webdavClient,
|
|
}, nil
|
|
}
|
|
}
|
|
|
|
// Name returns the name of the storage backend
|
|
func (b *webDavStorage) Name() string {
|
|
return "WebDav"
|
|
}
|
|
|
|
// Copy copies the given file to the WebDav storage backend.
|
|
func (b *webDavStorage) Copy(file string) error {
|
|
bytes, err := os.ReadFile(file)
|
|
_, name := path.Split(file)
|
|
if err != nil {
|
|
return b.Log(storage.ERROR, b.Name(), "Copy: Error reading the file to be uploaded! %w", err)
|
|
}
|
|
if err := b.client.MkdirAll(b.DestinationPath, 0644); err != nil {
|
|
return b.Log(storage.ERROR, b.Name(), "Copy: Error creating directory '%s' on WebDAV server! %w", b.DestinationPath, err)
|
|
}
|
|
if err := b.client.Write(filepath.Join(b.DestinationPath, name), bytes, 0644); err != nil {
|
|
return b.Log(storage.ERROR, b.Name(), "Copy: Error uploading the file to WebDAV server! %w", err)
|
|
}
|
|
b.Log(storage.INFO, b.Name(), "Uploaded a copy of backup `%s` to WebDAV-URL '%s' at path '%s'.", file, b.url, b.DestinationPath)
|
|
|
|
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 {
|
|
return nil, b.Log(storage.ERROR, b.Name(), "Prune: Error looking up candidates from remote storage! %w", err)
|
|
}
|
|
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)),
|
|
}
|
|
|
|
b.DoPrune(b.Name(), len(matches), lenCandidates, "WebDAV backup(s)", func() error {
|
|
for _, match := range matches {
|
|
if err := b.client.Remove(filepath.Join(b.DestinationPath, match.Name())); err != nil {
|
|
return b.Log(storage.ERROR, b.Name(), "Prune: Error removing file from WebDAV storage! %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
})
|
|
|
|
return stats, nil
|
|
}
|