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