Project

General

Profile

1
##### Configuration
2

    
3
remote_host ?= jupiter
4
remote_basepath ?= /data/dev/aaronmk/bien
5

    
6

    
7
selfDir_NzrNZp := $(dir $(lastword $(MAKEFILE_LIST)))
8
root := $(selfDir_NzrNZp)..
9
include $(root)/lib/common.Makefile
10

    
11

    
12
##### Vars/functions
13

    
14
# Make
15
SHELL := /bin/bash
16

    
17
# Paths
18
bin := $(root)/bin
19

    
20
# DB
21
psqlNoSearchPath := env no_search_path=1 $(bin)/psql_script_vegbien
22
pg_dump := $(bin)/pg_dump_vegbien
23
backup := "time" env data=1 $(pg_dump)
24
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error
25
restoreBien := $(restore) --dbname=vegbien
26
# pg_restore doesn't support PGDATABASE env var
27

    
28
##### Backups
29

    
30
backups := $(wildcard *.backup)
31

    
32
#### Checksums
33

    
34
md5s: _always $(backups:%=%.md5) ;
35

    
36
#### SQL
37

    
38
# Must come before %.sql with no prerequisites to be matched first
39
%.sql: %
40
	$(restore) $< >$@
41

    
42
# Full DB
43
# Must come before %.backup to be matched first
44
vegbien.%.backup:
45
	$(backup) all >$@
46
	$(MAKE) $@.md5
47

    
48
backup* = $(backup) $* >$@
49

    
50
# Note: This can't be used for the current (unrotated) public schema because
51
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
52
# that public already exists because it's in template1.
53
%.backup:
54
	$(backup*)
55
	$(MAKE) $@.md5
56

    
57
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
58
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
59
%.sql:
60
	env plain=1 $(backup*)
61
endif
62

    
63
%.backup/restore: %.backup _always
64
	$(restoreBien) $<
65

    
66
%.sql/restore: %.sql _always
67
	$(bin)/psql_script_vegbien <$<
68
# pg_restore only supports "non-plain-text formats"
69
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
70

    
71
%.backup/test: %.backup.sql _always
72
	rm $<
73

    
74
#### Archived imports
75

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

    
78
public* = $(*:vegbien.%=%)
79

    
80
%.backup/remove: %.backup _always
81
	@$(call confirmRm,$(public*))
82
	echo $(call rmSchemaCmd,$(public*))|$(psqlNoSearchPath)
83

    
84
##### Synchronization
85

    
86
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
87
--include="/*.csv" --include="/*.md5" --exclude="**"
88

    
89
upload: _always
90
	$(rsyncBackups) $(local) $(remote)
91

    
92
%/download: _always
93
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
94
	$(rsync) $(remote)$* $(local)$*
95
# ignore errors if $*.md5 does not exist
(1-1/2)