autopostgresqlbackup 20 KB

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