-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMakefile
More file actions
87 lines (68 loc) · 2.18 KB
/
Copy pathMakefile
File metadata and controls
87 lines (68 loc) · 2.18 KB
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
CONTAINER_NAME=split-the-bill-postgres-db
BACKUP_DIR=./storage/database/backups
# Get the current date and time in a format suitable for a filename
DATE := $(shell date +%Y%m%d%H%M%S)
# Set the backup file name
BACKUP_NAME := db-backup-$(DATE)
# Load environment variables from .env file
ifneq (,$(wildcard .env))
include .env
export $(shell sed 's/=.*//' .env)
endif
# Export the database data to a file inside the container
.PHONY: export-db
export-db:
docker exec -t $(CONTAINER_NAME) pg_dump -U $(DB_USER) -d $(DB_NAME) --data-only -f /tmp/$(BACKUP_NAME).sql
# Copy the backup file from the container to the host machine
.PHONY: copy-backup
copy-backup:
docker cp $(CONTAINER_NAME):/tmp/$(BACKUP_NAME).sql $(BACKUP_DIR)/$(BACKUP_NAME).sql
# Full backup process
.PHONY: backup-db
backup-db:
@make export-db
@make copy-backup
@echo "Backup completed and saved to $(BACKUP_DIR)/$(BACKUP_NAME).sql"
# Copy the backup file from the host to the container
.PHONY: copy-backup-to-container
copy-backup-to-container:
docker cp $(BACKUP_FILE) $(CONTAINER_NAME):/tmp/$(BACKUP_FILE_NAME)
# Import the backup file into the PostgreSQL database
.PHONY: import-db
import-db:
docker exec -t $(CONTAINER_NAME) psql -U $(DB_USER) -d $(DB_NAME) -f /tmp/$(BACKUP_FILE_NAME)
# Full import process with a specific backup file
.PHONY: load-backup
load-backup:
@if [ -z "$(file)" ]; then \
echo "Please provide a backup file using 'make import-backup file=<backup_file>'"; \
exit 1; \
fi
@make copy-backup-to-container BACKUP_FILE=$(file) BACKUP_FILE_NAME=$(notdir $(file))
@make import-db BACKUP_FILE_NAME=$(notdir $(file))
@echo "Backup $(BACKUP_DIR)/$(BACKUP_FILE_NAME) imported into the database"
# Open Postgres Docker container shell
docker-db-shell:
@docker exec -it $(CONTAINER_NAME) bash
run:
@make swag
@make build
@./split-the-bill-server
build:
@go build
watch:
@reflex -s -r '\.go$$' -R 'docs.go' make run
swag:
@swag init && swag fmt
clean:
@rm split-the-bill-server
test-all:
@go test --cover ./...
start-db:
@docker compose up --no-attach pgadmin
seed-db:
@docker exec split-the-bill-server sh -c "go run script/db_seed.go"
stop-db:
@docker compose down
reset-db:
@docker compose down -v