扩展gobackup支持minio以及定时配置
huacnlee/gobackup 是一个很不错的多数据库的备份工具,但是对于minio支持不太好(path 路径格式),同时也
不支持定时任务备份,所以基于cron 包支持定时任务处理
修改的代码
- main.go
添加cron支持
{
Name: "start",
Flags: []cli.Flag{
cli.StringFlag{
Name: "start",
Usage: "cron execute backup task",
},
},
Action: func(c *cli.Context) error {
cronPerformAll()
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM)
sig := <-sigChan
logger.Info("shutting down %v signal received", sig)
return nil
},
},
func cronPerformAll() {
logger.Info(config.Scheduler)
mycron := cron.New()
if err := mycron.AddFunc(config.Scheduler, func() {
for _, modelConfig := range config.Models {
m := model.Model{
Config: modelConfig,
}
m.Perform()
}
}); err != nil {
logger.Error("start cron service error:", err.Error())
os.Exit(1)
}
mycron.Start()
}
- minio s3 path 支持
s3.go
cfg.S3ForcePathStyle = aws.Bool(true)
cfg.Region = aws.String(ctx.viper.GetString("region"))
cfg.MaxRetries = aws.Int(ctx.viper.GetInt("max_retries"))
Docker支持
基于多阶段构建同时内置依赖的工具
FROM golang:1.15-alpine AS build-env
WORKDIR /go/src/app
ENV GO111MODULE=on
ENV GOPROXY=https://goproxy.cn
COPY . .
RUN apk update && apk add git \
&& go build
FROM alpine:latest
RUN apk update && apk add ca-certificates mongodb-tools mysql-client redis postgresql-client && rm -rf /var/cache/apk/*
COPY --from=build-env /go/src/app/gobackup /usr/bin/gobackup
COPY gobackup.yml /etc/gobackup/gobackup.yml
ENTRYPOINT [ "gobackup" ]
参考使用
- docker-compose 文件
version: "3"
services:
monio:
image: minio/minio
command: server /export
environment:
- "MINIO_ACCESS_KEY=minio"
- "MINIO_SECRET_KEY=minio123"
ports:
- "9000:9000"
mysqldb:
image: mysql:5.6
restart: always
command: --character-set-server=utf8
ports:
- "3306:3306"
environment:
- MYSQL_ROOT_PASSWORD=dalong
- mysql sql
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
-- ----------------------------
-- Table structure for userdemo
-- ----------------------------
DROP TABLE IF EXISTS `userdemo`;
CREATE TABLE `userdemo` (
`username` varchar(255) DEFAULT NULL,
`userage` int(255) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Records of userdemo
-- ----------------------------
BEGIN;
INSERT INTO `userdemo` VALUES ('dalong', 11);
COMMIT;
SET FOREIGN_KEY_CHECKS = 1;
- 配置
scheduler:
cron: "0 0/2 * * *"
models:
first:
compress_with:
type: tgz
store_with:
type: local
keep: 10
path: /Users/dalong/mylearning/db-back/gobackup/mydb
databases:
demo-db:
type: mysql
host: 127.0.0.1
port: 3306
database: demo
dumpPath:
username: root
password: dalong
second:
compress_with:
type: tgz
store_with:
type: s3
keep: 20
bucket: demo
endpoint: http://localhost:9000
path: backups
access_key_id: minio
secret_access_key: minio123
databases:
mydemodb:
type: mysql
host: 127.0.0.1
port: 3306
database: demo
username: root
password: dalong
- 构建&&启动
go build
./gobackup start
- 效果
说明
gobackup 的设计并不难,主要是集成了一些工具,我们可以通过简单的扩展就支持定时任务,基于此可以灵活的进行备份处理,完整代码
可以查看github
参考资料
https://github.com/huacnlee/gobackup
https://github.com/rongfengliang/gobackup