Project

General

Profile

1 6666 aaronmk
##### Configuration
2
3 7557 aaronmk
remote_host ?= jupiter
4 7559 aaronmk
remote_basepath ?= /data/dev/aaronmk/bien
5 6666 aaronmk
6
7 6522 aaronmk
selfDir_NzrNZp := $(dir $(lastword $(MAKEFILE_LIST)))
8
root := $(selfDir_NzrNZp)..
9
include $(root)/lib/common.Makefile
10 3699 aaronmk
11
12 3393 aaronmk
##### Vars/functions
13
14 3403 aaronmk
# Make
15
SHELL := /bin/bash
16
17 3438 aaronmk
# Paths
18 6522 aaronmk
bin := $(root)/bin
19 3438 aaronmk
20 3393 aaronmk
# DB
21 7024 aaronmk
psqlNoSearchPath := env no_search_path=1 $(bin)/psql_script_vegbien
22 3393 aaronmk
pg_dump := $(bin)/pg_dump_vegbien
23 3404 aaronmk
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 3393 aaronmk
28 3437 aaronmk
##### Backups
29 3393 aaronmk
30 6526 aaronmk
backups := $(wildcard *.backup)
31
32 6571 aaronmk
#### Checksums
33
34 6526 aaronmk
md5s: _always $(backups:%=%.md5) ;
35
36 6571 aaronmk
#### SQL
37
38 6520 aaronmk
# Must come before %.sql with no prerequisites to be matched first
39 3395 aaronmk
%.sql: %
40 3399 aaronmk
	$(restore) $< >$@
41 3395 aaronmk
42 4751 aaronmk
# Full DB
43
# Must come before %.backup to be matched first
44
vegbien.%.backup:
45
	$(backup) all >$@
46 6648 aaronmk
	$(MAKE) $@.md5
47 4751 aaronmk
48 3544 aaronmk
backup* = $(backup) $* >$@
49
50 3393 aaronmk
# 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 3543 aaronmk
%.backup:
54
	$(backup*)
55 6521 aaronmk
	$(MAKE) $@.md5
56 3544 aaronmk
57
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
58
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
59 3543 aaronmk
%.sql:
60
	env plain=1 $(backup*)
61 3544 aaronmk
endif
62 3393 aaronmk
63 3402 aaronmk
%.backup/restore: %.backup _always
64 3399 aaronmk
	$(restoreBien) $<
65 3393 aaronmk
66 3402 aaronmk
%.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 3409 aaronmk
%.backup/test: %.backup.sql _always
72
	rm $<
73
74 3437 aaronmk
#### Archived imports
75 3393 aaronmk
76
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
77
78 6940 aaronmk
public* = $(*:vegbien.%=%)
79 6645 aaronmk
80 3393 aaronmk
%.backup/remove: %.backup _always
81 6645 aaronmk
	@$(call confirmRm,$(public*))
82 7024 aaronmk
	echo $(call rmSchemaCmd,$(public*))|$(psqlNoSearchPath)
83 3393 aaronmk
84 3701 aaronmk
##### Synchronization
85
86 6469 aaronmk
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
87 6595 aaronmk
--include="/*.csv" --include="/*.md5" --exclude="**"
88 3701 aaronmk
89
upload: _always
90
	$(rsyncBackups) $(local) $(remote)
91 6667 aaronmk
92
%/download: _always
93 7037 aaronmk
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
94 6667 aaronmk
	$(rsync) $(remote)$* $(local)$*
95 6724 aaronmk
# ignore errors if $*.md5 does not exist