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
# OS
17
isMac := $(filter Darwin,$(os))
18

    
19
# Paths
20
bin := $(root)/bin
21

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

    
30
##### Backups
31

    
32
backups := $(wildcard *.backup)
33

    
34
#### Checksums
35

    
36
md5s: _always $(backups:%=%.md5) ;
37

    
38
%.md5: %
39
	nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
40

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

    
44
#### SQL
45

    
46
# Must come before %.sql with no prerequisites to be matched first
47
%.sql: %
48
	$(restore) $< >$@
49

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

    
56
backup* = $(backup) $* >$@
57

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

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

    
71
%.backup/restore: %.backup _always
72
	$(restoreBien) $<
73

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

    
79
%.backup/test: %.backup.sql _always
80
	rm $<
81

    
82
#### Archived imports
83

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

    
86
public* = $(*:vegbien.%=%)
87

    
88
%.backup/remove: %.backup _always
89
	@$(call confirmRm,$(public*))
90
	echo $(call rmSchemaCmd,$(public*))|$(bin)/psql_script_vegbien
91

    
92
%.backup/rm_indexes: %.backup _always
93
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
94

    
95
##### Synchronization
96

    
97
remote := $(src_user)@$(src_server):/data/dev/aaronmk/bien/backups/
98

    
99
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
100
--include="/*.csv" --include="/*.md5" --exclude="**"
101

    
102
upload: _always
103
	$(rsyncBackups) $(local) $(remote)
104

    
105
%/download: _always
106
	$(rsync) $(remote)$* $(local)$*
107
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
108
# ignore errors if $*.md5 does not exist
(1-1/2)