Project

General

Profile

1
##### Configuration
2

    
3
src_server ?= jupiter
4

    
5

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

    
10

    
11
##### Vars/functions
12

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

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

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

    
27
##### Backups
28

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

    
31
#### Checksums
32

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

    
35
%.md5: %
36
	nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
37

    
38
%.md5/test: %.md5 % _always
39
	echo '$(shell cat $<)  $*'|nice -n +5 md5sum -$(if $(isMac),v)c /dev/stdin
40

    
41
#### SQL
42

    
43
# Must come before %.sql with no prerequisites to be matched first
44
%.sql: %
45
	$(restore) $< >$@
46

    
47
# Full DB
48
# Must come before %.backup to be matched first
49
vegbien.%.backup:
50
	$(backup) all >$@
51
	$(MAKE) $@.md5
52

    
53
backup* = $(backup) $* >$@
54

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

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

    
68
%.backup/restore: %.backup _always
69
	$(restoreBien) $<
70

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

    
76
%.backup/test: %.backup.sql _always
77
	rm $<
78

    
79
#### Archived imports
80

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

    
83
public* = $(*:vegbien.%=%)
84

    
85
%.backup/remove: %.backup _always
86
	@$(call confirmRm,$(public*))
87
	echo $(call rmSchemaCmd,$(public*))|$(psqlNoSearchPath)
88

    
89
##### Synchronization
90

    
91
remote := $(src_user)@$(src_server):/data/dev/aaronmk/bien/backups/
92

    
93
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
94
--include="/*.csv" --include="/*.md5" --exclude="**"
95

    
96
upload: _always
97
	$(rsyncBackups) $(local) $(remote)
98

    
99
%/download: _always
100
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
101
	$(rsync) $(remote)$* $(local)$*
102
# ignore errors if $*.md5 does not exist
(1-1/2)