Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
[![Docker](https://img.shields.io/badge/docker_run-ghcr.io%2Fsmashedr%2Fbup-blue?style=flat-square&logo=docker)](#docker)
[![GitHub](https://img.shields.io/badge/curl_--L-i.jpillora.com%2Fsmashedr%2Fbup!|bash-blue?style=flat-square&logo=github)](#github)

<a title="BackUP" href="https://smashedr.github.io/bup" target="_blank">
<img alt="BackUP" align="right" width="128" height="auto" src="https://raw.githubusercontent.com/smashedr/bup/refs/heads/master/docs/assets/images/logo.png"></a>

- [Install](#install)
- [Usage](#usage)
- [Development](#development)
Expand Down
27 changes: 22 additions & 5 deletions cmd/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ import (
"time"
)

//var (
// port int
//)

//func validateDirectory(path, name string) error {
// info, err := os.Stat(path)
// if err != nil {
Expand All @@ -31,6 +27,19 @@ import (
// return nil
//}

func formatBytes(bytes int64) string {
const unit = 1024
if bytes < unit {
return fmt.Sprintf("%d B", bytes)
}
div, exp := int64(unit), 0
for n := bytes / unit; n >= unit; n /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(bytes)/float64(div), "KMGTPE"[exp])
}

func createZipArchive(excludes []string, source, destination string) error {
// Create timestamp filename
timestamp := time.Now().Format("06-01-02-15-04-05") // YY-MM-DD-HH-MM-SS
Expand All @@ -45,7 +54,7 @@ func createZipArchive(excludes []string, source, destination string) error {

// Create zip writer
zipWriter := zip.NewWriter(zipFile)
defer func() { _ = zipWriter.Close() }()
//defer func() { _ = zipWriter.Close() }()

// Get the base name of the source for proper relative paths
sourceBase := filepath.Base(source)
Expand Down Expand Up @@ -134,6 +143,14 @@ func createZipArchive(excludes []string, source, destination string) error {
return fmt.Errorf("failed to walk directory: %w", err)
}

_ = zipWriter.Close()
fileInfo, err := zipFile.Stat()
if err != nil {
fmt.Printf("Error getting archive info: %v", err)
} else {
fmt.Printf("Archive Size: %s\n", formatBytes(fileInfo.Size()))
}

fmt.Printf("Archive: %s\n", zipFilename)
return nil
}
Expand Down
10 changes: 0 additions & 10 deletions cmd/exclude.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,4 @@ var excludeCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(excludeCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// infoCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// infoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 0 additions & 10 deletions cmd/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,4 @@ var infoCmd = &cobra.Command{

func init() {
rootCmd.AddCommand(infoCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// infoCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// infoCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
10 changes: 0 additions & 10 deletions cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,4 @@ func listDir(path string) {

func init() {
rootCmd.AddCommand(listCmd)

// Here you will define your flags and configuration settings.

// Cobra supports Persistent Flags which will work for this command
// and all subcommands, e.g.:
// listCmd.PersistentFlags().String("foo", "", "A help for foo")

// Cobra supports local flags which will only run when this command
// is called directly, e.g.:
// listCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
}
23 changes: 14 additions & 9 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,14 @@ func initConfig() {

// Set default excluded directories
viper.SetDefault("excludes", []string{
".cache",
".*cache",
".venv",
"build",
"dist",
"node_modules",
"out",
"venv",
"*.exe",
})

if cfgFile != "" {
Expand All @@ -73,6 +75,7 @@ func initConfig() {
fmt.Printf("Unable to read config file: %s\n\n", viper.ConfigFileUsed())
os.Exit(1)
}
fmt.Printf("Config File: %s\n", cfgFile)
} else {
// Find Config
viper.SetConfigType("yaml")
Expand All @@ -89,22 +92,24 @@ func initConfig() {
//viper.ReadInConfig()

if err := viper.ReadInConfig(); err != nil {
home, _ := os.UserHomeDir()
//fmt.Printf("home: %s\n", home)
configPath := filepath.Join(home, ".config")
homeDir, err := os.UserHomeDir()
if err != nil {
homeDir = "."
}
fmt.Printf("homeDir: %s\n", homeDir)
configPath := filepath.Join(homeDir, ".config")
//fmt.Printf("configPath: %s\n", configPath)
_ = os.MkdirAll(configPath, 0755)
configFile := filepath.Join(configPath, "bup.yaml")
//fmt.Printf("configFile: %s\n", configFile)

// Set a specific file to create
viper.SetConfigFile(configFile)
_ = viper.SafeWriteConfigAs(configFile)
if err := viper.ReadInConfig(); err != nil {
fmt.Printf("Error reading config: %s\n", configFile)
return
fmt.Printf("Error reading config: %s\nUsing Default Config!", configFile)
}
fmt.Printf("Config File: %s\n", configFile)
} else {
fmt.Printf("Config File: %s\n", viper.ConfigFileUsed())
}
}
fmt.Printf("Config File: %s\n", viper.ConfigFileUsed())
}