Skip to content

Commit 56135b5

Browse files
committed
application
0 parents  commit 56135b5

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2982
-0
lines changed

.github/workflows/go.yml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
name: Go
2+
on: [push]
3+
jobs:
4+
5+
build:
6+
name: Build
7+
runs-on: ubuntu-latest
8+
env:
9+
GO_ENV: test
10+
steps:
11+
12+
- name: Set up Go
13+
uses: actions/setup-go@v1
14+
with:
15+
go-version: 1.13
16+
id: go
17+
18+
- name: Check out code into the Go module directory
19+
uses: actions/checkout@v1
20+
21+
- name: Get dependencies
22+
run: |
23+
go get -v -t -d ./...
24+
25+
- name: Test
26+
run: go test ./...

.github/workflows/goreleaser.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: goreleaser
2+
3+
on:
4+
push:
5+
tags:
6+
- "*"
7+
jobs:
8+
goreleaser:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- name: Checkout
12+
uses: actions/checkout@v2
13+
with:
14+
fetch-depth: 0
15+
- name: Set up Go
16+
uses: actions/setup-go@v2
17+
with:
18+
go-version: 1.16
19+
- name: Run GoReleaser
20+
uses: goreleaser/goreleaser-action@v2
21+
with:
22+
version: latest
23+
args: release --rm-dist
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

.gitignore

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.dll
4+
*.so
5+
*.dylib
6+
7+
# Test binary, build with `go test -c`
8+
*.test
9+
10+
# Output of the go coverage tool, specifically when used with LiteIDE
11+
*.out
12+
13+
# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
14+
.glide/
15+
gobackup.yml
16+
gobackup
17+
build/
18+
dist/
19+
vendor/
20+
log/

DEVELOPMENT

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
## Install
2+
3+
- Install Go 1.16.x
4+
5+
```
6+
$ brew install goreleaser
7+
$ brew install dep
8+
```
9+
10+
## Get Source
11+
12+
```
13+
$ git clone https://github.com/4nkitd/gobackup.git
14+
$ cd gobackup
15+
$ go get
16+
```
17+
18+
## Test
19+
20+
```
21+
$ make test
22+
```
23+
24+
## Release new version
25+
26+
```
27+
$ git tag 0.7.3
28+
$ make release
29+
```

Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
test:
2+
GO_ENV=test go test ./...
3+
run:
4+
go run main.go -- perform -m demo -c ./gobackup_test.yml
5+
release:
6+
@rm -Rf dist/
7+
@goreleaser --skip-validate

