Project

General

Profile

1
selfDir_NzrNZp := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_NzrNZp)..
3
include $(root)/lib/common.Makefile
4

    
5

    
6
##### Vars/functions
7

    
8
# Make
9
SHELL := /bin/bash
10

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

    
15
# 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
# 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
# Paths
33
bin := $(root)/bin
34

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

    
44
##### Backups
45

    
46
backups := $(wildcard *.backup)
47

    
48
#### Checksums
49

    
50
md5s: _always $(backups:%=%.md5) ;
51

    
52
%.md5: %
53
	nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
54

    
55
%.md5/test: %.md5 % _always
56
	echo '$(shell cat $<)  $*'|nice -n +5 md5sum -vc /dev/stdin
57

    
58
#### SQL
59

    
60
# Must come before %.sql with no prerequisites to be matched first
61
%.sql: %
62
	$(restore) $< >$@
63

    
64
# Full DB
65
# Must come before %.backup to be matched first
66
vegbien.%.backup:
67
	$(backup) all >$@
68
	$(MAKE) $@.md5
69

    
70
backup* = $(backup) $* >$@
71

    
72
# Note: This can't be used for the current (unrotated) public schema because
73
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
74
# that public already exists because it's in template1.
75
%.backup:
76
	$(backup*)
77
	$(MAKE) $@.md5
78

    
79
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
80
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
81
%.sql:
82
	env plain=1 $(backup*)
83
endif
84

    
85
%.backup/restore: %.backup _always
86
	$(restoreBien) $<
87

    
88
%.sql/restore: %.sql _always
89
	$(bin)/psql_script_vegbien <$<
90
# pg_restore only supports "non-plain-text formats"
91
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
92

    
93
%.backup/test: %.backup.sql _always
94
	rm $<
95

    
96
%.backup/rotate: %.backup _always
97
	mv $< $*.$(call mtime,$<).backup
98

    
99
#### Archived imports
100

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

    
103
public* = $(*:vegbien.%=public.%)
104

    
105
%.backup/remove: %.backup _always
106
	@$(call confirmRm,$(public*))
107
	echo $(call rmSchema,$(public*))|$(bin)/psql_script_vegbien
108

    
109
%.backup/rm_indexes: %.backup _always
110
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
111

    
112
##### Synchronization
113

    
114
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
115
--include="/*.csv" --include="/*.md5" --exclude="**"
116

    
117
upload: _always
118
	$(rsyncBackups) $(local) $(remote)
119
download: _always
120
	$(rsyncBackups) $(remote) $(local)
    (1-1/1)