autopostgresqlbackup 22 KB

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