-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackup-pgsql.sh
executable file
·114 lines (97 loc) · 2.79 KB
/
backup-pgsql.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
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
#!/bin/bash
# Adlibre 2012-13: Backup PostgreSQL databases and send passive check result to Nagios/Icinga (NSCA)
# Install:
# yum -y install nsca-client
## Source config file
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
if [ -e "${DIR}/config" ]; then
. ${DIR}/config
fi
## Config Defaults
PASS=${PASS}
USER=${USER-root}
HOST=${HOST-localhost}
BACKUPDIR=${BACKUPDIR-/srv/backup}
KEEP=${KEEP-30}
NAGIOS_SERVER=${NAGIOS_SERVER}
NAGIOS_SERVICE_NAME=${NAGIOS_SERVICE_NAME-PostgreSQL Dump Daily}
LOCKFILE="/var/run/`basename $0 | sed s/\.sh// `.pid"
##
DAY=`date +'%F %T'`
# Sanity Checks
# Check to see if we are already running / locked, limit to one instance
if [ -f ${LOCKFILE} ] ; then
echo "Error: Already running, or locked. Lockfile exists [`ls -ld $LOCKFILE`]."
exit 99
else
echo $$ > ${LOCKFILE}
# Upon exit, remove lockfile.
trap "{ rm -f ${LOCKFILE}; }" EXIT
fi
# Init
DATE=`date +%F`
cd $BACKUPDIR;
#
# Send passive alert information to Nagios / Icinga
#
function raiseAlert {
# $1 - Service name that has been set up on nagios/nagiosdev server
# $2 - Return code 0=success, 1=warning, 2=critical
# $3 - Message you want to send
# <host_name>,<svc_description>,<return_code>,<plugin_output>
# defaults that can be overridden
NAGIOS_DIR=${NAGIOS_DIR-/usr/sbin/}
NAGIOS_CFG=${NAGIOS_CFG-/etc/nagios/}
NAGIOS_PORT=${NAGIOS_PORT-5667}
if [ -f ${NAGIOS_DIR}send_nsca ]; then
echo "`hostname`,$1,$2,$3" | ${NAGIOS_DIR}send_nsca -H ${NAGIOS_SERVER} \
-p ${NAGIOS_PORT} -d "," -c ${NAGIOS_CFG}send_nsca.cfg > /dev/null;
# echo "Debug: Message Sent to Nagios ($NAGIOS_SERVER): $1 $2 $3.";
else
echo "Warning: NSCA (Nagios) Plugin not found.";
fi
}
function doBackup {
# do the backup
cd / && \
su postgres -c pg_dumpall > ${BACKUPDIR}/${DATE}.pgsql.dump ;
}
function delBackup {
# delete the old backups
find $BACKUPDIR -name "*.dump.gz" -mtime +$KEEP -exec rm -f {} \;
}
function compressBackup {
# Gzip
gzip -f -9 --rsyncable $BACKUPDIR/$DATE.*.dump;
}
function linkLatest {
# Latest link
ln -f ${DATE}.pgsql.dump.gz ${BACKUPDIR}/latest.pgsql.dump.gz
}
CMD=`doBackup`;
if [ $? -ne 0 ] ;
then
raiseAlert "$NAGIOS_SERVICE_NAME" 2 "Backup Failed during doBackup. $CMD"
exit 99;
fi
CMD=`delBackup`;
if [ $? -ne 0 ] ;
then
raiseAlert "$NAGIOS_SERVICE_NAME" 2 "Backup Failed during delBackup. $CMD"
exit 99;
fi
CMD=`compressBackup`;
if [ $? -ne 0 ] ;
then
raiseAlert "$NAGIOS_SERVICE_NAME" 2 "Backup Failed during compressBackup. $CMD"
exit 99;
fi
CMD=`linkLatest`;
if [ $? -ne 0 ] ;
then
raiseAlert "$NAGIOS_SERVICE_NAME" 2 "Backup Failed during linkLatest. $CMD"
exit 99;
fi
# default exit
raiseAlert "$NAGIOS_SERVICE_NAME" 0 "Backup Completed OK at $DAY"
exit 0