Skip to content

Commit

Permalink
application
Browse files Browse the repository at this point in the history
  • Loading branch information
4nkitd committed Jun 24, 2022
0 parents commit 56135b5
Show file tree
Hide file tree
Showing 50 changed files with 2,982 additions and 0 deletions.
26 changes: 26 additions & 0 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Go
on: [push]
jobs:

build:
name: Build
runs-on: ubuntu-latest
env:
GO_ENV: test
steps:

- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.13
id: go

- name: Check out code into the Go module directory
uses: actions/checkout@v1

- name: Get dependencies
run: |
go get -v -t -d ./...
- name: Test
run: go test ./...
25 changes: 25 additions & 0 deletions .github/workflows/goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: goreleaser

on:
push:
tags:
- "*"
jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: 0
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2
with:
version: latest
args: release --rm-dist
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
20 changes: 20 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Binaries for programs and plugins
*.exe
*.dll
*.so
*.dylib

# Test binary, build with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Project-local glide cache, RE: https://github.com/Masterminds/glide/issues/736
.glide/
gobackup.yml
gobackup
build/
dist/
vendor/
log/
29 changes: 29 additions & 0 deletions DEVELOPMENT
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
## Install

- Install Go 1.16.x

```
$ brew install goreleaser
$ brew install dep
```

## Get Source

```
$ git clone https://github.com/4nkitd/gobackup.git
$ cd gobackup
$ go get
```

## Test

```
$ make test
```

## Release new version

