1 |
3393
|
aaronmk
|
##### Vars/functions
|
2 |
|
|
|
3 |
3403
|
aaronmk
|
# Make
|
4 |
|
|
SHELL := /bin/bash
|
5 |
|
|
|
6 |
3438
|
aaronmk
|
# OS
|
7 |
|
|
os := $(shell uname)
|
8 |
|
|
isMac := $(filter Darwin,$(os))
|
9 |
3393
|
aaronmk
|
|
10 |
3438
|
aaronmk
|
# System
|
11 |
|
|
dateFmt := %Y-%m-%d-%H-%M-%S
|
12 |
|
|
mtime = $(shell $(if $(isMac),stat -f %Sm -t "$(dateFmt)" $(1),date\
|
13 |
|
|
--reference=$(1) +"$(dateFmt)"))
|
14 |
|
|
|
15 |
3393
|
aaronmk
|
# Terminal
|
16 |
|
|
esc := '['
|
17 |
|
|
reset := $(esc)'0m'
|
18 |
|
|
emph := $(esc)'7m '
|
19 |
|
|
endEmph := ' '$(reset)
|
20 |
|
|
|
21 |
|
|
# User interaction
|
22 |
|
|
|
23 |
|
|
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
|
24 |
|
|
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
|
25 |
|
|
$(error Aborting))
|
26 |
|
|
|
27 |
3438
|
aaronmk
|
# Paths
|
28 |
|
|
bin := ../bin
|
29 |
|
|
|
30 |
3393
|
aaronmk
|
# DB
|
31 |
|
|
psqlVerbose := $(bin)/psql_script_vegbien --echo-all
|
32 |
|
|
pg_dump := $(bin)/pg_dump_vegbien
|
33 |
3404
|
aaronmk
|
backup := "time" env data=1 $(pg_dump)
|
34 |
|
|
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error
|
35 |
|
|
restoreBien := $(restore) --dbname=vegbien
|
36 |
|
|
# pg_restore doesn't support PGDATABASE env var
|
37 |
3393
|
aaronmk
|
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
|
38 |
|
|
|
39 |
|
|
##### General targets
|
40 |
|
|
|
41 |
|
|
all:
|
42 |
|
|
|
43 |
|
|
.SUFFIXES: # turn off built-in suffix rules
|
44 |
|
|
.SECONDARY: # don't automatically delete intermediate files
|
45 |
|
|
.DELETE_ON_ERROR: # delete target if recipe fails
|
46 |
|
|
|
47 |
|
|
_always:
|
48 |
|
|
.PHONY: _always
|
49 |
|
|
|
50 |
3437
|
aaronmk
|
##### Backups
|
51 |
3393
|
aaronmk
|
|
52 |
3395
|
aaronmk
|
# Must come before %.sql with no prerequisites to be matched first
|
53 |
|
|
%.sql: %
|
54 |
3399
|
aaronmk
|
$(restore) $< >$@
|
55 |
3395
|
aaronmk
|
|
56 |
3393
|
aaronmk
|
# Note: This can't be used for the current (unrotated) public schema because
|
57 |
|
|
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
|
58 |
|
|
# that public already exists because it's in template1.
|
59 |
|
|
%.backup %.sql:
|
60 |
3404
|
aaronmk
|
$(if $(filter %.sql,$@),env plain=1) $(backup) $* >$@
|
61 |
3393
|
aaronmk
|
|
62 |
3402
|
aaronmk
|
%.backup/restore: %.backup _always
|
63 |
3399
|
aaronmk
|
$(restoreBien) $<
|
64 |
3393
|
aaronmk
|
|
65 |
3402
|
aaronmk
|
%.sql/restore: %.sql _always
|
66 |
|
|
$(bin)/psql_script_vegbien <$<
|
67 |
|
|
# pg_restore only supports "non-plain-text formats"
|
68 |
|
|
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
|
69 |
|
|
|
70 |
3409
|
aaronmk
|
%.backup/test: %.backup.sql _always
|
71 |
|
|
rm $<
|
72 |
|
|
|
73 |
3438
|
aaronmk
|
%.backup/rotate: %.backup _always
|
74 |
|
|
mv $< $*.$(call mtime,$<).backup
|
75 |
|
|
|
76 |
3437
|
aaronmk
|
#### Archived imports
|
77 |
3393
|
aaronmk
|
|
78 |
|
|
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
|
79 |
|
|
|
80 |
|
|
%.backup/remove: %.backup _always
|
81 |
|
|
@$(call confirmRm,$*)
|
82 |
|
|
echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
|
83 |
|
|
|
84 |
|
|
%.backup/rm_indexes: %.backup _always
|
85 |
|
|
$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
|
86 |
3407
|
aaronmk
|
|
87 |
3437
|
aaronmk
|
#### Full DB
|
88 |
3407
|
aaronmk
|
|
89 |
|
|
vegbien.backup:
|
90 |
|
|
$(backup) all >$@
|