-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile
47 lines (38 loc) · 1.36 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
.PHONY: backup run_database build_database install_dependencies wait_for_database prod dev
# Environment variables
DB_CONTAINER_NAME := database
DB_NAME := main
DB_USER := main
DB_PASSWORD := hHMP8cd^N3SnzGRR
DB_PORT := 5432
DB_VOLUME := postgres_data
PERSISTENT_DIR := $(HOME)/persistent/$(APP_NAME)
BACKUP_FILENAME := $(HOME)/backups/$(shell date +"%Y-%m-%d_%H-%M-%S").sql.gz
dev:
docker compose up
prod: install_dependencies backup run_database wait_for_database
install_dependencies:
bun install
backup:
mkdir -p $(PERSISTENT_DIR)
docker exec -t $(DB_CONTAINER_NAME) pg_dump --data-only -U $(DB_USER) $(DB_NAME) | gzip > $(BACKUP_FILENAME)
run_database: stop_database
docker volume rm -f $(DB_VOLUME)
docker volume create $(DB_VOLUME)
docker build -t $(DB_CONTAINER_NAME) .
docker run -d --name $(DB_CONTAINER_NAME) \
-e POSTGRES_USER=$(DB_USER) \
-e POSTGRES_PASSWORD=$(DB_PASSWORD) \
-p $(DB_PORT):5432 \
-v $(DB_VOLUME):/var/lib/postgresql/data \
-v $(PERSISTENT_DIR):/persistent \
$(DB_CONTAINER_NAME)
stop_database:
-docker stop $(DB_CONTAINER_NAME)
-docker rm $(DB_CONTAINER_NAME)
wait_for_database:
@while ! docker exec $(DB_CONTAINER_NAME) pg_isready -U $(DB_USER) -h localhost -p $(DB_PORT) > /dev/null 2>&1; do \
sleep 1; \
done
restore_backup:
gunzip -c $(BACKUP_FILENAME) | docker exec -i $(DB_CONTAINER_NAME) psql -U $(DB_USER) -d postgres