-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpostgres_backup.sh
More file actions
executable file
·149 lines (132 loc) · 4.06 KB
/
postgres_backup.sh
File metadata and controls
executable file
·149 lines (132 loc) · 4.06 KB
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env sh
# Usage:
# postgres_backup.sh [man] //Manual backup, full output
# postgres_backup.sh auto //Auto backup (separate backup location, only outputs STDERR messages)
# CONFIG
### Example Config File ############
###
### PORT=5432
### DIR=/var/lib/pgsql/backup
### COMPRESSION=zstd
### COMP_LEVEL=14
### THREADS=2
###
####################################
#
# Load config file:
if [ ! -f ${HOME}/.postgres_backup.conf ]; then
echo "ERROR 10: Configuration file not found"
exit 10
fi
source ${HOME}/.postgres_backup.conf
## Check if automated, set appropriate directory
if [ "$1" = "auto" ]; then
AUTO="true"
DIR=${DIR}/auto
else
AUTO="false"
DIR=${DIR}/manual
fi
## Define message commands
msg_show() {
[ "$AUTO" = "false" ] && printf "$1"
}
msg_check() {
[ "$AUTO" = "false" ] && printf '%-70s %s' "$1" # | sed s/\ \ /../g
}
get_size() {
tsize=$(find $1 -prune -printf '%s\n')
[ $tsize -lt 1024 ] && tsize="$tsize B" || tsize=$(numfmt --to=iec-i --suffix=B --format='%.3f' <<<$tsize | sed -r 's/([A-Z])/ \1/')
printf "$tsize"
}
## Sanity check
pass="$(tput bold)$(tput setaf 2)OK$(tput sgr0)\n"
fail="$(tput bold)$(tput setaf 1)FAIL$(tput sgr0)\n"
msg_show "=== $(tput bold)Config Sanity Check$(tput sgr0)\n"
msg_check "PostgreSQL server port: $(tput bold)${PORT}$(tput sgr0)"
if [ $PORT -lt 65536 -a $PORT -gt 0 ] 2>/dev/null; then
msg_show "$pass"
else
msg_show "$fail"
echo "ERROR 11: PORT configuration not a valid port number" >&2
exit 11
fi
msg_check "Backup directory: $(tput bold)${DIR}$(tput sgr0)"
if [ -d "$DIR" ]; then
msg_show "$pass"
else
msg_show "$fail"
echo "ERROR 12: Backup directory does not exist" >&2
exit 12
fi
msg_check "Compression type: $(tput bold)${COMPRESSION}$(tput sgr0)"
case $COMPRESSION in
xz)
msg_show "$pass"
msg_check "Compression level: $(tput bold)${COMP_LEVEL}$(tput sgr0)"
if [ $(sed s/e// <<< $COMP_LEVEL) -lt 10 -a $(sed s/e// <<< $COMP_LEVEL) -gt 0 ] 2>/dev/null; then
msg_show "$pass"
else
msg_show "$fail"
echo "ERROR 14: Compression level not valid" >&2
exit 14
fi
;;
zstd)
msg_show "$pass"
msg_check "Compression level: $(tput bold)${COMP_LEVEL}$(tput sgr0)"
if [ $COMP_LEVEL -lt 20 -a $COMP_LEVEL -gt 0 ] 2>/dev/null; then
msg_show "$pass"
else
msg_show "$fail"
echo "ERROR 14: Compression level not valid" >&2
exit 14
fi
;;
*)
msg_show "$fail"
echo "ERROR 13: Compression not configured correctly" >&2
exit 13
;;
esac
msg_check "Thread count: $(tput bold)${THREADS}$(tput sgr0)"
if [ $THREADS -eq $THREADS -a $THREADS -gt -1 ] 2>/dev/null; then
msg_show "$pass"
[ $THREADS -gt $(nproc) ] 2>/dev/null && msg_show "$(tput setaf 3)$(tput bold)WARN:$(tput sgr0) THREADS is set higher than CPU count!\n"
else
msg_show "$fail"
echo "ERROR 15: Thread count is not a valid positive integer" >&2
exit 15
fi
## End sanity check
## Set date for timestamp
DATE=$(date +%Y%m%d-%H%M)
msg_show "Timestamp set to $(tput bold)${DATE}$(tput sgr0)\n"
## Get list of DBs
list_db=$(psql -p${PORT} -U postgres -t -c 'select datname from pg_database;' | grep -v 'template0\|template1\|postgres' | sed '/^$/d')
## Set compression parameters
case $COMPRESSION in
xz)
compress="xz -T${THREADS} -${COMP_LEVEL} -"
comp_ext="xz"
;;
zstd)
compress="zstd -T${THREADS} -${COMP_LEVEL} -z -"
comp_ext="zst"
;;
*)
echo "ERROR 13: Compression not configured correctly!" >&2
exit 13
;;
esac
msg_show "\nStarting backup procedures...\n\n"
## Backup globals
target="${DIR}/globals_${DATE}.dmp.${comp_ext}"
pg_dumpall -p${PORT} -g | eval $compress > ${DIR}/globals_${DATE}.dmp.${comp_ext}
msg_show "Backed up $(tput setaf 2)$(tput bold)globals$(tput sgr0) as $(tput setaf 3)${target}$(tput sgr0) [$(tput setaf 2)$(get_size $target)$(tput sgr0)] using $(tput bold)${compress}$(tput sgr0)\n"
for db in $list_db; do
target="${DIR}/${db}_${DATE}.dmp.${comp_ext}"
pg_dump -O $db | eval $compress > $target
msg_show "Backed up $(tput setaf 2)$(tput bold)${db}$(tput sgr0) as $(tput setaf 3)${target}$(tput sgr0) [$(tput setaf 2)$(get_size $target)$(tput sgr0)] using $(tput bold)${compress}$(tput sgr0)\n"
done
exit 0