-
Notifications
You must be signed in to change notification settings - Fork 1
/
pg_dump-Dow
executable file
·125 lines (105 loc) · 2.52 KB
/
pg_dump-Dow
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
#!/usr/bin/env bash
help() { cat <<'END'
Synopsis:
Midnight dump of PG database into backup/<database>/<database>.<Dow>.d
Description:
Typical butt save, daily pg_dump of a PostgreSQL database.
The backup format is pg_dump directory, named like:
backup/<database>/<database>-<Dow>.d/
Compression is 9 (best). The parallel jobs is set to 4 processors.
Faults are written to run/pg_dump-midnight-<database>.fault
Usage:
D=setspace
0 0 * * * pg_dump-Dow $DB >>log/pg_dump-Dow-$DB.log 2>&1
Depends
$JMSCOTT_ROOT/bin/duration-english
$PGHOME/bin/pg_dump
Source Code:
https://github.com/jmscott/work
Exit Status:
0 dump ok
1 dump failed
END
#
# Note:
# Investigate if directory format of pg_dump includes checksum in
# manifest.
#
exit 0
}
DOW=$(date +%a)
PROG=$(basename $0)
START_EPOCH=$(date +%s)
JOBS=4
COMPRESSION=9
PSQL='psql --no-psqlrc'
now()
{
date +'%Y/%m/%d: %H:%M:%S'
}
log()
{
echo "$(now): $@"
}
leave()
{
STATUS=$?
ELAPSED_DURATION=$(duration-english $(expr $(date +%s) - $START_EPOCH))
log "elapsed: $ELAPSED_DURATION"
log 'good bye, cruel world'
exit $STATUS
}
die()
{
MSG="ERROR: $@"
test -w run && echo "$(now): $MSG" >>run/$PROG.fault
log "$MSG" >&2
exit 1
}
in_PATH()
{
EXE=$1
type -P $EXE 2>/dev/null || die "executable not in \$PATH: $EXE"
}
test "$1" = '--help' && help
test $# = 1 || die "wrong number of arguments: got $#, expected 1"
DATABASE=$1
log 'hello, world'
trap leave EXIT INT TERM
test -r etc/profile && . etc/profile
log "PATH=$PATH"
in_PATH duration-english
in_PATH pg_dump
log "PGHOME=$PGHOME"
log "PGHOST=$PGHOST"
log "PGPORT=$PGPORT"
log "PGUSER=$PGUSER"
log "PGPASSWORD=$PGPASSWORD"
export PGDATABASE=$DATABASE
log "PGDATABASE=$PGDATABASE"
log "day of week: $DOW"
log "compression: $COMPRESSION"
log "parallel jobs: $JOBS"
BACKUP=backup/$DATABASE
log "backup db dir: $BACKUP"
mkdir -p $BACKUP || die "mkdir backup failed: exit status=$?"
BACKUP=backup/$DATABASE/$DATABASE-$DOW.d
log "backup Dow dir: $BACKUP"
if [ -d $BACKUP ]; then
log "removing previous backup: $(du -sh $BACKUP | awk '{print $1}')"
rm -rf $BACKUP || die "rm daily backup dir failed: exit status=$?"
fi
DBSIZE=$($PSQL --tuples-only -c '
SELECT
pg_size_pretty(pg_database_size(current_database()));
' | sed 's/^ *//')
log "size of raw database: $DATABASE: $DBSIZE"
log 'starting pg_dump ...'
pg_dump $DATABASE \
--format=d \
--jobs=$JOBS \
--file=$BACKUP ||
die "pg_dump failed: exit status=$?"
log '... pg_dump done, OK, no errors'
log "dump size: $(du -sh $BACKUP | awk '{print $1}')"
exit 0