-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackup_postgresql_db.sh
executable file
·83 lines (66 loc) · 2.13 KB
/
backup_postgresql_db.sh
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
#!/bin/bash
# Copyright (c) 2016, Philippe Ivaldi <www.piprime.fr>
# This program is free software ; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation ; either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY ; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
# You should have received a copy of the GNU Lesser General Public License
# along with this program ; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
BACK_DIR=~postgres/backups
CURRENT_DIR=$(dirname "$0")
. ${CURRENT_DIR}/functions.rc
usage() {
echo '### DESCRIPTION ###' >&2
echo 'This script make a binary dump of a Postgres database using pg_dump'
echo "The directory where the backups live will be ${BACK_DIR}"
echo 'You can list the databases with the command executed as postgres : '
echo "psql --tuples-only -U postgres -c \"\\l\""
echo
echo '### USAGE ###' >&2
echo "$0 [-o output-file-name] database-name-to-backup" >&2
exit 1
}
[ $# -lt 1 ] && {
usage;
}
while getopts o:h option
do
case $option in
o)
BACK="${BACK_DIR}/${OPTARG}"
;;
h)
usage
;;
esac
done
if [ "$USER" != "postgres" ]; then
ERROR 'This script must be run as postgres user.'
exit 1
fi
shift $(($OPTIND-1))
[ -e $BACK_DIR ] || {
INFO "Creating directory '${BACK_DIR}'"
mkdir -p "$BACK_DIR"
chown postgres "$BACK_DIR"
}
databaseName="$1"
[ "$BACK" = "" ] && {
BACK="${BACK_DIR}/${databaseName}.$(date '+%Y-%m-%d_%H-%M-%S').bin"
}
[ -e $BACK ] && {
ERROR "The file ${BACK} exists"
ERROR 'Remove or rename it before proceed'
exit 1
}
pg_dump -v -F c -U postgres -f "$BACK" "$databaseName" && {
INFO "The database ${databaseName} was backuped in ${BACK}"
}
# Local variables:
# coding: utf-8
# End: