forked from Altinity/clickhouse-backup
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
119 lines (95 loc) · 4.53 KB
/
Makefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# Origin: https://github.com/innogames/graphite-ch-optimizer/blob/master/Makefile
# MIT License
NAME = clickhouse-backup
VERSION = $(shell git describe --always --tags --abbrev=0 2>/dev/null | sed 's/^v//;s/\([^-]*-g\)/c\1/;s/-/./g')
GIT_COMMIT = $(shell git rev-parse HEAD)
DATE = $(shell date +%F)
VENDOR = "Eugene Klimov <[email protected]>"
URL = https://github.com/Altinity/$(NAME)
define DESC =
'Tool for easy ClickHouse backup and restore with S3 and GCS support
Easy creating and restoring backups of all or specific tables
Efficient storing of multiple backups on the file system
Most efficient AWS S3/GCS uploading and downloading with streaming compression
Support of incremental backups on remote storages'
endef
GO_BUILD = go build -buildvcs=false -ldflags "-X 'main.version=$(VERSION)' -X 'main.gitCommit=$(GIT_COMMIT)' -X 'main.buildDate=$(DATE)'"
PKG_FILES = build/$(NAME)_$(VERSION).amd64.deb build/$(NAME)_$(VERSION).arm64.deb build/$(NAME)-$(VERSION)-1.amd64.rpm build/$(NAME)-$(VERSION)-1.arm64.rpm
HOST_OS = $(shell bash -c 'source <(go env) && echo $$GOHOSTOS')
HOST_ARCH = $(shell bash -c 'source <(go env) && echo $$GOHOSTARCH')
.PHONY: clean all version test
all: build config packages
version:
@echo $(VERSION)
clean:
rm -rf build
rm -rf $(NAME)
rebuild: clean all
test:
go vet ./...
mkdir -v ./_coverage_
GOCOVERDIR=./_coverage_/ go test -coverprofile=./_coverage_/coverage.out -v ./...
build: build/linux/amd64/$(NAME) build/linux/arm64/$(NAME) build/darwin/amd64/$(NAME) build/darwin/arm64/$(NAME)
build/linux/amd64/$(NAME) build/darwin/amd64/$(NAME): GOARCH = amd64
build/linux/arm64/$(NAME) build/darwin/arm64/$(NAME): GOARCH = arm64
build/linux/amd64/$(NAME) build/linux/arm64/$(NAME): GOOS = linux
build/darwin/amd64/$(NAME) build/darwin/arm64/$(NAME): GOOS = darwin
build/linux/amd64/$(NAME) build/linux/arm64/$(NAME) build/darwin/amd64/$(NAME) build/darwin/arm64/$(NAME):
CGO_ENABLED=0 GOOS=$(GOOS) GOARCH=$(GOARCH) $(GO_BUILD) -o $@ ./cmd/$(NAME)
config: $(NAME)/config.yml
$(NAME)/$(NAME): build/$(HOST_OS)/amd64/$(NAME)
mkdir -p $(NAME)
cp -lv ./build/linux/amd64/$(NAME) ./$(NAME)/$(NAME)
$(NAME)/config.yml: $(NAME)/$(NAME) build/$(HOST_OS)/$(HOST_ARCH)/$(NAME)
./build/$(HOST_OS)/$(HOST_ARCH)/$(NAME) default-config > $@
build/linux/amd64/config.yml build/linux/arm64/config.yml build/darwin/amd64/config.yml build/darwin/arm64/config.yml: config
cp -lv ./$(NAME)/config.yml $@
packages: $(PKG_FILES)
build/linux/amd64/pkg: ARCH = amd64
build/linux/arm64/pkg: ARCH = arm64
.ONESHELL:
build/linux/amd64/pkg build/linux/arm64/pkg: build config
cd ./build/linux/$(ARCH)
mkdir -pv pkg/etc/$(NAME)
mkdir -pv pkg/usr/bin
cp -lv $(NAME) pkg/usr/bin/
cp -lv ../../../$(NAME)/config.yml pkg/etc/$(NAME)/config.yml.example
deb: $(word 1, $(PKG_FILES))
deb_arm: $(word 2, $(PKG_FILES))
rpm: $(word 3, $(PKG_FILES))
rpm_arm: $(word 4, $(PKG_FILES))
# Set TYPE to package suffix w/o dot
$(PKG_FILES): PKG_TYPE = $(subst .,,$(suffix $@))
# Set ARCH to package python split('.')[-1].split('_')[-]
$(PKG_FILES): PKG_ARCH = $(word $(shell echo $(words $(subst ., ,$@))-1 | bc),$(subst ., ,$@))
$(PKG_FILES): build/linux/amd64/pkg build/linux/arm64/pkg
fpm --verbose \
-s dir \
-a $(PKG_ARCH) \
-t $(PKG_TYPE) \
--vendor $(VENDOR) \
-m $(VENDOR) \
--url $(URL) \
--description $(DESC) \
--license MIT \
-n $(NAME) \
-v $(VERSION) \
-p build/linux/$(PKG_ARCH) \
build/linux/$(PKG_ARCH)/pkg/=/
build-race: $(NAME)/$(NAME)-race
$(NAME)/$(NAME)-race:
CGO_ENABLED=1 $(GO_BUILD) -cover -gcflags "all=-N -l" -race -o $@ ./cmd/$(NAME)
# run `docker buildx create --use` first time
build-race-docker:
bash -xce 'docker buildx build --build-arg CLICKHOUSE_VERSION=$${CLICKHOUSE_VERSION:-latest} --build-arg CLICKHOUSE_IMAGE=$${CLICKHOUSE_IMAGE:-clickhouse/clickhouse-server} --tag $(NAME):build-race --target make-build-race --progress plain --load . && \
mkdir -pv ./$(NAME) && \
DOCKER_ID=$$(docker create $(NAME):build-race) && \
docker cp -q $${DOCKER_ID}:/src/$(NAME)/$(NAME)-race ./$(NAME)/ && \
docker rm -f "$${DOCKER_ID}" && \
cp -fl ./$(NAME)/$(NAME)-race ./$(NAME)/$(NAME)-race-docker'
build-docker:
bash -xce 'docker buildx build --build-arg CLICKHOUSE_VERSION=$${CLICKHOUSE_VERSION:-latest} --build-arg CLICKHOUSE_IMAGE=$${CLICKHOUSE_IMAGE:-clickhouse/clickhouse-server} --tag $(NAME):build-docker --target make-build-docker --progress plain --load . && \
mkdir -pv ./build && \
DOCKER_ID=$$(docker create $(NAME):build-docker) && \
docker cp -q $${DOCKER_ID}:/src/build/ ./build/ && \
docker rm -f "$${DOCKER_ID}"'