generated from dopos/dcape-app-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Makefile.parts
92 lines (74 loc) · 4.63 KB
/
Makefile.parts
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
# ------------------------------------------------------------------------------
## App operations
#:
## Выполнить файл в текущей БД (make sql SQL=file.sql)
sql:
@cat $${SQL:?Must be set} | docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER
# ------------------------------------------------------------------------------
## Бэкап БД без партиций (PGSCHEMA)
dump-noparts:
@echo "BackUp data for $${PGSCHEMA:?Must be set}..." ; \
docker exec -i $$DB_CONTAINER pg_dump -d $$PGDATABASE -U $$PGUSER -n $${PGSCHEMA} -T $${PGSCHEMA}'.*_p[0-9]+' -Ft \
| gzip > $${DEST_PATH}backupdb-zabbix-$${PGSCHEMA}-$(DATESTAMP)-noparts.tgz
## Бэкап партиций с данными на вчера (PGSCHEMA)
dump-parts:
@echo "BackUp partitions for $${PGSCHEMA:?Must be set}..." ; \
week=$$(expr $$(date --date=yesterday +%s) / 604800) ; \
from=$$(expr $$week \* 604800) ; dfrom=$$(date -d "@$$from" +%F) ; \
echo "BackUp parts for $$from ($$dfrom)..." ; \
docker exec -i $$DB_CONTAINER pg_dump -d $$PGDATABASE -U $$PGUSER -n $${PGSCHEMA} -t $${PGSCHEMA}'.*_p'$${from} -Ft \
| gzip > $${DEST_PATH}backupdb-zabbix-$${PGSCHEMA}-$${dfrom}-$${from}-parts.tgz
## Бэкап партиций с данными на сегодня, если партиция отличается от вчерашней
dump-parts-now:
@echo "BackUp current partitions for $${PGSCHEMA:?Must be set}..." ; \
week=$$(expr $$(date --date=yesterday +%s) / 604800) ; \
week_now=$$(expr $$(date +%s) / 604800) ; \
if [[ $$week == $$week_now ]] ; then \
echo "Parts for now and yesterday are the same"; \
else \
from=$$(expr $$week_now \* 604800) ; dfrom=$$(date -d "@$$from" +%F) ; \
echo "BackUp parts for $$from ($$dfrom)..." ; \
docker exec -i $$DB_CONTAINER pg_dump -d $$PGDATABASE -U $$PGUSER -n $${PGSCHEMA} -t $${PGSCHEMA}'.*_p'$${from} -Ft \
| gzip > $${DEST_PATH}backupdb-zabbix-$${PGSCHEMA}-$${dfrom}-$${from}-parts.tgz ; \
fi
# ------------------------------------------------------------------------------
## Восстановление архива из параметра SRC
rest:
zcat $${SRC:?Must be set} | docker exec -i $$DB_CONTAINER pg_restore -Ft \
--if-exists --clean -O -d $$PGDATABASE -U $$PGUSER
## Восстановление партиций из параметра SRC
rest-parts:
zcat $${SRC:?Must be set} | docker exec -i $$DB_CONTAINER pg_restore -Ft \
-1 --section=pre-data --section=data --if-exists --clean -O -d $$PGDATABASE -U $$PGUSER
# ------------------------------------------------------------------------------
## Проверка, что дефолтные партиции пустые
check-def-empty:
@docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER \
-c "call parts.defaults_size('$${PGSCHEMA}')"
# ------------------------------------------------------------------------------
## Создание дефолтных партиций
parts-def:
@echo "Create default partitions for schema $${PGSCHEMA:?Must be set}..." ; \
docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER \
-c "call parts.defaults('$${PGSCHEMA}')"
## Создание новых партиций для послезавтра
parts-new:
@dt=$$(date --date='+2days' +%F) ; \
echo "Create new partitions for $$dt in schema $${PGSCHEMA:?Must be set}..." ; \
docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER \
-c "call parts.attach('$${PGSCHEMA}', time_min := parts.date2uts('$$dt'))"
## Создание новой партиции заданной таблицы для данных из дефолтной партиции
parts-new-from:
@echo "Create new partitions for $${PART_DATE:?Must be set}" ; \
echo "Table: $${PGSCHEMA:?Must be set}.$${TABLE:?Must be set}" ; \
docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER \
-c "call parts.attach_from_default('$$TABLE', '$$PGSCHEMA', time_min := parts.date2uts('$$PART_DATE'))"
## Конвертация таблицы в партиционированную по заданному столбцу
parts-enable:
@echo "Enable partitions for $${PGSCHEMA:?Must be set}.$${TABLE:?Must be set} on $${COLUMN:?Must be set}" ; \
docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER \
-c "call parts.enable('$$TABLE', '$$COLUMN', '$$PGSCHEMA')"
# ------------------------------------------------------------------------------
## Загрузить код (схема parts)
parts-install:
@cat parts.sql | docker exec -i $${DB_CONTAINER:?Must be set} psql -d $$PGDATABASE -U $$PGUSER