Project

General

Profile

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