Project

General

Profile

1 6522 aaronmk
selfDir_NzrNZp := $(dir $(lastword $(MAKEFILE_LIST)))
2
root := $(selfDir_NzrNZp)..
3
include $(root)/lib/common.Makefile
4 3699 aaronmk
5
6 3393 aaronmk
##### Vars/functions
7
8 3403 aaronmk
# Make
9
SHELL := /bin/bash
10
11 3438 aaronmk
# Paths
12 6522 aaronmk
bin := $(root)/bin
13 3438 aaronmk
14 3393 aaronmk
# DB
15 7024 aaronmk
psqlNoSearchPath := env no_search_path=1 $(bin)/psql_script_vegbien
16 3393 aaronmk
pg_dump := $(bin)/pg_dump_vegbien
17 3404 aaronmk
backup := "time" env data=1 $(pg_dump)
18 10254 aaronmk
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error --verbose
19 3404 aaronmk
restoreBien := $(restore) --dbname=vegbien
20
# pg_restore doesn't support PGDATABASE env var
21 3393 aaronmk
22 3437 aaronmk
##### Backups
23 3393 aaronmk
24 6526 aaronmk
backups := $(wildcard *.backup)
25
26 6571 aaronmk
#### Checksums
27
28 6526 aaronmk
md5s: _always $(backups:%=%.md5) ;
29
30 6571 aaronmk
#### SQL
31
32 6520 aaronmk
# Must come before %.sql with no prerequisites to be matched first
33 3395 aaronmk
%.sql: %
34 3399 aaronmk
	$(restore) $< >$@
35 3395 aaronmk
36 4751 aaronmk
# Full DB
37
# Must come before %.backup to be matched first
38
vegbien.%.backup:
39
	$(backup) all >$@
40 6648 aaronmk
	$(MAKE) $@.md5
41 4751 aaronmk
42 3544 aaronmk
backup* = $(backup) $* >$@
43
44 3393 aaronmk
# 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 3543 aaronmk
%.backup:
48
	$(backup*)
49 6521 aaronmk
	$(MAKE) $@.md5
50 3544 aaronmk
51
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
52
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
53 3543 aaronmk
%.sql:
54
	env plain=1 $(backup*)
55 3544 aaronmk
endif
56 3393 aaronmk
57 10267 aaronmk
# runtime: 11 h ("10:50:23elapsed"); ~5 h to insert data
58 3402 aaronmk
%.backup/restore: %.backup _always
59 3399 aaronmk
	$(restoreBien) $<
60 3393 aaronmk
61 3402 aaronmk
%.sql/restore: %.sql _always
62
	$(bin)/psql_script_vegbien <$<
63
# pg_restore only supports "non-plain-text formats"
64
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
65
66 3409 aaronmk
%.backup/test: %.backup.sql _always
67
	rm $<
68
69 3437 aaronmk
#### Archived imports
70 3393 aaronmk
71
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
72
73 6940 aaronmk
public* = $(*:vegbien.%=%)
74 6645 aaronmk
75 3393 aaronmk
%.backup/remove: %.backup _always
76 6645 aaronmk
	@$(call confirmRm,$(public*))
77 7024 aaronmk
	echo $(call rmSchemaCmd,$(public*))|$(psqlNoSearchPath)
78 3393 aaronmk
79 3701 aaronmk
##### Synchronization
80
81 6469 aaronmk
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
82 6595 aaronmk
--include="/*.csv" --include="/*.md5" --exclude="**"
83 3701 aaronmk
84
upload: _always
85 10027 aaronmk
	$(bin)/sync_upload './**'
86 6667 aaronmk
87
%/download: _always
88 7037 aaronmk
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
89 6667 aaronmk
	$(rsync) $(remote)$* $(local)$*
90 6724 aaronmk
# ignore errors if $*.md5 does not exist