Доработав сценарий, делюсь, может кому будет полезно. Кроме того внимательно выслушаю замечания.
root@PostgreSQL:/home/maintainer# cat /root/backup.sh
#!/bin/bash
FILENAME='/root/psql_dblist'
PORT=5433
USERNAME='dbadmin'
SERVERNAME='localhost'
ARCHIVEDIR='/Backup'
SUFFIX=''
ADMINSEMAILS='email@gmail.com email@yandex.ru'
NETDIR='//192.168.88.11/Kamin/PostgreSQL'
EMFROM="email@mail.su"
ERRLOG="/root/backup.txt"
EMSMTP="192.168.88.232:25"
EMPASS="Pa$$w0rd"
if [[ 'daily' = $1 ]]
then
SUFFIX='daily_'$(date +%u)'_'$(date +%H)
#echo $SUFFIX
#echo Ok
fi
if [[ 'monthly' = $1 ]]
then
SUFFIX='monthly_'$(date +%b)'_'$(date +%H)
fi
if [[ 'hot' = $1 ]]
then
SUFFIX='hot_'$(date +%Y-%b-%d-%H%M)
fi
if [[ $SUFFIX = '' ]]
then
echo 'Type monthly, daily or hot as parametr'
exit
fi
if mount | grep -qw $NETDIR
then
echo "Network directory is mounted"
else
echo "Network direcrory $NETDIR is not mounted" >> $ERRLOG
SUBJECT="PostgeSQL:NoGood SUFFUX=$SUFFIX"
MESSAGE="File /root/backup.txt contain some errors. Look for attachment"
sendEmail -f $EMFROM -t $ADMINSEMAILS -u $SUBJECT -m $MESSAGE -s $EMSMTP -xu $EMFROM -xp $EMPASS -o tls=no -q -a $ERRLOG
exit
fi
#DBLIST=$(psql -U $USERNAME -p $PORT -l | awk '/psql/ { print $1 }')
DBLIST=$(psql -U $USERNAME -p $PORT -l | q -d'|' "select c1 from - where c1 <> '' and c2 <> '' and c1 not like 'template%' limit 1,10000")
echo "$DBLIST" > $FILENAME
cat ${FILENAME} | while read DBNAME;
do
mkdir -p ${ARCHIVEDIR}'/'${DBNAME};
pg_dump -d $DBNAME -h ${SERVERNAME} -p $PORT -U ${USERNAME} -w | gzip > ${ARCHIVEDIR}'/'${DBNAME}'/'${DBNAME}'_'$SUFFIX'.dump.gz';
#pg_dump -d $DBNAME -h ${SERVERNAME} -p $PORT -U ${USERNAME} -w | gzip > ${ARCHIVEDIR}'/'${DBNAME}'/'${DBNAME}'_'$(date +%Y-%b-%d-%H%M)'.dump.gz';
done
read BackupErr < /root/backup.txt
SUBJECT=''
MESSAGE=''
if [[ $BackupErr = '' ]]
then
SUBJECT="Archiving PostgeSQL:Ok SUFFUX=$SUFFIX"
MESSAGE="Archiving successful. File /root/backup.txt is empry."
else
SUBJECT="PostgeSQL:NoGood SUFFUX=$SUFFIX"
MESSAGE="File /root/backup.txt contain some errors. Look for attachment"
fi
sendEmail -f $EMFROM -t $ADMINSEMAILS -u $SUBJECT -m $MESSAGE -s $EMSMTP -xu $EMFROM -xp $EMPASS -o tls=no -q -a $ERRLOG
Добавлено уведомление о результате по e-mail. Оптимизации кода пока нет. На это тоже нужно время и самое главное время на тестирование.
root@PostgreSQL:/home/maintainer# crontab -l
# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command
0 8,12,16,20 * * * /root/backup.sh daily 2>> /root/backup.txt
0 4 15 * * /root/backup.sh monthly 2>> /root/backup.txt