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
# Paths
12
bin := $(root)/bin
13

    
14
# DB
15
psqlNoSearchPath := env no_search_path=1 $(bin)/psql_script_vegbien
16
pg_dump := $(bin)/pg_dump_vegbien
17
backup := "time" env data=1 $(pg_dump)
18
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error --verbose
19
restoreBien := $(restore) --dbname=vegbien
20
# pg_restore doesn't support PGDATABASE env var
21

    
22
##### Backups
23

    
24
backups := $(wildcard *.backup)
25

    
26
#### Checksums
27

    
28
md5s: _always $(backups:%=%.md5) ;
29

    
30
#### SQL
31

    
32
# Must come before %.sql with no prerequisites to be matched first
33
%.sql: %
34
	$(restore) $< >$@
35

    
36
# Full DB
37
# Must come before %.backup to be matched first
38
vegbien.%.backup:
39
	$(backup) all >$@
40
	$(MAKE) $@.md5
41

    
42
backup* = $(backup) $* >$@
43

    
44
# Note: This can't be used for the current (unrotated) public schema because
45
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
46
# that public already exists because it's in template1.
47
%.backup:
48
	$(backup*)
49
	$(MAKE) $@.md5
50

    
51
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
52
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
53
%.sql:
54
	env plain=1 $(backup*)
55
endif
56

    
57
# runtime: 11 h ("10:50:23elapsed"); ~5 h to insert data
58
%.backup/restore: %.backup _always
59
	$(restoreBien) $<
60

    
61
%.sql/restore: %.sql _always
62
	$(bin)/psql_script_vegbien <$<
63
# pg_restore only supports "non-plain-text formats"
64
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
65

    
66
%.backup/test: %.backup.sql _always
67
	rm $<
68

    
69
#### Archived imports
70

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

    
73
public* = $(*:vegbien.%=%)
74

    
75
%.backup/remove: %.backup _always
76
	@$(call confirmRm,$(public*))
77
	echo $(call rmSchemaCmd,$(public*))|$(psqlNoSearchPath)
78

    
79
##### Synchronization
80

    
81
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
82
--include="/*.csv" --include="/*.md5" --exclude="**"
83

    
84
upload: _always
85
	$(bin)/sync_upload './**'
86

    
87
%/download: _always
88
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
89
	$(rsync) $(remote)$* $(local)$*
90
# ignore errors if $*.md5 does not exist
(3-3/189)