Added option to run local (rsync) or cloud (rclone) backups, in case you prefer a "full" copy with hardlinks via rsync, or latest/incremental snapshots via rclone

This commit is contained in:
2026-09-03 13:21:54 -04:00
parent fe6101a532
commit 0144cc2b4e
11 changed files with 1428 additions and 0 deletions
+25
View File
@@ -0,0 +1,25 @@
package cmd
import (
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var backupCmd = &cobra.Command{
Use: "backup",
Short: "Run an incremental backup job",
Long: `Run incremental backups to cloud targets (via Rclone) or local targets (via Rsync).`,
}
func init() {
rootCmd.AddCommand(backupCmd)
// Flags inherited by both 'cloud' and 'local' subcommands
backupCmd.PersistentFlags().StringP("src", "s", "", "source directory")
backupCmd.PersistentFlags().StringP("dst", "d", "", "destination directory")
backupCmd.PersistentFlags().Int("keep", 14, "number of incremental versions to retain")
_ = viper.BindPFlag("source", backupCmd.PersistentFlags().Lookup("src"))
_ = viper.BindPFlag("destination", backupCmd.PersistentFlags().Lookup("dst"))
_ = viper.BindPFlag("keep_incrementals", backupCmd.PersistentFlags().Lookup("keep"))
}
+33
View File
@@ -0,0 +1,33 @@
package cmd
import (
"fmt"
"git.metaunix.net/bitgoblin/goblin-store/lib/backup"
"git.metaunix.net/bitgoblin/goblin-store/lib/config"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var backupCloudCmd = &cobra.Command{
Use: "cloud",
Short: "Run cloud forward-incremental backup (via Rclone)",
Long: `Uploads changes to a remote or cloud destination using Rclone with forward-incremental backup retention.`,
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("configuration error: %w", err)
}
return backup.RunIncrementalBackup(cmd.Context(), cfg)
},
}
func init() {
backupCmd.AddCommand(backupCloudCmd)
// --workers is defined locally here so it only applies to cloud operations
backupCloudCmd.Flags().IntP("workers", "w", 4, "number of concurrent transfer threads")
_ = viper.BindPFlag("workers", backupCloudCmd.Flags().Lookup("workers"))
}
+27
View File
@@ -0,0 +1,27 @@
package cmd
import (
"fmt"
"git.metaunix.net/bitgoblin/goblin-store/lib/backup"
"git.metaunix.net/bitgoblin/goblin-store/lib/config"
"github.com/spf13/cobra"
)
var backupLocalCmd = &cobra.Command{
Use: "local",
Short: "Run local hardlinked snapshot backup (via Rsync)",
RunE: func(cmd *cobra.Command, args []string) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("configuration error: %w", err)
}
return backup.RunRsyncIncremental(cmd.Context(), cfg)
},
}
func init() {
backupCmd.AddCommand(backupLocalCmd)
}
+66
View File
@@ -0,0 +1,66 @@
package cmd
import (
"fmt"
"os"
"strings"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var (
cfgFile string
rootCmd = &cobra.Command{
Use: "goblin-store",
Short: "A fast, concurrent backup utility",
Long: `A multi-threaded incremental backup CLI supporting cloud and local targets.`,
}
)
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}
func init() {
cobra.OnInitialize(initConfig)
// Global flags available across all commands
rootCmd.PersistentFlags().StringVar(&cfgFile, "config", "", "config file (default is ~/.config/goblin-store.yaml)")
rootCmd.PersistentFlags().BoolP("verbose", "v", false, "enable verbose output")
_ = viper.BindPFlag("verbose", rootCmd.PersistentFlags().Lookup("verbose"))
}
func initConfig() {
if cfgFile != "" {
// Explicit config file passed via --config flag
viper.SetConfigFile(cfgFile)
} else {
// 1. Check OS user config directory (~/.config on Linux)
if configDir, err := os.UserConfigDir(); err == nil {
viper.AddConfigPath(configDir) // Searches ~/.config/goblin-store.yaml
}
// 2. Check current working directory as fallback
viper.AddConfigPath(".")
// File base name (without extension) and format
viper.SetConfigName("goblin-store")
viper.SetConfigType("yaml")
}
viper.SetEnvPrefix("GSBACKUP")
viper.SetEnvKeyReplacer(strings.NewReplacer("-", "_", ".", "_"))
viper.AutomaticEnv()
if err := viper.ReadInConfig(); err == nil {
if viper.GetBool("verbose") {
fmt.Printf("Using config file: %s\n", viper.ConfigFileUsed())
}
}
}