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
os := $(shell uname)
18
isMac := $(filter Darwin,$(os))
19 3393 aaronmk
20 3438 aaronmk
# System
21
dateFmt := %Y-%m-%d-%H-%M-%S
22
mtime = $(shell $(if $(isMac),stat -f %Sm -t "$(dateFmt)" $(1),date\
23
--reference=$(1) +"$(dateFmt)"))
24
25 3393 aaronmk
# Terminal
26
esc := '['
27
reset := $(esc)'0m'
28
emph := $(esc)'7m '
29
endEmph := ' '$(reset)
30
31
# User interaction
32
33
confirm = $(if $(shell read -p $(emph)"$(1)"$(endEmph)$$'$(if\
34
$(2),\n$(2))\nContinue? (y/n) ' REPLY; test "$$REPLY" = y && echo t),,\
35
$(error Aborting))
36
37 3438 aaronmk
# Paths
38 6522 aaronmk
bin := $(root)/bin
39 3438 aaronmk
40 3393 aaronmk
# DB
41
psqlVerbose := $(bin)/psql_script_vegbien --echo-all
42
pg_dump := $(bin)/pg_dump_vegbien
43 3404 aaronmk
backup := "time" env data=1 $(pg_dump)
44
restore := "time" $(bin)/postgres_vegbien pg_restore --exit-on-error
45
restoreBien := $(restore) --dbname=vegbien
46
# pg_restore doesn't support PGDATABASE env var
47 3393 aaronmk
rmSchema = 'DROP SCHEMA IF EXISTS "$(1)" CASCADE;'
48
49 3437 aaronmk
##### Backups
50 3393 aaronmk
51 6526 aaronmk
backups := $(wildcard *.backup)
52
53 6571 aaronmk
#### Checksums
54
55 6526 aaronmk
md5s: _always $(backups:%=%.md5) ;
56
57 6520 aaronmk
%.md5: %
58 6536 aaronmk
	nice -n +5 md5sum $<|cut -d ' ' -f 1 >$@
59 6520 aaronmk
60 6573 aaronmk
%.md5/test: %.md5 % _always
61 6655 aaronmk
	echo '$(shell cat $<)  $*'|nice -n +5 md5sum -$(if $(isMac),v)c /dev/stdin
62 6572 aaronmk
63 6571 aaronmk
#### SQL
64
65 6520 aaronmk
# Must come before %.sql with no prerequisites to be matched first
66 3395 aaronmk
%.sql: %
67 3399 aaronmk
	$(restore) $< >$@
68 3395 aaronmk
69 4751 aaronmk
# Full DB
70
# Must come before %.backup to be matched first
71
vegbien.%.backup:
72
	$(backup) all >$@
73 6648 aaronmk
	$(MAKE) $@.md5
74 4751 aaronmk
75 3544 aaronmk
backup* = $(backup) $* >$@
76
77 3393 aaronmk
# Note: This can't be used for the current (unrotated) public schema because
78
# pg_dump doesn't back up the CREATE SCHEMA statement for it, assuming falsely
79
# that public already exists because it's in template1.
80 3543 aaronmk
%.backup:
81
	$(backup*)
82 6521 aaronmk
	$(MAKE) $@.md5
83 3544 aaronmk
84
# When testing, turn off %.sql so make won't skip `%.sql: %` in favor of it
85
ifeq ($(filter %.backup/test,$(MAKECMDGOALS)),)
86 3543 aaronmk
%.sql:
87
	env plain=1 $(backup*)
88 3544 aaronmk
endif
89 3393 aaronmk
90 3402 aaronmk
%.backup/restore: %.backup _always
91 3399 aaronmk
	$(restoreBien) $<
92 3393 aaronmk
93 3402 aaronmk
%.sql/restore: %.sql _always
94
	$(bin)/psql_script_vegbien <$<
95
# pg_restore only supports "non-plain-text formats"
96
# (http://www.postgresql.org/docs/9.1/static/app-pgrestore.html)
97
98 3409 aaronmk
%.backup/test: %.backup.sql _always
99
	rm $<
100
101 3438 aaronmk
%.backup/rotate: %.backup _always
102
	mv $< $*.$(call mtime,$<).backup
103
104 3437 aaronmk
#### Archived imports
105 3393 aaronmk
106
confirmRm = $(call confirm,WARNING: This will delete the archived import $(1)!)
107
108 6645 aaronmk
public* = $(*:vegbien.%=public.%)
109
110 3393 aaronmk
%.backup/remove: %.backup _always
111 6645 aaronmk
	@$(call confirmRm,$(public*))
112
	echo $(call rmSchema,$(public*))|$(bin)/psql_script_vegbien
113 3393 aaronmk
114
%.backup/rm_indexes: %.backup _always
115
	$(pg_dump) $*|$(bin)/mk_rm_indexes|$(psqlVerbose)
116 3407 aaronmk
117 3701 aaronmk
##### Synchronization
118
119 6889 aaronmk
remote := $(src_user)@$(src_server):/data/dev/aaronmk/bien/backups
120 6666 aaronmk
121 6469 aaronmk
rsyncBackups := $(rsync) --include="/*.backup" --include="/*.sql"\
122 6595 aaronmk
--include="/*.csv" --include="/*.md5" --exclude="**"
123 3701 aaronmk
124
upload: _always
125
	$(rsyncBackups) $(local) $(remote)
126 6667 aaronmk
127
%/download: _always
128
	$(rsync) $(remote)$* $(local)$*
129 6724 aaronmk
	-$(if $(filter %.md5,$*),,$(MAKE) $*.md5/download)
130
# ignore errors if $*.md5 does not exist