```
$ git tag 0.7.3
$ make release
```
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
test:
GO_ENV=test go test ./...
run:
go run main.go -- perform -m demo -c ./gobackup_test.yml
release:
@rm -Rf dist/
@goreleaser --skip-validate
110 changes: 110 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
# GoBackup is a fullstack backup tool
design for web servers similar with [backup/backup](https://github.com/backup/backup), work with Crontab to backup automatically.

## Features

- No dependencies.
- Multiple Databases source support.
- Multiple Storage type support.
- Archive paths or files into a tar.

## Current Support status

### Databases

- MySQL
- PostgreSQL
- Redis - `mode: sync/copy`
- MongoDB

### Archive

Use `tar` command to archive many file or path into a `.tar` file.

### Compressor

- Tgz - `.tar.gz`
- Uncompressed - `.tar`

### Encryptor

- OpenSSL - `aes-256-cbc` encrypt

### Storages

- Local
- FTP
- SCP - Upload via SSH copy
- [Amazon S3](https://aws.amazon.com/s3)

## Install (macOS / Linux)

```bash
$ curl -sSL https://git.io/gobackup | bash
```

after that, you will get `/usr/local/bin/gobackup` command.

```bash
$ gobackup -h
NAME:
gobackup - Easy full stack backup operations on UNIX-like systems

USAGE:
gobackup [global options] command [command options] [arguments...]

VERSION:
1.0.0

COMMANDS:
perform
help, h Shows a list of commands or help for one command

GLOBAL OPTIONS:
--help, -h show help
--version, -v print the version
```

## Configuration

GoBackup will seek config files in:

- ~/.gobackup/gobackup.yml
- /etc/gobackup/gobackup.yml

Example config: [gobackup_test.yml](https://github.com/4nkitd/gobackup/blob/master/gobackup_test.yml)

```yml
# gobackup config example
# -----------------------
models:
JobName:
compress_with:
type: tgz
store_with:
type: s3
keep: 20
bucket: gobackup-test
region: ap-south-1
path: backups
access_key_id: Ohsgwk86h2ksas
secret_access_key: Ojsiw729wujhKdhwsIIOw9173
databases:
DbName:
type: mysql
host: localhost
port: 3306
database: test
username: root
password: 123456
```
## Backup Run
You may want run backup in scheduly, you need Crontab:
```bash
gobackup perform >> ~/.gobackup/gobackup.log
```
And after a day, you can check up the execute status by `~/.gobackup/gobackup.log`.

63 changes: 63 additions & 0 deletions archive/archive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package archive

import (
"fmt"
"path"
"path/filepath"

"github.com/4nkitd/gobackup/config"
"github.com/4nkitd/gobackup/helper"
"github.com/4nkitd/gobackup/logger"
)

// Run archive
func Run(model config.ModelConfig) (err error) {
if model.Archive == nil {
return nil
}

logger.Info("------------- Archives -------------")

helper.MkdirP(model.DumpPath)

includes := model.Archive.GetStringSlice("includes")
includes = cleanPaths(includes)

excludes := model.Archive.GetStringSlice("excludes")
excludes = cleanPaths(excludes)

if len(includes) == 0 {
return fmt.Errorf("archive.includes have no config")
}
logger.Info("=> includes", len(includes), "rules")

opts := options(model.DumpPath, excludes, includes)
helper.Exec("tar", opts...)

logger.Info("------------- Archives -------------\n")

return nil
}

func options(dumpPath string, excludes, includes []string) (opts []string) {
tarPath := path.Join(dumpPath, "archive.tar")
if helper.IsGnuTar {
opts = append(opts, "--ignore-failed-read")
}
opts = append(opts, "-cPf", tarPath)

for _, exclude := range excludes {
opts = append(opts, "--exclude="+filepath.Clean(exclude))
}

opts = append(opts, includes...)

return opts
}

func cleanPaths(paths []string) (results []string) {
for _, p := range paths {
results = append(results, filepath.Clean(p))
}
return
}
42 changes: 42 additions & 0 deletions archive/artchive_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package archive

import (
"strings"
"testing"

"github.com/4nkitd/gobackup/config"
"github.com/4nkitd/gobackup/helper"
"github.com/stretchr/testify/assert"
)

func TestRun(t *testing.T) {
// with nil Archive
model := config.ModelConfig{
Archive: nil,
}
err := Run(model)
assert.NoError(t, err)
}

func TestOptions(t *testing.T) {
includes := []string{
"/foo/bar/dar",
"/bar/foo",
"/ddd",
}

excludes := []string{
"/hello/world",
"/cc/111",
}

dumpPath := "~/work/dir"

opts := options(dumpPath, excludes, includes)
cmd := strings.Join(opts, " ")
if helper.IsGnuTar {
assert.Equal(t, cmd, "--ignore-failed-read -cPf ~/work/dir/archive.tar --exclude=/hello/world --exclude=/cc/111 /foo/bar/dar /bar/foo /ddd")
} else {
assert.Equal(t, cmd, "-cPf ~/work/dir/archive.tar --exclude=/hello/world --exclude=/cc/111 /foo/bar/dar /bar/foo /ddd")
}
}
65 changes: 65 additions & 0 deletions compressor/base.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package compressor

import (
"os"
"path"
"time"

"github.com/4nkitd/gobackup/config"
"github.com/4nkitd/gobackup/logger"
"github.com/spf13/viper"
)

// Base compressor
type Base struct {
name string
model config.ModelConfig
viper *viper.Viper
}

// Context compressor
type Context interface {
perform() (archivePath string, err error)
}

func (ctx *Base) archiveFilePath(ext string) string {
return path.Join(ctx.model.TempPath, time.Now().Format("2006.01.02.15.04.05")+ext)
}

func newBase(model config.ModelConfig) (base Base) {
base = Base{
name: model.Name,
model: model,
viper: model.CompressWith.Viper,
}
return
}

// Run compressor
func Run(model config.ModelConfig) (archivePath string, err error) {
base := newBase(model)

var ctx Context
switch model.CompressWith.Type {
case "tgz":
ctx = &Tgz{Base: base}
case "tar":
ctx = &Tar{Base: base}
default:
ctx = &Tar{}
}

logger.Info("------------ Compressor -------------")
logger.Info("=> Compress | " + model.CompressWith.Type)

// set workdir
os.Chdir(path.Join(model.DumpPath, "../"))
archivePath, err = ctx.perform()
if err != nil {
return
}
logger.Info("->", archivePath)
logger.Info("------------ Compressor -------------\n")

return
}
Loading

0 comments on commit 56135b5

Please sign in to comment.