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
%.backup/restore: %.backup _always
58
	$(restoreBien) $<
59

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

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

    
68
#### Archived imports
69

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

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

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

    
78
##### Synchronization
79

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

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

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