Project

General

Profile

1
include ../lib/common.Makefile
2

    
3

    
4
##### Vars/functions
5

    
6
# Make
7
SHELL := /bin/bash
8

    
9
# OS
10
os := $(shell uname)
11
isMac := $(filter Darwin,$(os))
12

    
13
# 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
# 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
# Paths
31
bin := ../bin
32

    
33
# DB
34
psqlVerbose := $(bin)/psql_script_vegbien --echo-all
35
pg_dump := $(bin)/pg_dump_vegbien
36
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
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
41

    
42
##### Backups
43

    
44
# Must come before %.sql with no prerequisites to be matched first
45
%.md5: %
46
	md5 -q $< >$@
47

    
48
# Must come before %.sql with no prerequisites to be matched first
49
%.sql: %
50
	$(restore) $< >$@
51

    
52
# Full DB
53
# Must come before %.backup to be matched first
54
vegbien.%.backup:
55
	$(backup) all >$@
56

    
57
backup* = $(backup) $* >$@
58

    
59
# 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
%.backup:
63
	$(backup*)
64

    
65
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
66
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
67
%.sql:
68
	env plain=1 $(backup*)
69
endif
70

    
71
%.backup/restore: %.backup _always
72
	$(restoreBien) $<
73

    
74
%.sql/restore: %.sql _always
75
	$(bin)/psql_script_vegbien <$<
76
# pg_restore only supports "non-plain-text formats"
77
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
78

    
79
%.backup/test: %.backup.sql _always
80
	rm $<
81

    
82
%.backup/rotate: %.backup _always
83
	mv $< $*.$(call mtime,$<).backup
84

    
85
#### Archived imports
86

    
87
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
88

    
89
%.backup/remove: %.backup _always
90
	@$(call confirmRm,$*)
91
	echo $(call rmSchema,$*)|$(bin)/psql_script_vegbien
92

    
93
%.backup/rm_indexes: %.backup _always
94
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
95

    
96
##### Synchronization
97

    
98
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
99
--include="/*.csv" --exclude="**"
100

    
101
upload: _always
102
	$(rsyncBackups) $(local) $(remote)
103
download: _always
104
	$(rsyncBackups) $(remote) $(local)
    (1-1/1)