Skip to content

Commit

Permalink
fix errors
Browse files Browse the repository at this point in the history
  • Loading branch information
hibare committed Sep 2, 2024
1 parent 6da19cd commit ddd6f4b
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 86 deletions.
6 changes: 2 additions & 4 deletions cmd/backup/backup.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package backup

import (
"errors"
"log/slog"

log "github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -12,9 +11,8 @@ var BackupCmd = &cobra.Command{
Short: "Perform backups & related operations",
Long: "",
Run: func(cmd *cobra.Command, args []string) {
log.Error().Err(errors.New("test error")).Msg("Printing help")
if err := cmd.Help(); err != nil {
log.Error().Err(err).Msg("error printing help")
slog.Error("error printing help")
}
},
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/config/clean.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package config

import (
"log/slog"

"github.com/hibare/GoS3Backup/internal/config"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -11,7 +12,7 @@ var CleanConfigCmd = &cobra.Command{
Short: "Command to clean up program config",
Run: func(cmd *cobra.Command, args []string) {
if err := config.CleanConfig(); err != nil {
log.Error().Err(err).Msg("error cleaning config")
slog.Error("error cleaning config", "error", err)
}
},
}
14 changes: 7 additions & 7 deletions cmd/root.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package cmd

import (
"log/slog"
"os"
"time"

Expand All @@ -12,7 +13,6 @@ import (
"github.com/hibare/GoS3Backup/internal/config"
"github.com/hibare/GoS3Backup/internal/constants"
"github.com/hibare/GoS3Backup/internal/version"
"github.com/rs/zerolog/log"
"github.com/spf13/cobra"
)

Expand All @@ -30,15 +30,15 @@ var rootCmd = &cobra.Command{
intBackup.Backup()
intBackup.PurgeOldBackups()
}); err != nil {
log.Error().Err(err).Msg("Error setting up cron")
slog.Error("Error setting up cron")
}
log.Info().Msgf("Scheduled backup job to run every %s", config.Current.Backup.Cron)
slog.Info("Scheduled backup job", "cron", config.Current.Backup.Cron)

// Schedule version check job
if _, err := s.Cron(constants.VersioCheckCron).Do(func() {
if _, err := s.Cron(constants.VersionCheckCron).Do(func() {
version.V.CheckUpdate()
}); err != nil {
log.Warn().Err(err).Msg("Failed to schedule version check job")
slog.Warn("Failed to schedule version check job")
}

s.StartBlocking()
Expand All @@ -56,12 +56,12 @@ func init() {
rootCmd.AddCommand(configCmd.ConfigCmd)
rootCmd.AddCommand(backup.BackupCmd)

cobra.OnInitialize(commonLogger.InitLogger, config.LoadConfig)
cobra.OnInitialize(commonLogger.InitDefaultLogger, config.LoadConfig)

initialVersionCheck := func() {
version.V.CheckUpdate()
if version.V.NewVersionAvailable {
log.Info().Msg(version.V.GetUpdateNotification())
slog.Info(version.V.GetUpdateNotification())
}
}
go initialVersionCheck()
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ require (
github.com/go-co-op/gocron v1.37.0
github.com/hibare/GoCommon/v2 v2.13.0
github.com/jedib0t/go-pretty/v6 v6.5.9
github.com/rs/zerolog v1.33.0
github.com/spf13/cobra v1.8.1
)

Expand All @@ -20,8 +19,6 @@ require (
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
Expand Down
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ github.com/aws/aws-sdk-go v1.55.3 h1:0B5hOX+mIx7I5XPOrjrHlKSDQV/+ypFZpIHOx5LOk3E
github.com/aws/aws-sdk-go v1.55.3/go.mod h1:eRwEWoyTWFMVYVQzKMNHWP5/RV4xIUGMQfXQHfHkpNU=
github.com/cloudflare/circl v1.3.7 h1:qlCDlTPz2n9fu58M0Nh1J/JzcFpfgkFHHX3O35r5vcU=
github.com/cloudflare/circl v1.3.7/go.mod h1:sRTcRWXGLrKw6yIGJ+l7amYJFfAXbZG0kBSc8r4zxgA=
github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand All @@ -17,7 +16,6 @@ github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nos
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/go-co-op/gocron v1.37.0 h1:ZYDJGtQ4OMhTLKOKMIch+/CY70Brbb1dGdooLEhh7b0=
github.com/go-co-op/gocron v1.37.0/go.mod h1:3L/n6BkO7ABj+TrfSVXLRzsP26zmikL4ISkLQ0O8iNY=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
Expand Down Expand Up @@ -45,19 +43,13 @@ github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM=
github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs=
github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U=
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
Expand All @@ -69,9 +61,6 @@ github.com/rogpeppe/go-internal v1.6.1/go.mod h1:xXDCJY+GAPziupqXw64V24skbSoqbTE
github.com/rogpeppe/go-internal v1.8.1/go.mod h1:JeRgkft04UBgHMgCIwADu4Pn6Mtm5d4nPKWu0nJ5d+o=
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/rs/zerolog v1.33.0 h1:1cU2KZkvPxNyfgEmhHAz/1A9Bz+llsdYzklWFzgp0r8=
github.com/rs/zerolog v1.33.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6keLGt6kNQ=
github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4=
Expand Down Expand Up @@ -110,9 +99,6 @@ golang.org/x/crypto v0.21.0 h1:X31++rzVUdKhX5sWmSOFZxx8UW/ldWx55cbf08iNAMA=
golang.org/x/crypto v0.21.0/go.mod h1:0BP7YvVV9gBbVKyeTG0Gyn+gZm94bibOW5BjDEYAOMs=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 h1:GoHiUyI/Tp2nVkLI2mCxVkOjsbSXD66ic0XW0js0R9g=
golang.org/x/exp v0.0.0-20230905200255-921286631fa9/go.mod h1:S2oDrQGGwySpoQPVqRShND87VCbxmc6bL1Yd2oYrm6k=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
Expand Down
59 changes: 30 additions & 29 deletions internal/backup/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"path/filepath"

"github.com/rs/zerolog/log"
"log/slog"

commonGPG "github.com/hibare/GoCommon/v2/pkg/crypto/gpg"
commonDateTimes "github.com/hibare/GoCommon/v2/pkg/datetime"
Expand Down Expand Up @@ -33,80 +33,81 @@ func Backup() {
s3.SetPrefix(config.Current.S3.Prefix, config.Current.Backup.Hostname, true)

if err := s3.NewSession(); err != nil {
log.Fatal().Err(err).Msg("Error creating session")
slog.Error("Error creating session", "error", err)
os.Exit(1)
return
}

// Loop through individual backup dir & perform backup
for _, dir := range config.Current.Backup.Dirs {
log.Info().Msgf("Processing path %s", dir)
slog.Info("Processing path", "path", dir)

if config.Current.Backup.ArchiveDirs {
log.Info().Msgf("Archiving dir %s", dir)
slog.Info("Archiving dir", "dir", dir)
archivePath, totalFiles, totalDirs, successFiles, err := commonFiles.ArchiveDir(dir, nil)
if err != nil {
log.Error().Err(err).Msgf("Archiving failed %s", dir)
slog.Error("Error archiving", "error", err)
notifiers.NotifyBackupFailure(dir, totalDirs, totalFiles, err)
continue
}

if successFiles <= 0 {
log.Error().Err(err).Msgf("Uploading failed %s", dir)
slog.Error("No processable files", "dir", dir)
notifiers.NotifyBackupFailure(dir, totalDirs, totalFiles, ErrNoProcessableFiles)
continue
}
log.Info().Msgf("Archived files %d/%d, archive path %s", successFiles, totalFiles, archivePath)
slog.Info("Archived files", "successFiles", successFiles, "totalFiles", totalFiles, "archivePath", archivePath)

uploadPath := archivePath

if config.Current.Backup.Encryption.Enabled {
log.Info().Msgf("Encrypting archive %s", archivePath)
slog.Info("Encrypting archive", "archivePath", archivePath)
gpg, err := commonGPG.DownloadGPGPubKey(config.Current.Backup.Encryption.GPG.KeyID, config.Current.Backup.Encryption.GPG.KeyServer)
if err != nil {
log.Error().Err(err).Msg("Error downloading gpg key")
slog.Error("Error downloading gpg key", "error", err)
notifiers.NotifyBackupFailure(dir, totalDirs, totalFiles, err)
continue
}

encryptedFilePath, err := gpg.EncryptFile(archivePath)
if err != nil {
log.Error().Err(err).Msg("Error encrypting file")
slog.Error("Error encrypting file", "error", err)
notifiers.NotifyBackupFailure(dir, totalDirs, totalFiles, err)
continue
}

uploadPath = encryptedFilePath
log.Info().Msgf("Archive encrypted at %s", encryptedFilePath)
slog.Info("Encrypted archive", "uploadPath", uploadPath)
os.Remove(archivePath)
}

log.Info().Msgf("Uploading file %s", uploadPath)
slog.Info("Uploading file", "uploadPath", uploadPath)
key, err := s3.UploadFile(uploadPath)
if err != nil {
log.Error().Err(err).Msgf("Uploading failed %s", dir)
slog.Error("Uploading failed", "error", err)
notifiers.NotifyBackupFailure(dir, totalDirs, totalFiles, err)
continue
}

log.Info().Msgf("Uploaded files %d/%d at %s", successFiles, totalFiles, key)
slog.Info("Uploaded file", "key", key, "successFiles", successFiles, "totalFiles", totalFiles, "uploadPath", uploadPath)
notifiers.NotifyBackupSuccess(dir, totalDirs, totalFiles, successFiles, key)
os.Remove(uploadPath)
} else {
log.Info().Msgf("Uploading dir %s", dir)
slog.Info("Uploading dir", "dir", dir)
key, totalFiles, totalDirs, successFiles := s3.UploadDir(dir, nil)

if successFiles <= 0 {
log.Warn().Msgf("Uploading failed %s", dir)
slog.Warn("No processable files", "dir", dir)
notifiers.NotifyBackupFailure(dir, totalDirs, totalFiles, ErrNoProcessableFiles)
continue
}

log.Warn().Msgf("Uploaded files %d/%d at %s", successFiles, totalFiles, s3.Prefix)
slog.Warn("Uploaded files", "successFiles", successFiles, "totalFiles", totalFiles, "dir", dir)
notifiers.NotifyBackupSuccess(dir, totalDirs, totalFiles, successFiles, key)
}

}
log.Info().Msg("Backup job ran successfully")
slog.Info("Backup job ran successfully")
}

func ListBackups() ([]string, error) {
Expand All @@ -123,25 +124,25 @@ func ListBackups() ([]string, error) {
s3.SetPrefix(config.Current.S3.Prefix, config.Current.Backup.Hostname, false)

if err := s3.NewSession(); err != nil {
log.Error().Err(err).Msg("Error creating session")
slog.Error("Error creating session", "error", err)
return keys, err
}

log.Info().Msgf("prefix: %s", s3.Prefix)
slog.Info("prefix", "prefix", s3.Prefix)

// Retrieve objects by prefix
keys, err := s3.ListObjectsAtPrefixRoot()
if err != nil {
log.Error().Err(err).Msg("Error listing objects")
slog.Error("Error listing objects", "error", err)
return keys, err
}

if len(keys) == 0 {
log.Info().Msg("No backups found")
slog.Info("No backups found")
return keys, nil
}

log.Info().Msgf("Found %d backups", len(keys))
slog.Info("Found backups", "keys", len(keys))

// Remove prefix from key to get datetime string
keys = s3.TrimPrefix(keys)
Expand All @@ -163,7 +164,7 @@ func PurgeOldBackups() {
s3.SetPrefix(config.Current.S3.Prefix, config.Current.Backup.Hostname, false)

if err := s3.NewSession(); err != nil {
log.Error().Err(err).Msg("Error creating session")
slog.Error("Error creating session", "error", err)
}

backups, err := ListBackups()
Expand All @@ -173,24 +174,24 @@ func PurgeOldBackups() {
}

if len(backups) <= int(config.Current.Backup.RetentionCount) {
log.Info().Msg("No backups to delete")
slog.Info("No backups to delete")
return
}

keysToDelete := backups[config.Current.Backup.RetentionCount:]
log.Info().Msgf("Found %d backups to delete (backup rentention %d) [%s]", len(keysToDelete), config.Current.Backup.RetentionCount, keysToDelete)
slog.Info("Found backups to delete", "backups", len(keysToDelete), "retention", config.Current.Backup.RetentionCount, "keys", keysToDelete)

// Delete datetime keys from S3 exceding retention count
for _, key := range keysToDelete {
log.Info().Msgf("Deleting backup %s", key)
slog.Info("Deleting backup", "key", key)
key = filepath.Join(s3.Prefix, key)

if err := s3.DeleteObjects(key, true); err != nil {
log.Error().Err(err).Msgf("Error deleting backup %s", key)
slog.Error("Error deleting backup", "key", key, "error", err)
notifiers.NotifyBackupDeleteFailure(key, err)
continue
}
}

log.Info().Msg("Deletion completed successfully")
slog.Info("Deletion completed successfully")
}
Loading

0 comments on commit ddd6f4b

Please sign in to comment.