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
|
#! /bin/bash
#
# Script for automatic maintenance of Debian PostgreSQL, to be run by cron,
# with the owner being the postgres superuser
# Use of this script with PostgreSQL passwords is likely to be insecure.
# The preferred method for ensuring access is to have "ident" authentication
# enabled by having this line in /etc/postgresql/pg_hba.conf:
#
# local all postgres ident sameuser
#
# this will allow the postgres superuser on the local machine to connect as
# itself without giving a password. This is now the default configuration for
# the Debian package.
#
# If password access for "local" is turned on in /etc/postgresql/pg_hba.conf,
# you must create a file ~postgres/.pgpass containing a line specifying the
# password, as explained in section 1.11 of the PostgreSQL Programmer's Guide
# (package postgresql-doc).
. /etc/postgresql/postgresql.env
syntax() {
echo Syntax: $0 [-v] [-a] [-f] [-F] [-u user] [-u]
exit 1
}
verbose=
analyse=
while getopts aFfvu: arg
do
case $arg
in
a)
analyse=ANALYZE
;;
F)
force=FORCE
;;
f)
full=FULL
;;
v)
verbose=VERBOSE
;;
u)
user=$OPTARG
;;
*)
syntax;;
esac
done
. /etc/postgresql/postgresql.env
/usr/sbin/invoke-rc.d --query postgresql start
if [ $? -ne 104 ]
then
[ -n "$verbose" ] &&
echo "PostgreSQL is currently disabled by update-rc.d or file-rc"
exit 1
fi
if [ ! -S /var/run/postgresql/.s.PGSQL.5432 ]
then
[ -n "$verbose" ] &&
echo "The PostgreSQL postmaster is not running (there is no UNIX socket)"
exit 1
fi
if [ -r $PGDATA/postmaster.pid ]
then
# check the postmaster is running
if ! /bin/kill -0 `sed '2,$d' $PGDATA/postmaster.pid`
then
[ -n "$verbose" ] &&
echo "The PostgreSQL postmaster is not running (no process)"
exit 1
fi
fi
if [ -r $PGDATA/autovacuum.pid ]
then
# check if autovacuum is running
if /bin/kill -0 `cat $PGDATA/autovacuum.pid`
then
if [ -z "$force" ]
then
echo "The PostgreSQL autovacuum daemon is running and force is not enabled"
exit 1
fi
fi
fi
if [ -n "$user" ]
then
UNAME="-U$user"
fi
dblist=$(/usr/bin/psql $UNAME -q -X -t -d template1 -P border=0 -c "
SELECT datname
FROM pg_database
WHERE datallowconn
ORDER BY datname")
IFS='
'
(
for database in $dblist
do
echo "\c '${database}'"
echo "vacuum ${full} ${verbose} ${analyse};"
done
) | /usr/bin/psql -d template1 -t -q -X ${UNAME} 2>&1
|