Compare commits

..

No commits in common. "main" and "v0.0.4" have entirely different histories.
main ... v0.0.4

6 changed files with 26 additions and 105 deletions

View File

@ -1,58 +1,2 @@
# backuper
## Usage
### Incremental backup
```sh
backuper i <config file path>
```
### Full backup
```sh
backuper f <config file path>
```
### Search files in backup
```sh
backuper s <config file path> <mask>
```
### Recover files from backup
```sh
backuper r <config file path> <mask> <files datetime> <path to recover>
```
Examples:
```sh
# Recover Go files relevant as of 01.01.2023 to /home/user/go directory
backuper r config.conf "*.go" "01.01.2023" "/home/user/go"
```
### Test backup for errors
```sh
backuper t <config file path>
```
## Basic config example
Backup config files from `/etc` and sqlite files from `/var`:
```toml
FileName = "backup"
[[Patterns]]
Path = "/etc"
FileNamePatternList = ["*.conf", "*.toml", "*.ini", "*.yaml"]
Recursive = true
[[Patterns]]
Path = "/var"
FileNamePatternList = ["*.sqlite"]
Recursive = true
```

View File

@ -46,8 +46,6 @@ func (b *Config) fileList(fileNames chan FileInfo) {
if b.StopOnAnyError {
return fmt.Errorf("get file info error: %v", err)
}
return nil
}
file := FileInfo{

View File

@ -62,8 +62,7 @@ func (index Index) Save(fileName string) error {
enc, err := zstd.NewWriter(f, zstd.WithEncoderLevel(zstd.SpeedBestCompression))
if err != nil {
f.Close()
os.Remove(fileName)
f.Close() // TODO: удалить частичный файл?
return err
}
@ -83,8 +82,7 @@ func (index Index) Save(fileName string) error {
err := csvWriter.Write([]string{fileName, historyItem.ArchiveFileName, strconv.Itoa(int(historyItem.ModificationTime.Unix()))})
if err != nil {
enc.Close()
f.Close()
os.Remove(fileName)
f.Close() // TODO: удалить частичный файл?
return err
}
}
@ -93,15 +91,13 @@ func (index Index) Save(fileName string) error {
csvWriter.Flush()
if err := csvWriter.Error(); err != nil {
enc.Close()
f.Close()
os.Remove(fileName)
f.Close() // TODO: удалить частичный файл?
return err
}
err = enc.Close()
if err != nil {
f.Close()
os.Remove(fileName)
f.Close() // TODO: удалить частичный файл?
return err
}

21
main.go
View File

@ -4,6 +4,7 @@ import (
"log"
"os"
"path/filepath"
"time"
)
func init() {
@ -55,9 +56,25 @@ func main() {
log.Fatalln(err)
}
t, err := parseTime(os.Args[4])
var t time.Time
switch len(os.Args[4]) {
case len("02.01.2006"):
t, err = time.Parse("02.01.2006 15:04", os.Args[4])
if err != nil {
config.fatalln(err)
config.fatalln("time parse error:", err)
}
case len("02.01.2006 15:04"):
t, err = time.Parse("02.01.2006 15:04", os.Args[4])
if err != nil {
config.fatalln("time parse error:", err)
}
case len("02.01.2006 15:04:05"):
t, err = time.Parse("02.01.2006 15:04:05", os.Args[4])
if err != nil {
config.fatalln("time parse error:", err)
}
default:
config.fatalln(`wrong time format, must be ["DD.MM.YYYY", "DD.MM.YYYY hh:mm", "DD.MM.YYYY hh:mm:ss"]`)
}
plan, err := config.extractionPlan(os.Args[3], t)

View File

@ -1,11 +1,9 @@
package main
import (
"errors"
"fmt"
"path/filepath"
"strings"
"time"
"github.com/tidwall/match"
)
@ -34,7 +32,7 @@ func sizeToApproxHuman(s int64) string {
return fmt.Sprintf("%d B", s)
}
// clean убирает невозможные комбинации символов из пути
// clean убирает невозможнын комбинации символов из пути
func clean(s string) string {
s = strings.ReplaceAll(s, ":", "")
s = strings.ReplaceAll(s, `\\`, `\`)
@ -73,16 +71,3 @@ func isFilePathMatchPatterns(patterns []string, fileName string) bool {
return false
}
func parseTime(s string) (time.Time, error) {
switch len(s) {
case len("02.01.2006"):
return time.ParseInLocation("02.01.2006", s, time.Local)
case len("02.01.2006 15:04"):
return time.ParseInLocation("02.01.2006 15:04", s, time.Local)
case len("02.01.2006 15:04:05"):
return time.ParseInLocation("02.01.2006 15:04:05", s, time.Local)
}
return time.Time{}, errors.New("unknown time format")
}

View File

@ -2,7 +2,6 @@ package main
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
)
@ -11,21 +10,3 @@ func TestSizeToApproxHuman(t *testing.T) {
assert.Equal(t, "1.0 KiB", sizeToApproxHuman(1024))
assert.Equal(t, "1.1 KiB", sizeToApproxHuman(1126))
}
func TestParseTime(t *testing.T) {
tests := []struct {
input string
expected time.Time
}{
{"02.01.2006", time.Date(2006, 01, 02, 0, 0, 0, 0, time.Local)},
{"02.01.2006 15:04", time.Date(2006, 01, 02, 15, 4, 0, 0, time.Local)},
{"02.01.2006 15:04:05", time.Date(2006, 01, 02, 15, 4, 5, 0, time.Local)},
}
for _, test := range tests {
got, err := parseTime(test.input)
assert.NoError(t, err)
assert.Equal(t, test.expected, got)
}
}