README.md

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
# GoBackup is a fullstack backup tool
2+
design for web servers similar with [backup/backup](https://github.com/backup/backup), work with Crontab to backup automatically.
3+
4+
## Features
5+
6+
- No dependencies.
7+
- Multiple Databases source support.
8+
- Multiple Storage type support.
9+
- Archive paths or files into a tar.
10+
11+
## Current Support status
12+
13+
### Databases
14+
15+
- MySQL
16+
- PostgreSQL
17+
- Redis - `mode: sync/copy`
18+
- MongoDB
19+
20+
### Archive
21+
22+
Use `tar` command to archive many file or path into a `.tar` file.
23+
24+
### Compressor
25+
26+
- Tgz - `.tar.gz`
27+
- Uncompressed - `.tar`
28+
29+
### Encryptor
30+
31+
- OpenSSL - `aes-256-cbc` encrypt
32+
33+
### Storages
34+
35+
- Local
36+
- FTP
37+
- SCP - Upload via SSH copy
38+
- [Amazon S3](https://aws.amazon.com/s3)
39+
40+
## Install (macOS / Linux)
41+
42+
```bash
43+
$ curl -sSL https://git.io/gobackup | bash
44+
```
45+
46+
after that, you will get `/usr/local/bin/gobackup` command.
47+
48+
```bash
49+
$ gobackup -h
50+
NAME:
51+
gobackup - Easy full stack backup operations on UNIX-like systems
52+
53+
USAGE:
54+
gobackup [global options] command [command options] [arguments...]
55+
56+
VERSION:
57+
1.0.0
58+
59+
COMMANDS:
60+
perform
61+
help, h Shows a list of commands or help for one command
62+
63+
GLOBAL OPTIONS:
64+
--help, -h show help
65+
--version, -v print the version
66+
```
67+
68+
## Configuration
69+
70+
GoBackup will seek config files in:
71+
72+
- ~/.gobackup/gobackup.yml
73+
- /etc/gobackup/gobackup.yml
74+
75+
Example config: [gobackup_test.yml](https://github.com/4nkitd/gobackup/blob/master/gobackup_test.yml)
76+
77+
```yml
78+
# gobackup config example
79+
# -----------------------
80+
models:
81+
JobName:
82+
compress_with:
83+
type: tgz
84+
store_with:
85+
type: s3
86+
keep: 20
87+
bucket: gobackup-test
88+
region: ap-south-1
89+
path: backups
90+
access_key_id: Ohsgwk86h2ksas
91+
secret_access_key: Ojsiw729wujhKdhwsIIOw9173
92+
databases:
93+
DbName:
94+
type: mysql
95+
host: localhost
96+
port: 3306
97+
database: test
98+
username: root
99+
password: 123456
100+
```
101+
102+
## Backup Run
103+
104+
You may want run backup in scheduly, you need Crontab:
105+
106+
```bash
107+
gobackup perform >> ~/.gobackup/gobackup.log
108+
```
109+
And after a day, you can check up the execute status by `~/.gobackup/gobackup.log`.
110+

archive/archive.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package archive
2+
3+
import (
4+
"fmt"
5+
"path"
6+
"path/filepath"
7+
8+
"github.com/4nkitd/gobackup/config"
9+
"github.com/4nkitd/gobackup/helper"
10+
"github.com/4nkitd/gobackup/logger"
11+
)
12+
13+
// Run archive
14+
func Run(model config.ModelConfig) (err error) {
15+
if model.Archive == nil {
16+
return nil
17+
}
18+
19+
logger.Info("------------- Archives -------------")
20+
21+
helper.MkdirP(model.DumpPath)
22+
23+
includes := model.Archive.GetStringSlice("includes")
24+
includes = cleanPaths(includes)
25+
26+
excludes := model.Archive.GetStringSlice("excludes")
27+
excludes = cleanPaths(excludes)
28+
29+
if len(includes) == 0 {
30+
return fmt.Errorf("archive.includes have no config")
31+
}
32+
logger.Info("=> includes", len(includes), "rules")
33+
34+
opts := options(model.DumpPath, excludes, includes)
35+
helper.Exec("tar", opts...)
36+
37+
logger.Info("------------- Archives -------------\n")
38+
39+
return nil
40+
}
41+
42+
func options(dumpPath string, excludes, includes []string) (opts []string) {
43+
tarPath := path.Join(dumpPath, "archive.tar")
44+
if helper.IsGnuTar {
45+
opts = append(opts, "--ignore-failed-read")
46+
}
47+
opts = append(opts, "-cPf", tarPath)
48+
49+
for _, exclude := range excludes {
50+
opts = append(opts, "--exclude="+filepath.Clean(exclude))
51+
}
52+
53+
opts = append(opts, includes...)
54+
55+
return opts
56+
}
57+
58+
func cleanPaths(paths []string) (results []string) {
59+
for _, p := range paths {
60+
results = append(results, filepath.Clean(p))
61+
}
62+
return
63+
}

archive/artchive_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package archive
2+
3+
import (
4+
"strings"
5+
"testing"
6+
7+
"github.com/4nkitd/gobackup/config"
8+
"github.com/4nkitd/gobackup/helper"
9+
"github.com/stretchr/testify/assert"
10+
)
11+
12+
func TestRun(t *testing.T) {
13+
// with nil Archive
14+
model := config.ModelConfig{
15+
Archive: nil,
16+
}
17+
err := Run(model)
18+
assert.NoError(t, err)
19+
}
20+
21+
func TestOptions(t *testing.T) {
22+
includes := []string{
23+
"/foo/bar/dar",
24+
"/bar/foo",
25+
"/ddd",
26+
}
27+
28+
excludes := []string{
29+
"/hello/world",
30+
"/cc/111",
31+
}
32+
33+
dumpPath := "~/work/dir"
34+
35+
opts := options(dumpPath, excludes, includes)
36+
cmd := strings.Join(opts, " ")
37+
if helper.IsGnuTar {
38+
assert.Equal(t, cmd, "--ignore-failed-read -cPf ~/work/dir/archive.tar --exclude=/hello/world --exclude=/cc/111 /foo/bar/dar /bar/foo /ddd")
39+
} else {
40+
assert.Equal(t, cmd, "-cPf ~/work/dir/archive.tar --exclude=/hello/world --exclude=/cc/111 /foo/bar/dar /bar/foo /ddd")
41+
}
42+
}

compressor/base.go

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package compressor
2+
3+
import (
4+
"os"
5+
"path"
6+
"time"
7+
8+
"github.com/4nkitd/gobackup/config"
9+
"github.com/4nkitd/gobackup/logger"
10+
"github.com/spf13/viper"
11+
)
12+
13+
// Base compressor
14+
type Base struct {
15+
name string
16+
model config.ModelConfig
17+
viper *viper.Viper
18+
}
19+
20+
// Context compressor
21+
type Context interface {
22+
perform() (archivePath string, err error)
23+
}
24+
25+
func (ctx *Base) archiveFilePath(ext string) string {
26+
return path.Join(ctx.model.TempPath, time.Now().Format("2006.01.02.15.04.05")+ext)
27+
}
28+
29+
func newBase(model config.ModelConfig) (base Base) {
30+
base = Base{
31+
name: model.Name,
32+
model: model,
33+
viper: model.CompressWith.Viper,
34+
}
35+
return
36+
}
37+
38+
// Run compressor
39+
func Run(model config.ModelConfig) (archivePath string, err error) {
40+
base := newBase(model)
41+
42+
var ctx Context
43+
switch model.CompressWith.Type {
44+
case "tgz":
45+
ctx = &Tgz{Base: base}
46+
case "tar":
47+
ctx = &Tar{Base: base}
48+
default:
49+
ctx = &Tar{}
50+
}
51+
52+
logger.Info("------------ Compressor -------------")
53+
logger.Info("=> Compress | " + model.CompressWith.Type)
54+
55+
// set workdir
56+
os.Chdir(path.Join(model.DumpPath, "../"))
57+
archivePath, err = ctx.perform()
58+
if err != nil {
59+
return
60+
}
61+
logger.Info("->", archivePath)
62+
logger.Info("------------ Compressor -------------\n")
63+
64+
return
65+
}

0 commit comments

Comments
 (0)