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