Project

General

Profile

1 6666 aaronmk
##### Configuration
2
3
src_server ?= jupiter
4
5
6 6522 aaronmk
selfDir_NzrNZp := $(dir $(lastword $(MAKEFILE_LIST)))
7
root := $(selfDir_NzrNZp)..
8
include $(root)/lib/common.Makefile
9 3699 aaronmk
10
11 3393 aaronmk
##### Vars/functions
12
13 3403 aaronmk
# Make
14
SHELL := /bin/bash
15
16 3438 aaronmk
# OS
17
isMac := $(filter Darwin,$(os))
18 3393 aaronmk
19 3438 aaronmk
# Paths
20 6522 aaronmk
bin := $(root)/bin
21 3438 aaronmk
22 3393 aaronmk
# DB
23
psqlVerbose := $(bin)/psql_script_vegbien --echo-all
24
pg_dump := $(bin)/pg_dump_vegbien
25 3404 aaronmk
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 3393 aaronmk
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
30
31 3437 aaronmk
##### Backups
32 3393 aaronmk
33 6526 aaronmk
backups := $(wildcard *.backup)
34
35 6571 aaronmk
#### Checksums
36
37 6526 aaronmk
md5s: _always $(backups:%=%.md5) ;
38
39 6520 aaronmk
%.md5: %
40 6536 aaronmk
	nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
41 6520 aaronmk
42 6573 aaronmk
%.md5/test: %.md5 % _always
43 6655 aaronmk
	echo '$(shell cat $<)  $*'|nice -n +5 md5sum -$(if $(isMac),v)c /dev/stdin
44 6572 aaronmk
45 6571 aaronmk
#### SQL
46
47 6520 aaronmk
# Must come before %.sql with no prerequisites to be matched first
48 3395 aaronmk
%.sql: %
49 3399 aaronmk
	$(restore) $< >$@
50 3395 aaronmk
51 4751 aaronmk
# Full DB
52
# Must come before %.backup to be matched first
53
vegbien.%.backup:
54
	$(backup) all >$@
55 6648 aaronmk
	$(MAKE) $@.md5
56 4751 aaronmk
57 3544 aaronmk
backup* = $(backup) $* >$@
58
59 3393 aaronmk
# 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 3543 aaronmk
%.backup:
63
	$(backup*)
64 6521 aaronmk
	$(MAKE) $@.md5
65 3544 aaronmk
66
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
67
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
68 3543 aaronmk
%.sql:
69
	env plain=1 $(backup*)
70 3544 aaronmk
endif
71 3393 aaronmk
72 3402 aaronmk
%.backup/restore: %.backup _always
73 3399 aaronmk
	$(restoreBien) $<
74 3393 aaronmk
75 3402 aaronmk
%.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 3409 aaronmk
%.backup/test: %.backup.sql _always
81
	rm $<
82
83 3437 aaronmk
#### Archived imports
84 3393 aaronmk
85
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
86
87 6645 aaronmk
public* = $(*:vegbien.%=public.%)
88
89 3393 aaronmk
%.backup/remove: %.backup _always
90 6645 aaronmk
	@$(call confirmRm,$(public*))
91
	echo $(call rmSchema,$(public*))|$(bin)/psql_script_vegbien
92 3393 aaronmk
93
%.backup/rm_indexes: %.backup _always
94
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
95 3407 aaronmk
96 3701 aaronmk
##### Synchronization
97
98 6890 aaronmk
remote := $(src_user)@$(src_server):/data/dev/aaronmk/bien/backups/
99 6666 aaronmk
100 6469 aaronmk
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
101 6595 aaronmk
--include="/*.csv" --include="/*.md5" --exclude="**"
102 3701 aaronmk
103
upload: _always
104
	$(rsyncBackups) $(local) $(remote)
105 6667 aaronmk
106
%/download: _always
107
	$(rsync) $(remote)$* $(local)$*
108 6724 aaronmk
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
109
# ignore errors if $*.md5 does not exist