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