mirror of
https://github.com/offen/docker-volume-backup.git
synced 2024-11-24 22:20:28 +01:00
Drop logrus dependency, log using slog package from stdlib (#247)
This commit is contained in:
parent
a93ff6fe09
commit
67d978f515
@ -160,7 +160,7 @@ func (s *script) runLabeledCommands(label string) error {
|
|||||||
userLabelName := fmt.Sprintf("%s.user", label)
|
userLabelName := fmt.Sprintf("%s.user", label)
|
||||||
user := c.Labels[userLabelName]
|
user := c.Labels[userLabelName]
|
||||||
|
|
||||||
s.logger.Infof("Running %s command %s for container %s", label, cmd, strings.TrimPrefix(c.Names[0], "/"))
|
s.logger.Info(fmt.Sprintf("Running %s command %s for container %s", label, cmd, strings.TrimPrefix(c.Names[0], "/")))
|
||||||
stdout, stderr, err := s.exec(c.ID, cmd, user)
|
stdout, stderr, err := s.exec(c.ID, cmd, user)
|
||||||
if s.c.ExecForwardOutput {
|
if s.c.ExecForwardOutput {
|
||||||
os.Stderr.Write(stderr)
|
os.Stderr.Write(stderr)
|
||||||
|
@ -41,9 +41,11 @@ func (s *script) lock(lockfile string) (func() error, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if !s.encounteredLock {
|
if !s.encounteredLock {
|
||||||
s.logger.Infof(
|
s.logger.Info(
|
||||||
"Exclusive lock was not available on first attempt. Will retry until it becomes available or the timeout of %s is exceeded.",
|
fmt.Sprintf(
|
||||||
s.c.LockTimeout,
|
"Exclusive lock was not available on first attempt. Will retry until it becomes available or the timeout of %s is exceeded.",
|
||||||
|
s.c.LockTimeout,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
s.encounteredLock = true
|
s.encounteredLock = true
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,7 +22,9 @@ func main() {
|
|||||||
if pArg := recover(); pArg != nil {
|
if pArg := recover(); pArg != nil {
|
||||||
if err, ok := pArg.(error); ok {
|
if err, ok := pArg.(error); ok {
|
||||||
if hookErr := s.runHooks(err); hookErr != nil {
|
if hookErr := s.runHooks(err); hookErr != nil {
|
||||||
s.logger.Errorf("An error occurred calling the registered hooks: %s", hookErr)
|
s.logger.Error(
|
||||||
|
fmt.Sprintf("An error occurred calling the registered hooks: %s", hookErr),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
@ -29,9 +32,12 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if err := s.runHooks(nil); err != nil {
|
if err := s.runHooks(nil); err != nil {
|
||||||
s.logger.Errorf(
|
s.logger.Error(
|
||||||
"Backup procedure ran successfully, but an error ocurred calling the registered hooks: %v",
|
fmt.Sprintf(
|
||||||
err,
|
|
||||||
|
"Backup procedure ran successfully, but an error ocurred calling the registered hooks: %v",
|
||||||
|
err,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"io/fs"
|
"io/fs"
|
||||||
|
"log/slog"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -32,7 +33,6 @@ import (
|
|||||||
"github.com/kelseyhightower/envconfig"
|
"github.com/kelseyhightower/envconfig"
|
||||||
"github.com/leekchan/timeutil"
|
"github.com/leekchan/timeutil"
|
||||||
"github.com/otiai10/copy"
|
"github.com/otiai10/copy"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
"golang.org/x/crypto/openpgp"
|
"golang.org/x/crypto/openpgp"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
)
|
)
|
||||||
@ -42,7 +42,7 @@ import (
|
|||||||
type script struct {
|
type script struct {
|
||||||
cli *client.Client
|
cli *client.Client
|
||||||
storages []storage.Backend
|
storages []storage.Backend
|
||||||
logger *logrus.Logger
|
logger *slog.Logger
|
||||||
sender *router.ServiceRouter
|
sender *router.ServiceRouter
|
||||||
template *template.Template
|
template *template.Template
|
||||||
hooks []hook
|
hooks []hook
|
||||||
@ -63,13 +63,8 @@ type script struct {
|
|||||||
func newScript() (*script, error) {
|
func newScript() (*script, error) {
|
||||||
stdOut, logBuffer := buffer(os.Stdout)
|
stdOut, logBuffer := buffer(os.Stdout)
|
||||||
s := &script{
|
s := &script{
|
||||||
c: &Config{},
|
c: &Config{},
|
||||||
logger: &logrus.Logger{
|
logger: slog.New(slog.NewTextHandler(stdOut, nil)),
|
||||||
Out: stdOut,
|
|
||||||
Formatter: new(logrus.TextFormatter),
|
|
||||||
Hooks: make(logrus.LevelHooks),
|
|
||||||
Level: logrus.InfoLevel,
|
|
||||||
},
|
|
||||||
stats: &Stats{
|
stats: &Stats{
|
||||||
StartTime: time.Now(),
|
StartTime: time.Now(),
|
||||||
LogOutput: logBuffer,
|
LogOutput: logBuffer,
|
||||||
@ -114,12 +109,12 @@ func newScript() (*script, error) {
|
|||||||
logFunc := func(logType storage.LogLevel, context string, msg string, params ...any) {
|
logFunc := func(logType storage.LogLevel, context string, msg string, params ...any) {
|
||||||
switch logType {
|
switch logType {
|
||||||
case storage.LogLevelWarning:
|
case storage.LogLevelWarning:
|
||||||
s.logger.Warnf("["+context+"] "+msg, params...)
|
s.logger.Warn(fmt.Sprintf("["+context+"] "+msg, params...))
|
||||||
case storage.LogLevelError:
|
case storage.LogLevelError:
|
||||||
s.logger.Errorf("["+context+"] "+msg, params...)
|
s.logger.Error(fmt.Sprintf("["+context+"] "+msg, params...))
|
||||||
case storage.LogLevelInfo:
|
case storage.LogLevelInfo:
|
||||||
default:
|
default:
|
||||||
s.logger.Infof("["+context+"] "+msg, params...)
|
s.logger.Info(fmt.Sprintf("["+context+"] "+msg, params...))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -306,11 +301,13 @@ func (s *script) stopContainers() (func() error, error) {
|
|||||||
return noop, nil
|
return noop, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger.Infof(
|
s.logger.Info(
|
||||||
"Stopping %d container(s) labeled `%s` out of %d running container(s).",
|
fmt.Sprintf(
|
||||||
len(containersToStop),
|
"Stopping %d container(s) labeled `%s` out of %d running container(s).",
|
||||||
containerLabel,
|
len(containersToStop),
|
||||||
len(allContainers),
|
containerLabel,
|
||||||
|
len(allContainers),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
var stoppedContainers []types.Container
|
var stoppedContainers []types.Container
|
||||||
@ -382,9 +379,11 @@ func (s *script) stopContainers() (func() error, error) {
|
|||||||
errors.Join(restartErrors...),
|
errors.Join(restartErrors...),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
s.logger.Infof(
|
s.logger.Info(
|
||||||
"Restarted %d container(s) and the matching service(s).",
|
fmt.Sprintf(
|
||||||
len(stoppedContainers),
|
"Restarted %d container(s) and the matching service(s).",
|
||||||
|
len(stoppedContainers),
|
||||||
|
),
|
||||||
)
|
)
|
||||||
return nil
|
return nil
|
||||||
}, stopError
|
}, stopError
|
||||||
@ -408,7 +407,9 @@ func (s *script) createArchive() error {
|
|||||||
if err := remove(backupSources); err != nil {
|
if err := remove(backupSources); err != nil {
|
||||||
return fmt.Errorf("createArchive: error removing snapshot: %w", err)
|
return fmt.Errorf("createArchive: error removing snapshot: %w", err)
|
||||||
}
|
}
|
||||||
s.logger.Infof("Removed snapshot `%s`.", backupSources)
|
s.logger.Info(
|
||||||
|
fmt.Sprintf("Removed snapshot `%s`.", backupSources),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
if err := copy.Copy(s.c.BackupSources, backupSources, copy.Options{
|
if err := copy.Copy(s.c.BackupSources, backupSources, copy.Options{
|
||||||
@ -417,7 +418,9 @@ func (s *script) createArchive() error {
|
|||||||
}); err != nil {
|
}); err != nil {
|
||||||
return fmt.Errorf("createArchive: error creating snapshot: %w", err)
|
return fmt.Errorf("createArchive: error creating snapshot: %w", err)
|
||||||
}
|
}
|
||||||
s.logger.Infof("Created snapshot of `%s` at `%s`.", s.c.BackupSources, backupSources)
|
s.logger.Info(
|
||||||
|
fmt.Sprintf("Created snapshot of `%s` at `%s`.", s.c.BackupSources, backupSources),
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
tarFile := s.file
|
tarFile := s.file
|
||||||
@ -425,7 +428,9 @@ func (s *script) createArchive() error {
|
|||||||
if err := remove(tarFile); err != nil {
|
if err := remove(tarFile); err != nil {
|
||||||
return fmt.Errorf("createArchive: error removing tar file: %w", err)
|
return fmt.Errorf("createArchive: error removing tar file: %w", err)
|
||||||
}
|
}
|
||||||
s.logger.Infof("Removed tar file `%s`.", tarFile)
|
s.logger.Info(
|
||||||
|
fmt.Sprintf("Removed tar file `%s`.", tarFile),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -453,7 +458,9 @@ func (s *script) createArchive() error {
|
|||||||
return fmt.Errorf("createArchive: error compressing backup folder: %w", err)
|
return fmt.Errorf("createArchive: error compressing backup folder: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
s.logger.Infof("Created backup of `%s` at `%s`.", backupSources, tarFile)
|
s.logger.Info(
|
||||||
|
fmt.Sprintf("Created backup of `%s` at `%s`.", backupSources, tarFile),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -470,7 +477,9 @@ func (s *script) encryptArchive() error {
|
|||||||
if err := remove(gpgFile); err != nil {
|
if err := remove(gpgFile); err != nil {
|
||||||
return fmt.Errorf("encryptArchive: error removing gpg file: %w", err)
|
return fmt.Errorf("encryptArchive: error removing gpg file: %w", err)
|
||||||
}
|
}
|
||||||
s.logger.Infof("Removed GPG file `%s`.", gpgFile)
|
s.logger.Info(
|
||||||
|
fmt.Sprintf("Removed GPG file `%s`.", gpgFile),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
})
|
})
|
||||||
|
|
||||||
@ -500,7 +509,9 @@ func (s *script) encryptArchive() error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
s.file = gpgFile
|
s.file = gpgFile
|
||||||
s.logger.Infof("Encrypted backup using given passphrase, saving as `%s`.", s.file)
|
s.logger.Info(
|
||||||
|
fmt.Sprintf("Encrypted backup using given passphrase, saving as `%s`.", s.file),
|
||||||
|
)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -572,7 +583,9 @@ func (s *script) pruneBackups() error {
|
|||||||
// is non-nil.
|
// is non-nil.
|
||||||
func (s *script) must(err error) {
|
func (s *script) must(err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
s.logger.Errorf("Fatal error running backup: %s", err)
|
s.logger.Error(
|
||||||
|
fmt.Sprintf("Fatal error running backup: %s", err),
|
||||||
|
)
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
2
go.mod
2
go.mod
@ -14,7 +14,6 @@ require (
|
|||||||
github.com/minio/minio-go/v7 v7.0.61
|
github.com/minio/minio-go/v7 v7.0.61
|
||||||
github.com/otiai10/copy v1.11.0
|
github.com/otiai10/copy v1.11.0
|
||||||
github.com/pkg/sftp v1.13.5
|
github.com/pkg/sftp v1.13.5
|
||||||
github.com/sirupsen/logrus v1.9.3
|
|
||||||
github.com/studio-b12/gowebdav v0.9.0
|
github.com/studio-b12/gowebdav v0.9.0
|
||||||
golang.org/x/crypto v0.11.0
|
golang.org/x/crypto v0.11.0
|
||||||
golang.org/x/sync v0.3.0
|
golang.org/x/sync v0.3.0
|
||||||
@ -52,6 +51,7 @@ require (
|
|||||||
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
|
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
|
||||||
github.com/pkg/errors v0.9.1 // indirect
|
github.com/pkg/errors v0.9.1 // indirect
|
||||||
github.com/rs/xid v1.5.0 // indirect
|
github.com/rs/xid v1.5.0 // indirect
|
||||||
|
github.com/sirupsen/logrus v1.9.3 // indirect
|
||||||
golang.org/x/net v0.12.0 // indirect
|
golang.org/x/net v0.12.0 // indirect
|
||||||
golang.org/x/sys v0.10.0 // indirect
|
golang.org/x/sys v0.10.0 // indirect
|
||||||
golang.org/x/text v0.11.0 // indirect
|
golang.org/x/text v0.11.0 // indirect
|
||||||
|
Loading…
Reference in New Issue
Block a user