autopostgresqlbackup 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600
  1. #!/bin/bash
  2. #
  3. # PostgreSQL Backup Script Ver 1.0
  4. # http://autopgsqlbackup.frozenpc.net
  5. # Copyright (c) 2005 Aaron Axelsen <axelseaa@amadmax.com>
  6. #
  7. # This script is based of the AutoMySQLBackup Script Ver 2.2
  8. # It can be found at http://sourceforge.net/projects/automysqlbackup/
  9. #
  10. # The PostgreSQL changes are based on a patch agaisnt AutoMySQLBackup 1.9
  11. # created by Friedrich Lobenstock <fl@fl.priv.at>
  12. #
  13. # This program is free software; you can redistribute it and/or modify
  14. # it under the terms of the GNU General Public License as published by
  15. # the Free Software Foundation; either version 2 of the License, or
  16. # (at your option) any later version.
  17. #
  18. # This program is distributed in the hope that it will be useful,
  19. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. # GNU General Public License for more details.
  22. #
  23. # You should have received a copy of the GNU General Public License
  24. # along with this program; if not, write to the Free Software
  25. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  26. #
  27. #=====================================================================
  28. # Set the following variables to your system needs
  29. # (Detailed instructions below variables)
  30. #=====================================================================
  31. # Username to access the PostgreSQL server e.g. dbuser
  32. USERNAME=postgres
  33. # Password
  34. # create a file $HOME/.pgpass containing a line like this
  35. # hostname:*:*:dbuser:dbpass
  36. # replace hostname with the value of DBHOST and postgres with
  37. # the value of USERNAME
  38. # Host name (or IP address) of PostgreSQL server e.g localhost
  39. DBHOST=localhost
  40. # List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
  41. DBNAMES="all"
  42. # Backup directory location e.g /backups
  43. BACKUPDIR="/backups"
  44. # Mail setup
  45. # What would you like to be mailed to you?
  46. # - log : send only log file
  47. # - files : send log file and sql files as attachments (see docs)
  48. # - stdout : will simply output the log to the screen if run manually.
  49. # - quiet : Only send logs if an error occurs to the MAILADDR.
  50. MAILCONTENT="stdout"
  51. # Set the maximum allowed email size in k. (4000 = approx 5MB email [see docs])
  52. MAXATTSIZE="4000"
  53. # Email Address to send mail to? (user@domain.com)
  54. MAILADDR="user@domain.com"
  55. # ============================================================
  56. # === ADVANCED OPTIONS ( Read the doc's below for details )===
  57. #=============================================================
  58. # List of DBBNAMES for Monthly Backups.
  59. MDBNAMES="template1 $DBNAMES"
  60. # List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes)
  61. DBEXCLUDE=""
  62. # Include CREATE DATABASE in backup?
  63. CREATE_DATABASE=yes
  64. # Separate backup directory and file for each DB? (yes or no)
  65. SEPDIR=yes
  66. # Which day do you want weekly backups? (1 to 7 where 1 is Monday)
  67. DOWEEKLY=6
  68. # Choose Compression type. (gzip or bzip2)
  69. COMP=gzip
  70. # Compress communications between backup server and PostgreSQL server?
  71. # set compression level from 0 to 9 (0 means no compression)
  72. COMMCOMP=0
  73. # Additionally keep a copy of the most recent backup in a seperate directory.
  74. LATEST=no
  75. # OPT string for use with pg_dump ( see man pg_dump )
  76. OPT=""
  77. # Command to run before backups (uncomment to use)
  78. #PREBACKUP="/etc/postgresql-backup-pre"
  79. # Command run after backups (uncomment to use)
  80. #POSTBACKUP="/etc/postgresql-backup-post"
  81. # ===============================
  82. # === Debian specific options ===
  83. #================================
  84. if [ -f /etc/default/autopostgresqlbackup ]; then
  85. . /etc/default/autopostgresqlbackup
  86. fi
  87. #=====================================================================
  88. # Options documentation
  89. #=====================================================================
  90. # Set USERNAME and PASSWORD of a user that has at least SELECT permission
  91. # to ALL databases.
  92. #
  93. # Set the DBHOST option to the server you wish to backup, leave the
  94. # default to backup "this server".(to backup multiple servers make
  95. # copies of this file and set the options for that server)
  96. #
  97. # Put in the list of DBNAMES(Databases)to be backed up. If you would like
  98. # to backup ALL DBs on the server set DBNAMES="all".(if set to "all" then
  99. # any new DBs will automatically be backed up without needing to modify
  100. # this backup script when a new DB is created).
  101. #
  102. # If the DB you want to backup has a space in the name replace the space
  103. # with a % e.g. "data base" will become "data%base"
  104. # NOTE: Spaces in DB names may not work correctly when SEPDIR=no.
  105. #
  106. # You can change the backup storage location from /backups to anything
  107. # you like by using the BACKUPDIR setting..
  108. #
  109. # The MAILCONTENT and MAILADDR options and pretty self explanitory, use
  110. # these to have the backup log mailed to you at any email address or multiple
  111. # email addresses in a space seperated list.
  112. # (If you set mail content to "log" you will require access to the "mail" program
  113. # on your server. If you set this to "files" you will have to have mutt installed
  114. # on your server. If you set it to "stdout" it will log to the screen if run from
  115. # the console or to the cron job owner if run through cron. If you set it to "quiet"
  116. # logs will only be mailed if there are errors reported. )
  117. #
  118. # MAXATTSIZE sets the largest allowed email attachments total (all backup files) you
  119. # want the script to send. This is the size before it is encoded to be sent as an email
  120. # so if your mail server will allow a maximum mail size of 5MB I would suggest setting
  121. # MAXATTSIZE to be 25% smaller than that so a setting of 4000 would probably be fine.
  122. #
  123. # Finally copy autopostgresqlbackup.sh to anywhere on your server and make sure
  124. # to set executable permission. You can also copy the script to
  125. # /etc/cron.daily to have it execute automatically every night or simply
  126. # place a symlink in /etc/cron.daily to the file if you wish to keep it
  127. # somwhere else.
  128. # NOTE:On Debian copy the file with no extention for it to be run
  129. # by cron e.g just name the file "autopostgresqlbackup"
  130. #
  131. # Thats it..
  132. #
  133. #
  134. # === Advanced options doc's ===
  135. #
  136. # The list of MDBNAMES is the DB's to be backed up only monthly. You should
  137. # always include "template1" in this list to backup the default database
  138. # template used to create new databases.
  139. # NOTE: If DBNAMES="all" then MDBNAMES has no effect as all DBs will be backed
  140. # up anyway.
  141. #
  142. # If you set DBNAMES="all" you can configure the option DBEXCLUDE. Other
  143. # wise this option will not be used.
  144. # This option can be used if you want to backup all dbs, but you want
  145. # exclude some of them. (eg. a db is to big).
  146. #
  147. # Set CREATE_DATABASE to "yes" (the default) if you want your SQL-Dump to create
  148. # a database with the same name as the original database when restoring.
  149. # Saying "no" here will allow your to specify the database name you want to
  150. # restore your dump into, making a copy of the database by using the dump
  151. # created with autopostgresqlbackup.
  152. # NOTE: Not used if SEPDIR=no
  153. #
  154. # The SEPDIR option allows you to choose to have all DBs backed up to
  155. # a single file (fast restore of entire server in case of crash) or to
  156. # seperate directories for each DB (each DB can be restored seperately
  157. # in case of single DB corruption or loss).
  158. #
  159. # To set the day of the week that you would like the weekly backup to happen
  160. # set the DOWEEKLY setting, this can be a value from 1 to 7 where 1 is Monday,
  161. # The default is 6 which means that weekly backups are done on a Saturday.
  162. #
  163. # COMP is used to choose the copmression used, options are gzip or bzip2.
  164. # bzip2 will produce slightly smaller files but is more processor intensive so
  165. # may take longer to complete.
  166. #
  167. # COMMCOMP is used to set the compression level (from 0 to 9, 0 means no compression)
  168. # between the client and the server, so it is useful to save bandwidth when backing up
  169. # a remote PostgresSQL server over the network.
  170. #
  171. # LATEST is to store an additional copy of the latest backup to a standard
  172. # location so it can be downloaded bt thrid party scripts.
  173. #
  174. # Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands
  175. # or scripts to perform tasks either before or after the backup process.
  176. #
  177. #
  178. #=====================================================================
  179. # Backup Rotation..
  180. #=====================================================================
  181. #
  182. # Daily Backups are rotated weekly..
  183. # Weekly Backups are run by default on Saturday Morning when
  184. # cron.daily scripts are run...Can be changed with DOWEEKLY setting..
  185. # Weekly Backups are rotated on a 5 week cycle..
  186. # Monthly Backups are run on the 1st of the month..
  187. # Monthly Backups are NOT rotated automatically...
  188. # It may be a good idea to copy Monthly backups offline or to another
  189. # server..
  190. #
  191. #=====================================================================
  192. # Please Note!!
  193. #=====================================================================
  194. #
  195. # I take no resposibility for any data loss or corruption when using
  196. # this script..
  197. # This script will not help in the event of a hard drive crash. If a
  198. # copy of the backup has not be stored offline or on another PC..
  199. # You should copy your backups offline regularly for best protection.
  200. #
  201. # Happy backing up...
  202. #
  203. #=====================================================================
  204. # Restoring
  205. #=====================================================================
  206. # Firstly you will need to uncompress the backup file.
  207. # eg.
  208. # gunzip file.gz (or bunzip2 file.bz2)
  209. #
  210. # Next you will need to use the postgresql client to restore the DB from the
  211. # sql file.
  212. # eg.
  213. # psql --host dbserver --dbname database < /path/file.sql
  214. #
  215. # NOTE: Make sure you use "<" and not ">" in the above command because
  216. # you are piping the file.sql to psql and not the other way around.
  217. #
  218. # Lets hope you never have to use this.. :)
  219. #
  220. #=====================================================================
  221. # Change Log
  222. #=====================================================================
  223. #
  224. # VER 1.0 - (2005-03-25)
  225. # Initial Release - based on AutoMySQLBackup 2.2
  226. #
  227. #=====================================================================
  228. #=====================================================================
  229. #=====================================================================
  230. #
  231. # Should not need to be modified from here down!!
  232. #
  233. #=====================================================================
  234. #=====================================================================
  235. #=====================================================================
  236. PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin
  237. DATE=`date +%Y-%m-%d_%Hh%Mm` # Datestamp e.g 2002-09-21
  238. DOW=`date +%A` # Day of the week e.g. Monday
  239. DNOW=`date +%u` # Day number of the week 1 to 7 where 1 represents Monday
  240. DOM=`date +%d` # Date of the Month e.g. 27
  241. M=`date +%B` # Month e.g January
  242. W=`date +%V` # Week Number e.g 37
  243. VER=1.0 # Version Number
  244. LOGFILE=$BACKUPDIR/$DBHOST-`date +%N`.log # Logfile Name
  245. LOGERR=$BACKUPDIR/ERRORS_$DBHOST-`date +%N`.log # Logfile Name
  246. BACKUPFILES=""
  247. # Add --compress pg_dump option to $OPT
  248. if [ "$COMMCOMP" -gt 0 ];
  249. then
  250. OPT="$OPT --compress=$COMMCOMP"
  251. fi
  252. # Create required directories
  253. if [ ! -e "$BACKUPDIR" ] # Check Backup Directory exists.
  254. then
  255. mkdir -p "$BACKUPDIR"
  256. fi
  257. if [ ! -e "$BACKUPDIR/daily" ] # Check Daily Directory exists.
  258. then
  259. mkdir -p "$BACKUPDIR/daily"
  260. fi
  261. if [ ! -e "$BACKUPDIR/weekly" ] # Check Weekly Directory exists.
  262. then
  263. mkdir -p "$BACKUPDIR/weekly"
  264. fi
  265. if [ ! -e "$BACKUPDIR/monthly" ] # Check Monthly Directory exists.
  266. then
  267. mkdir -p "$BACKUPDIR/monthly"
  268. fi
  269. if [ "$LATEST" = "yes" ]
  270. then
  271. if [ ! -e "$BACKUPDIR/latest" ] # Check Latest Directory exists.
  272. then
  273. mkdir -p "$BACKUPDIR/latest"
  274. fi
  275. eval rm -f "$BACKUPDIR/latest/*"
  276. fi
  277. # IO redirection for logging.
  278. touch $LOGFILE
  279. exec 6>&1 # Link file descriptor #6 with stdout.
  280. # Saves stdout.
  281. exec > $LOGFILE # stdout replaced with file $LOGFILE.
  282. touch $LOGERR
  283. exec 7>&2 # Link file descriptor #7 with stderr.
  284. # Saves stderr.
  285. exec 2> $LOGERR # stderr replaced with file $LOGERR.
  286. # Functions
  287. # Database dump function
  288. dbdump () {
  289. touch $2
  290. chmod 600 $2
  291. pg_dump --username=$USERNAME $PGHOST $OPT $1 > $2
  292. return 0
  293. }
  294. # Compression function plus latest copy
  295. SUFFIX=""
  296. compression () {
  297. if [ "$COMP" = "gzip" ]; then
  298. gzip -f "$1"
  299. echo
  300. echo Backup Information for "$1"
  301. gzip -l "$1.gz"
  302. SUFFIX=".gz"
  303. elif [ "$COMP" = "bzip2" ]; then
  304. echo Compression information for "$1.bz2"
  305. bzip2 -f -v $1 2>&1
  306. SUFFIX=".bz2"
  307. else
  308. echo "No compression option set, check advanced settings"
  309. fi
  310. if [ "$LATEST" = "yes" ]; then
  311. cp $1$SUFFIX "$BACKUPDIR/latest/"
  312. fi
  313. return 0
  314. }
  315. # Run command before we begin
  316. if [ "$PREBACKUP" ]
  317. then
  318. echo ======================================================================
  319. echo "Prebackup command output."
  320. echo
  321. eval $PREBACKUP
  322. echo
  323. echo ======================================================================
  324. echo
  325. fi
  326. if [ "$SEPDIR" = "yes" ]; then # Check if CREATE DATABSE should be included in Dump
  327. if [ "$CREATE_DATABASE" = "no" ]; then
  328. OPT="$OPT"
  329. else
  330. OPT="$OPT --create"
  331. fi
  332. else
  333. OPT="$OPT"
  334. fi
  335. # Hostname for LOG information
  336. if [ "$DBHOST" = "localhost" ]; then
  337. HOST=`hostname`
  338. PGHOST=""
  339. else
  340. HOST=$DBHOST
  341. PGHOST="-h $DBHOST"
  342. fi
  343. # If backing up all DBs on the server
  344. if [ "$DBNAMES" = "all" ]; then
  345. DBNAMES="`psql -U $USERNAME $PGHOST -l -A -F: | sed -ne "/:/ { /Name:Owner/d; /template0/d; s/:.*$//; p }"`"
  346. # If DBs are excluded
  347. for exclude in $DBEXCLUDE
  348. do
  349. DBNAMES=`echo $DBNAMES | sed "s/\b$exclude\b//g"`
  350. done
  351. MDBNAMES=$DBNAMES
  352. fi
  353. echo ======================================================================
  354. echo AutoPostgreSQLBackup VER $VER
  355. echo http://autopgsqlbackup.frozenpc.net/
  356. echo
  357. echo Backup of Database Server - $HOST
  358. echo ======================================================================
  359. # Test is seperate DB backups are required
  360. if [ "$SEPDIR" = "yes" ]; then
  361. echo Backup Start Time `date`
  362. echo ======================================================================
  363. # Monthly Full Backup of all Databases
  364. if [ "$DOM" = "01" ]; then
  365. for MDB in $MDBNAMES
  366. do
  367. # Prepare $DB for using
  368. MDB="`echo $MDB | sed 's/%/ /g'`"
  369. if [ ! -e "$BACKUPDIR/monthly/$MDB" ] # Check Monthly DB Directory exists.
  370. then
  371. mkdir -p "$BACKUPDIR/monthly/$MDB"
  372. fi
  373. echo Monthly Backup of $MDB...
  374. dbdump "$MDB" "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
  375. compression "$BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql"
  376. BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$MDB/${MDB}_$DATE.$M.$MDB.sql$SUFFIX"
  377. echo ----------------------------------------------------------------------
  378. done
  379. fi
  380. for DB in $DBNAMES
  381. do
  382. # Prepare $DB for using
  383. DB="`echo $DB | sed 's/%/ /g'`"
  384. # Create Seperate directory for each DB
  385. if [ ! -e "$BACKUPDIR/daily/$DB" ] # Check Daily DB Directory exists.
  386. then
  387. mkdir -p "$BACKUPDIR/daily/$DB"
  388. fi
  389. if [ ! -e "$BACKUPDIR/weekly/$DB" ] # Check Weekly DB Directory exists.
  390. then
  391. mkdir -p "$BACKUPDIR/weekly/$DB"
  392. fi
  393. # Weekly Backup
  394. if [ "$DNOW" = "$DOWEEKLY" ]; then
  395. echo Weekly Backup of Database \( $DB \)
  396. echo Rotating 5 weeks Backups...
  397. if [ "$W" -le 05 ];then
  398. REMW=`expr 48 + $W`
  399. elif [ "$W" -lt 15 ];then
  400. REMW=0`expr $W - 5`
  401. else
  402. REMW=`expr $W - 5`
  403. fi
  404. eval rm -fv "$BACKUPDIR/weekly/$DB/${DB}_week.$REMW.*"
  405. echo
  406. dbdump "$DB" "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
  407. compression "$BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql"
  408. BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/$DB/${DB}_week.$W.$DATE.sql$SUFFIX"
  409. echo ----------------------------------------------------------------------
  410. # Daily Backup
  411. else
  412. echo Daily Backup of Database \( $DB \)
  413. echo Rotating last weeks Backup...
  414. eval rm -fv "$BACKUPDIR/daily/$DB/*.$DOW.sql.*"
  415. echo
  416. dbdump "$DB" "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
  417. compression "$BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql"
  418. BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DB/${DB}_$DATE.$DOW.sql$SUFFIX"
  419. echo ----------------------------------------------------------------------
  420. fi
  421. done
  422. echo Backup End `date`
  423. echo ======================================================================
  424. else # One backup file for all DBs
  425. echo Backup Start `date`
  426. echo ======================================================================
  427. # Monthly Full Backup of all Databases
  428. if [ "$DOM" = "01" ]; then
  429. echo Monthly full Backup of \( $MDBNAMES \)...
  430. dbdump "$MDBNAMES" "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
  431. compression "$BACKUPDIR/monthly/$DATE.$M.all-databases.sql"
  432. BACKUPFILES="$BACKUPFILES $BACKUPDIR/monthly/$DATE.$M.all-databases.sql$SUFFIX"
  433. echo ----------------------------------------------------------------------
  434. fi
  435. # Weekly Backup
  436. if [ "$DNOW" = "$DOWEEKLY" ]; then
  437. echo Weekly Backup of Databases \( $DBNAMES \)
  438. echo
  439. echo Rotating 5 weeks Backups...
  440. if [ "$W" -le 05 ];then
  441. REMW=`expr 48 + $W`
  442. elif [ "$W" -lt 15 ];then
  443. REMW=0`expr $W - 5`
  444. else
  445. REMW=`expr $W - 5`
  446. fi
  447. eval rm -fv "$BACKUPDIR/weekly/week.$REMW.*"
  448. echo
  449. dbdump "$DBNAMES" "$BACKUPDIR/weekly/week.$W.$DATE.sql"
  450. compression "$BACKUPDIR/weekly/week.$W.$DATE.sql"
  451. BACKUPFILES="$BACKUPFILES $BACKUPDIR/weekly/week.$W.$DATE.sql$SUFFIX"
  452. echo ----------------------------------------------------------------------
  453. # Daily Backup
  454. else
  455. echo Daily Backup of Databases \( $DBNAMES \)
  456. echo
  457. echo Rotating last weeks Backup...
  458. eval rm -fv "$BACKUPDIR/daily/*.$DOW.sql.*"
  459. echo
  460. dbdump "$DBNAMES" "$BACKUPDIR/daily/$DATE.$DOW.sql"
  461. compression "$BACKUPDIR/daily/$DATE.$DOW.sql"
  462. BACKUPFILES="$BACKUPFILES $BACKUPDIR/daily/$DATE.$DOW.sql$SUFFIX"
  463. echo ----------------------------------------------------------------------
  464. fi
  465. echo Backup End Time `date`
  466. echo ======================================================================
  467. fi
  468. echo Total disk space used for backup storage..
  469. echo Size - Location
  470. echo `du -hs "$BACKUPDIR"`
  471. echo
  472. # Run command when we're done
  473. if [ "$POSTBACKUP" ]
  474. then
  475. echo ======================================================================
  476. echo "Postbackup command output."
  477. echo
  478. eval $POSTBACKUP
  479. echo
  480. echo ======================================================================
  481. fi
  482. #Clean up IO redirection
  483. exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
  484. exec 1>&7 7>&- # Restore stdout and close file descriptor #7.
  485. if [ "$MAILCONTENT" = "files" ]
  486. then
  487. if [ -s "$LOGERR" ]
  488. then
  489. # Include error log if is larger than zero.
  490. BACKUPFILES="$BACKUPFILES $LOGERR"
  491. ERRORNOTE="WARNING: Error Reported - "
  492. fi
  493. #Get backup size
  494. ATTSIZE=`du -c $BACKUPFILES | grep "[[:digit:][:space:]]total$" |sed s/\s*total//`
  495. if [ $MAXATTSIZE -ge $ATTSIZE ]
  496. then
  497. BACKUPFILES=`echo "$BACKUPFILES" | sed -e "s# # -a #g"` #enable multiple attachments
  498. mutt -s "PostgreSQL Backup Log and SQL Files for $HOST - $DATE" $BACKUPFILES $MAILADDR < $LOGFILE #send via mutt
  499. else
  500. cat "$LOGFILE" | mail -s "WARNING! - PostgreSQL Backup exceeds set maximum attachment size on $HOST - $DATE" $MAILADDR
  501. fi
  502. elif [ "$MAILCONTENT" = "log" ]
  503. then
  504. cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $HOST - $DATE" $MAILADDR
  505. if [ -s "$LOGERR" ]
  506. then
  507. cat "$LOGERR" | mail -s "ERRORS REPORTED: PostgreSQL Backup error Log for $HOST - $DATE" $MAILADDR
  508. fi
  509. elif [ "$MAILCONTENT" = "quiet" ]
  510. then
  511. if [ -s "$LOGERR" ]
  512. then
  513. cat "$LOGERR" | mail -s "ERRORS REPORTED: PostgreSQL Backup error Log for $HOST - $DATE" $MAILADDR
  514. cat "$LOGFILE" | mail -s "PostgreSQL Backup Log for $HOST - $DATE" $MAILADDR
  515. fi
  516. else
  517. if [ -s "$LOGERR" ]
  518. then
  519. cat "$LOGFILE"
  520. echo
  521. echo "###### WARNING ######"
  522. echo "Errors reported during AutoPostgreSQLBackup execution.. Backup failed"
  523. echo "Error log below.."
  524. cat "$LOGERR"
  525. else
  526. cat "$LOGFILE"
  527. fi
  528. fi
  529. if [ -s "$LOGERR" ]
  530. then
  531. STATUS=1
  532. else
  533. STATUS=0
  534. fi
  535. # Clean up Logfile
  536. eval rm -f "$LOGFILE"
  537. eval rm -f "$LOGERR"
  538. exit $STATUS