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
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
30

    
31
##### Backups
32

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

    
35
#### Checksums
36

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

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

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

    
45
#### SQL
46

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

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

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

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

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

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

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

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

    
83
#### Archived imports
84

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

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

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

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

    
96
##### Synchronization
97

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

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

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

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