autopostgresqlbackup 24 KB

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