provisioning tool for building opinionated architecture
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

666 lines
24 KiB

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