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 local
|
|
|
|
|
|
|
|
import (
|
2023-02-02 21:07:25 +01:00
|
|
|
"errors"
|
2022-08-18 08:52:09 +02:00
|
|
|
"fmt"
|
2022-08-18 10:11:13 +02:00
|
|
|
"io"
|
2022-08-18 08:52:09 +02:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"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"
|
|
|
|
)
|
|
|
|
|
|
|
|
type localStorage struct {
|
|
|
|
*storage.StorageBackend
|
|
|
|
latestSymlink string
|
|
|
|
}
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
// Config allows configuration of a local storage backend.
|
|
|
|
type Config struct {
|
|
|
|
ArchivePath string
|
|
|
|
LatestSymlink string
|
|
|
|
}
|
|
|
|
|
2022-08-18 08:52:09 +02:00
|
|
|
// NewStorageBackend creates and initializes a new local storage backend.
|
2022-08-18 10:11:13 +02:00
|
|
|
func NewStorageBackend(opts Config, logFunc storage.Log) storage.Backend {
|
2022-08-18 08:52:09 +02:00
|
|
|
return &localStorage{
|
|
|
|
StorageBackend: &storage.StorageBackend{
|
2022-08-18 10:11:13 +02:00
|
|
|
DestinationPath: opts.ArchivePath,
|
2022-08-18 08:52:09 +02:00
|
|
|
Log: logFunc,
|
|
|
|
},
|
2022-08-18 10:11:13 +02:00
|
|
|
latestSymlink: opts.LatestSymlink,
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Name return the name of the storage backend
|
|
|
|
func (b *localStorage) Name() string {
|
|
|
|
return "Local"
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy copies the given file to the local storage backend.
|
|
|
|
func (b *localStorage) Copy(file string) error {
|
|
|
|
_, name := path.Split(file)
|
|
|
|
|
2022-08-18 10:11:13 +02:00
|
|
|
if err := copyFile(file, path.Join(b.DestinationPath, name)); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error copying file to archive")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
2023-08-25 12:44:43 +02:00
|
|
|
b.Log(storage.LogLevelInfo, b.Name(), "Stored copy of backup `%s` in `%s`.", file, b.DestinationPath)
|
2022-08-18 08:52:09 +02:00
|
|
|
|
|
|
|
if b.latestSymlink != "" {
|
|
|
|
symlink := path.Join(b.DestinationPath, b.latestSymlink)
|
|
|
|
if _, err := os.Lstat(symlink); err == nil {
|
|
|
|
os.Remove(symlink)
|
|
|
|
}
|
|
|
|
if err := os.Symlink(name, symlink); err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(err, "error creating latest symlink")
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
2022-08-18 10:11:13 +02:00
|
|
|
b.Log(storage.LogLevelInfo, b.Name(), "Created/Updated symlink `%s` for latest backup.", b.latestSymlink)
|
2022-08-18 08:52:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Prune rotates away backups according to the configuration and provided deadline for the local storage backend.
|
|
|
|
func (b *localStorage) Prune(deadline time.Time, pruningPrefix string) (*storage.PruneStats, error) {
|
|
|
|
globPattern := path.Join(
|
|
|
|
b.DestinationPath,
|
|
|
|
fmt.Sprintf("%s*", pruningPrefix),
|
|
|
|
)
|
|
|
|
globMatches, err := filepath.Glob(globPattern)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(
|
2022-08-18 08:52:09 +02:00
|
|
|
err,
|
2024-02-16 15:35:42 +01:00
|
|
|
fmt.Sprintf(
|
|
|
|
"error looking up matching files using pattern %s",
|
|
|
|
globPattern,
|
|
|
|
),
|
2022-08-18 08:52:09 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var candidates []string
|
|
|
|
for _, candidate := range globMatches {
|
|
|
|
fi, err := os.Lstat(candidate)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(
|
2022-08-18 08:52:09 +02:00
|
|
|
err,
|
2024-02-16 15:35:42 +01:00
|
|
|
fmt.Sprintf(
|
|
|
|
"error calling Lstat on file %s",
|
|
|
|
candidate,
|
|
|
|
),
|
2022-08-18 08:52:09 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.Mode()&os.ModeSymlink != os.ModeSymlink {
|
|
|
|
candidates = append(candidates, candidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var matches []string
|
|
|
|
for _, candidate := range candidates {
|
|
|
|
fi, err := os.Stat(candidate)
|
|
|
|
if err != nil {
|
2024-02-16 15:35:42 +01:00
|
|
|
return nil, errwrap.Wrap(
|
2022-08-18 08:52:09 +02:00
|
|
|
err,
|
2024-02-16 15:35:42 +01:00
|
|
|
fmt.Sprintf(
|
|
|
|
"error calling stat on file %s",
|
|
|
|
candidate,
|
|
|
|
),
|
2022-08-18 08:52:09 +02:00
|
|
|
)
|
|
|
|
}
|
|
|
|
if fi.ModTime().Before(deadline) {
|
|
|
|
matches = append(matches, candidate)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
var removeErrors []error
|
|
|
|
for _, match := range matches {
|
|
|
|
if err := os.Remove(match); err != nil {
|
|
|
|
removeErrors = append(removeErrors, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(removeErrors) != 0 {
|
2024-02-16 15:35:42 +01:00
|
|
|
return errwrap.Wrap(
|
2023-02-02 21:07:25 +01:00
|
|
|
errors.Join(removeErrors...),
|
2024-02-16 15:35:42 +01:00
|
|
|
fmt.Sprintf(
|
|
|
|
"%d error(s) deleting files",
|
|
|
|
len(removeErrors),
|
|
|
|
),
|
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
|
|
|
}
|
2022-08-18 10:11:13 +02:00
|
|
|
|
|
|
|
// copy creates a copy of the file located at `dst` at `src`.
|
|
|
|
func copyFile(src, dst string) error {
|
|
|
|
in, err := os.Open(src)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer in.Close()
|
|
|
|
|
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = io.Copy(out, in)
|
|
|
|
if err != nil {
|
|
|
|
out.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return out.Close()
|
|
|
|
}
|