autopostgresqlbackup 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650
  1. #!/bin/bash
  2. # {{{ License and Copyright
  3. # PostgreSQL Backup Script
  4. # https://github.com/k0lter/autopostgresqlbackup
  5. # Copyright (c) 2005 Aaron Axelsen <axelseaa@amadmax.com>
  6. # 2005 Friedrich Lobenstock <fl@fl.priv.at>
  7. # 2013-2022 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. # {{{ Variables
  24. # Username to access the PostgreSQL server e.g. dbuser
  25. USERNAME=postgres
  26. # Password
  27. # create a file ${HOME}/.pgpass containing a line like this
  28. # hostname:*:*:dbuser:dbpass
  29. # replace hostname with the value of DBHOST and postgres with
  30. # the value of USERNAME
  31. # Host name (or IP address) of PostgreSQL server e.g localhost
  32. DBHOST=localhost
  33. # Port of PostgreSQL server e.g 5432 (only used if DBHOST != localhost)
  34. DBPORT=5432
  35. # List of DBNAMES for Daily/Weekly Backup e.g. "DB1 DB2 DB3"
  36. DBNAMES="all"
  37. # pseudo database name used to dump global objects (users, roles, tablespaces)
  38. GLOBALS_OBJECTS="postgres_globals"
  39. # Backup directory location e.g /backups
  40. BACKUPDIR="/backups"
  41. # Email Address to send mail to? (user@domain.com)
  42. MAILADDR="user@domain.com"
  43. # ============================================================
  44. # === ADVANCED OPTIONS ( Read the doc's below for details )===
  45. #=============================================================
  46. # List of DBNAMES to EXLUCDE if DBNAMES are set to all (must be in " quotes)
  47. DBEXCLUDE=""
  48. # Include CREATE DATABASE in backup?
  49. CREATE_DATABASE=yes
  50. # Which day do you want weekly backups? (1 to 7 where 1 is Monday)
  51. # When set to 0, weekly backups are disabled
  52. DOWEEKLY=6
  53. # Which day do you want monthly backups? (default is 1, first day of the month)
  54. # When set to 0, monthly backups are disabled
  55. DOMONTHLY=1
  56. # Backup retention count for daily backups
  57. # Default is 14 days
  58. BRDAILY=14
  59. # Backup retention count for weekly backups
  60. # Default is 5 weeks
  61. BRWEEKLY=5
  62. # Backup retention count for monthly backups
  63. # Default is 12 months
  64. BRMONTHLY=12
  65. # Choose Compression type. (gzip, pigz, bzip2, xz or zstd)
  66. COMP=gzip
  67. # Compression options
  68. COMP_OPTS=
  69. # OPT string for use with pg_dump (see man pg_dump)
  70. OPT=""
  71. # Backup files extension
  72. EXT="sql"
  73. # Backup files permission
  74. PERM=600
  75. # Encryption settings
  76. # (inspired by http://blog.altudov.com/2010/09/27/using-openssl-for-asymmetric-encryption-of-backups/)
  77. #
  78. # It is recommended to backup into a staging directory, and then use the
  79. # POSTBACKUP script to sync the encrypted files to the desired location.
  80. #
  81. # Encryption uses private/public keys. You can generate the key pairs like the following:
  82. # openssl req -x509 -nodes -days 100000 -newkey rsa:2048 -keyout backup.key -out backup.crt -subj '/'
  83. #
  84. # Decryption:
  85. # openssl smime -decrypt -in backup.sql.gz.enc -binary -inform DEM -inkey backup.key -out backup.sql.gz
  86. # Enable encryption
  87. ENCRYPTION=no
  88. # Encryption public key (path to the key)
  89. ENCRYPTION_PUBLIC_KEY=""
  90. # Encryption Cipher (see enc manpage)
  91. ENCRYPTION_CIPHER="aes256"
  92. # Suffix for encyrpted files
  93. ENCRYPTION_SUFFIX=".enc"
  94. # Command to run before backups (uncomment to use)
  95. #PREBACKUP="/etc/postgresql-backup-pre"
  96. # Command run after backups (uncomment to use)
  97. #POSTBACKUP="/etc/postgresql-backup-post"
  98. # }}}
  99. # {{{ OS Specific
  100. #=====================================================================
  101. # Debian specific options ===
  102. #=====================================================================
  103. if [ -f /etc/default/autopostgresqlbackup ]; then
  104. # shellcheck source=/dev/null
  105. . /etc/default/autopostgresqlbackup
  106. fi
  107. # }}}
  108. # {{{ Documentation
  109. #=====================================================================
  110. # Options documentation
  111. #=====================================================================
  112. # Set USERNAME and PASSWORD of a user that has at least SELECT permission to
  113. # ALL databases.
  114. #
  115. # Set the DBHOST option to the server you wish to backup, leave the default to
  116. # backup "this server". To backup multiple servers make copies of this file and
  117. # set the options for that server.
  118. #
  119. # Put in the list of DBNAMES (Databases) to be backed up. If you would like to
  120. # backup ALL DBs on the server set DBNAMES="all". If set to "all" then any new
  121. # DBs will automatically be backed up without needing to modify this backup
  122. # script when a new DB is created.
  123. #
  124. # If the DB you want to backup has a space in the name replace the space with a
  125. # % e.g. "data base" will become "data%base"
  126. #
  127. # You can change the backup storage location to anything you like by using the
  128. # BACKUPDIR setting.
  129. #
  130. # === Advanced options doc's ===
  131. #
  132. # If you set DBNAMES="all" you can configure the option DBEXCLUDE. Other wise
  133. # this option will not be used. This option can be used if you want to backup
  134. # all dbs, but you want exclude some of them. (eg. if a db is to big).
  135. #
  136. # Set CREATE_DATABASE to "yes" (the default) if you want your SQL-Dump to
  137. # create a database with the same name as the original database when restoring.
  138. # Saying "no" here will allow your to specify the database name you want to
  139. # restore your dump into, making a copy of the database by using the dump
  140. # created with autopostgresqlbackup.
  141. #
  142. # Use PREBACKUP and POSTBACKUP to specify Per and Post backup commands
  143. # or scripts to perform tasks either before or after the backup process.
  144. #
  145. #=====================================================================
  146. # Backup Rotation..
  147. #=====================================================================
  148. #
  149. # Rotation is configurable for each period:
  150. # - daily (max $BRDAILY backups are keeped)
  151. # - weekly (max $BRWEEKLY backups are keeped)
  152. # - monthy (max $BRMONTHLY backups are keeped)
  153. #
  154. # }}}
  155. # {{{ Defaults
  156. PATH=/usr/local/bin:/usr/bin:/bin:/usr/local/postgres/bin:/usr/local/pgsql/bin
  157. NAME="AutoPostgreSQLBackup" # Script name
  158. VERSION="2.0" # Version Number
  159. DATE="$(date '+%Y-%m-%d_%Hh%Mm')" # Datestamp e.g 2002-09-21
  160. DNOW="$(date '+%u')" # Day number of the week 1 to 7 where 1 represents Monday
  161. DNOM="$(date '+%d')" # Date of the Month e.g. 27
  162. LOG_DIR="${BACKUPDIR}" # Directory where the main log is saved
  163. # Fix day of month (left padding with 0)
  164. DOMONTHLY="$(echo "${DOMONTHLY}" | sed -r 's/^[0-9]$/0\0/')"
  165. # Using a shared memory filesystem (if available) to avoid
  166. # issues when there is no left space on backup storage
  167. if [ -w "/dev/shm" ]; then
  168. LOG_DIR="/dev/shm"
  169. fi
  170. LOG_FILE="${LOG_DIR}/${NAME}_${DBHOST//\//_}-$(date '+%Y-%m-%d_%Hh%Mm').log"
  171. # Debug mode
  172. DEBUG="no"
  173. # pg_dump options
  174. if [ -n "${OPT}" ]; then
  175. IFS=" " read -r -a PG_OPTIONS <<< "${OPT}"
  176. else
  177. PG_OPTIONS=()
  178. fi
  179. # Create required directories
  180. if [ ! -e "${BACKUPDIR}" ]; then # Check Backup Directory exists.
  181. mkdir -p "${BACKUPDIR}"
  182. fi
  183. if [ ! -e "${BACKUPDIR}/daily" ]; then # Check Daily Directory exists.
  184. mkdir -p "${BACKUPDIR}/daily"
  185. fi
  186. if [ ! -e "${BACKUPDIR}/weekly" ]; then # Check Weekly Directory exists.
  187. mkdir -p "${BACKUPDIR}/weekly"
  188. fi
  189. if [ ! -e "${BACKUPDIR}/monthly" ]; then # Check Monthly Directory exists.
  190. mkdir -p "${BACKUPDIR}/monthly"
  191. fi
  192. # Hostname for LOG information and
  193. # pg_dump{,all} connection settings
  194. if [ "${DBHOST}" = "localhost" ]; then
  195. HOST="$(hostname --fqdn)"
  196. PG_CONN=()
  197. else
  198. HOST="${DBHOST}:${DBPORT}"
  199. PG_CONN=(--host "${DBHOST}" --port "${DBPORT}")
  200. fi
  201. if [ -n "${USERNAME}" ]; then
  202. PG_CONN+=(--username "${USERNAME}")
  203. fi
  204. # }}}
  205. # {{{ log{,ger,_info,_debug,_warn,_error}()
  206. logger() {
  207. local fd line severity reset color
  208. fd="${1}"
  209. severity="${2}"
  210. reset=
  211. color=
  212. if [ -n "${TERM}" ]; then
  213. reset="\e[0m"
  214. case "${severity}" in
  215. error)
  216. color="\e[0;91m"
  217. ;;
  218. warn)
  219. color="\e[0;93m"
  220. ;;
  221. debug)
  222. color="\e[0;96m"
  223. ;;
  224. *)
  225. color="\e[0;94m"
  226. ;;
  227. esac
  228. fi
  229. while IFS= read -r line ; do
  230. printf "%s|%s|%s\n" "${fd}" "${severity}" "${line}" >> "${LOG_FILE}"
  231. if [ "${DEBUG}" = "yes" ]; then
  232. if [ "${fd}" = "out" ]; then
  233. printf "${color}%6s${reset}|%s\n" "${severity}" "${line}" >&6
  234. elif [ "${fd}" = "err" ]; then
  235. printf "${color}%6s${reset}|%s\n" "${severity}" "${line}" >&7
  236. fi
  237. fi
  238. done
  239. }
  240. log() {
  241. echo "$@" | logger "out" ""
  242. }
  243. log_debug() {
  244. echo "$@" | logger "out" "debug"
  245. }
  246. log_info() {
  247. echo "$@" | logger "out" "info"
  248. }
  249. log_error() {
  250. echo "$@" | logger "err" "error"
  251. }
  252. log_warn() {
  253. echo "$@" | logger "err" "warn"
  254. }
  255. # }}}
  256. # {{{ dblist()
  257. dblist () {
  258. local cmd_prog cmd_args raw_dblist dblist dbexcl databases
  259. cmd_prog="psql"
  260. cmd_args=(-t -l -A -F:)
  261. if [ "${#PG_CONN[@]}" -gt 0 ]; then
  262. cmd_args+=("${PG_CONN[@]}")
  263. fi
  264. log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
  265. raw_dblist=$(
  266. if [ -n "${SU_USERNAME}" ]; then
  267. su - "${SU_USERNAME}" -l -c "${cmd_prog} ${cmd_args[*]}"
  268. else
  269. "${cmd_prog}" "${cmd_args[@]}"
  270. fi
  271. )
  272. read -r -a dblist <<< "$(
  273. printf "%s" "${raw_dblist}" | \
  274. sed -r -n 's/^([^:]+):.+$/\1/p' | \
  275. tr '\n' ' '
  276. )"
  277. log_debug "Automatically found databases: ${dblist[*]}"
  278. if [ -n "${DBEXCLUDE}" ]; then
  279. IFS=" " read -r -a dbexcl <<< "${DBEXCLUDE}"
  280. else
  281. dbexcl=()
  282. fi
  283. dbexcl+=(template0)
  284. log_debug "Excluded databases: ${dbexcl[*]}"
  285. mapfile -t databases < <(
  286. comm -23 \
  287. <(IFS=$'\n'; echo "${dblist[*]}" | sort) \
  288. <(IFS=$'\n'; echo "${dbexcl[*]}" | sort) \
  289. )
  290. databases+=("${GLOBALS_OBJECTS}")
  291. log_debug "Database(s) to be backuped: ${databases[*]}"
  292. printf "%s " "${databases[@]}"
  293. }
  294. # }}}
  295. # {{{ dbdump()
  296. dbdump () {
  297. local db cmd_prog cmd_args pg_args
  298. db="${1}"
  299. pg_args="${PG_OPTIONS[*]}"
  300. if [ "${db}" = "${GLOBALS_OBJECTS}" ]; then
  301. cmd_prog="pg_dumpall"
  302. cmd_args=(--globals-only)
  303. else
  304. cmd_prog="pg_dump"
  305. cmd_args=("${DB}")
  306. if [ "${CREATE_DATABASE}" = "yes" ]; then
  307. pg_args+=(--create)
  308. fi
  309. fi
  310. if [ "${#PG_CONN[@]}" -gt 0 ]; then
  311. cmd_args+=("${PG_CONN[@]}")
  312. fi
  313. if [ "${#pg_args[@]}" -gt 0 ]; then
  314. cmd_args+=("${pg_args[@]}")
  315. fi
  316. log_debug "Running command: ${cmd_prog} ${cmd_args[*]}"
  317. if [ -n "${SU_USERNAME}" ]; then
  318. su - "${SU_USERNAME}" -l -c "${cmd_prog} ${cmd_args[*]}"
  319. else
  320. "${cmd_prog}" "${cmd_args[@]}"
  321. fi
  322. }
  323. # }}}
  324. # {{{ encryption()
  325. encryption() {
  326. log_debug "Encrypting using cypher ${ENCRYPTION_CIPHER} and public key ${ENCRYPTION_PUBLIC_KEY}"
  327. openssl smime -encrypt -${ENCRYPTION_CIPHER} -binary -outform DEM "${ENCRYPTION_PUBLIC_KEY}" 2>&7
  328. }
  329. # }}}
  330. # {{{ compression()
  331. compression () {
  332. if [ -n "${COMP_OPTS}" ]; then
  333. IFS=" " read -r -a comp_args <<< "${COMP_OPTS}"
  334. log_debug "Compressing using '${COMP} ${comp_args[*]}'"
  335. "${COMP}" "${comp_args[@]}" 2>&7
  336. else
  337. log_debug "Compressing using '${COMP}'"
  338. "${COMP}" 2>&7
  339. fi
  340. }
  341. # }}}
  342. # {{{ dump()
  343. dump() {
  344. local db_name dump_file comp_ext
  345. db_name="${1}"
  346. dump_file="${2}"
  347. if [ -n "${COMP}" ]; then
  348. comp_ext=".comp"
  349. case "${COMP}" in
  350. gzip|pigz)
  351. comp_ext=".gz"
  352. ;;
  353. bzip2)
  354. comp_ext=".bz2"
  355. ;;
  356. xz)
  357. comp_ext=".xz"
  358. ;;
  359. zstd)
  360. comp_ext=".zstd"
  361. ;;
  362. esac
  363. dump_file="${dump_file}.${comp_ext}"
  364. fi
  365. if [ "${ENCRYPTION}" = "yes" ]; then
  366. dump_file="${dump_file}${ENCRYPTION_SUFFIX}"
  367. fi
  368. if [ -n "${COMP}" ] && [ "${ENCRYPTION}" = "yes" ]; then
  369. log_debug "Dumping (${db_name}) +compress +encrypt to '${dump_file}'"
  370. dbdump "${db_name}" | compression | encryption > "${dump_file}"
  371. elif [ -n "${COMP}" ]; then
  372. log_debug "Dumping (${db_name}) +compress to '${dump_file}'"
  373. dbdump "${db_name}" | compression > "${dump_file}"
  374. elif [ "${ENCRYPTION}" = "yes" ]; then
  375. log_debug "Dumping (${db_name}) +encrypt to '${dump_file}'"
  376. dbdump "${db_name}" | encryption > "${dump_file}"
  377. else
  378. log_debug "Dumping (${db_name}) to '${dump_file}'"
  379. dbdump "${db_name}" > "${dump_file}"
  380. fi
  381. if [ -f "${dump_file}" ]; then
  382. log_debug "Fixing permissions (${PERM}) on '${dump_file}'"
  383. chmod "${PERM}" "${dump_file}"
  384. if [ ! -s "${dump_file}" ]; then
  385. log_error "Something went wrong '${dump_file}' is empty (no space left on device?)"
  386. fi
  387. else
  388. log_error "Something went wrong '${dump_file}' does not exists (error during dump?)"
  389. fi
  390. }
  391. # }}}
  392. # {{{ cleanup()
  393. cleanup() {
  394. local dumpdir db when count line
  395. dumpdir="${1}"
  396. db="${2}"
  397. when="${3}"
  398. count="${4}"
  399. # Since version >= 2.0 the dump filename no longer contains the week number
  400. # or the abbreviated month name so in order to be sure to remove the older
  401. # dumps we need to sort the filename on the datetime part (YYYY-MM-DD_HHhMMm)
  402. log_info "Rotating ${count} ${when} backups..."
  403. log_debug "Looking for '${db}_*' in '${dumpdir}/${when}/${db}'"
  404. find "${dumpdir}/${when}/${db}/" -name "${db}_*" | \
  405. sed -r 's/^.+([0-9]{4}-[0-9]{2}-[0-9]{2}_[0-9]{2}h[0-9]{2}m).*$/\1 \0/' | \
  406. sort -r | \
  407. sed -r -n 's/\S+ //p' | \
  408. tail "+${count}" | \
  409. xargs -L1 rm -fv | \
  410. while IFS= read -r line ; do
  411. log_info "${line}"
  412. done
  413. }
  414. # }}}
  415. # {{{ usage()
  416. usage() {
  417. cat <<EOH
  418. USAGE: $(basename "$0") [OPTIONS]
  419. ${NAME} ${VERSION}
  420. A fully automated tool to make periodic backups of PostgreSQL databases.
  421. Options:
  422. -h Shows this help
  423. -d Run in debug mode (no mail sent)
  424. EOH
  425. }
  426. # }}}
  427. # {{{ Process command line arguments
  428. while getopts "hd" OPTION ; do
  429. case "${OPTION}" in
  430. h)
  431. usage
  432. exit 0
  433. ;;
  434. d)
  435. DEBUG="yes"
  436. ;;
  437. *)
  438. printf "Try \`%s -h\` to check the command line arguments\n" "$(basename "$0")" >&2
  439. exit 1
  440. esac
  441. done
  442. # }}}
  443. # {{{ I/O redirection(s) for logging
  444. exec 6>&1 # Link file descriptor #6 with stdout.
  445. # Saves stdout.
  446. exec 7>&2 # Link file descriptor #7 with stderr.
  447. # Saves stderr.
  448. exec > >( logger "out")
  449. exec 2> >( logger "err")
  450. # }}}
  451. # {{{ PreBackup
  452. # Run command before we begin
  453. if [ -n "${PREBACKUP}" ]; then
  454. log_info "Prebackup command output:"
  455. ${PREBACKUP} | \
  456. while IFS= read -r line ; do
  457. log " ${line}"
  458. done
  459. fi
  460. # }}}
  461. # {{{ main()
  462. log_info "${NAME} version ${VERSION}"
  463. log_info "Backup of Database Server - ${HOST}"
  464. if [ -n "${COMP}" ]; then
  465. if ! command -v "${COMP}" >/dev/null ; then
  466. log_warn "Disabling compression, '${COMP}' command not found"
  467. unset COMP
  468. fi
  469. fi
  470. if [ "${ENCRYPTION}" = "yes" ] && ! command -v "openssl" >/dev/null ; then
  471. log_warn "Disabling encryption, 'openssl' command not found"
  472. ENCRYPTION="no"
  473. fi
  474. log_info "Backup Start: $(date)"
  475. if [ "${DNOM}" = "${DOMONTHLY}" ]; then
  476. period="monthly"
  477. rotate="${BRMONTHLY}"
  478. elif [ "${DNOW}" = "${DOWEEKLY}" ]; then
  479. period="weekly"
  480. rotate="${BRWEEKLY}"
  481. else
  482. period="daily"
  483. rotate="${BRDAILY}"
  484. fi
  485. # If backing up all DBs on the server
  486. if [ "${DBNAMES}" = "all" ]; then
  487. DBNAMES="$(dblist)"
  488. fi
  489. for db in ${DBNAMES} ; do
  490. db="${db//%/ / }"
  491. log_info "Backup of Database (${period}) '${db}'"
  492. backupdbdir="${BACKUPDIR}/${period}/${db}"
  493. if [ ! -e "${backupdbdir}" ]; then
  494. log_debug "Creating Backup DB directory '${backupdbdir}'"
  495. mkdir -p "${backupdbdir}"
  496. fi
  497. cleanup "${BACKUPDIR}" "${db}" "${period}" "${rotate}"
  498. backupfile="${backupdbdir}/${db}_${DATE}.${EXT}"
  499. dump "${db}" "${backupfile}"
  500. done
  501. log_info "Backup End: $(date)"
  502. log_info "Total disk space used for ${BACKUPDIR}: $(du -hs "${BACKUPDIR}" | cut -f1)"
  503. # }}}
  504. # {{{ PostBackup
  505. # Run command when we're done
  506. if [ -n "${POSTBACKUP}" ]; then
  507. log_info "Postbackup command output:"
  508. ${POSTBACKUP} | \
  509. while IFS= read -r line ; do
  510. log " ${line}"
  511. done
  512. fi
  513. # }}}
  514. # {{{ cleanup I/O redirections
  515. exec 1>&6 6>&- # Restore stdout and close file descriptor #6.
  516. exec 2>&7 7>&- # Restore stdout and close file descriptor #7.
  517. # }}}
  518. # {{{ Reporting
  519. if [ "${DEBUG}" = "no" ] && grep -q '^err|' "${LOG_FILE}" ; then
  520. (
  521. printf "*Errors/Warnings* (below) reported during backup on *%s*:\n\n" "${HOST}"
  522. grep '^err|' "${LOG_FILE}" | cut -d '|' -f 3- | \
  523. while IFS= read -r line ; do
  524. printf " | %s\n" "${line}"
  525. done
  526. printf "\n\nFull backup log follows:\n\n"
  527. grep -v '^...|debug|' "${LOG_FILE}" | \
  528. while IFS="|" read -r fd level line ; do
  529. if [ -n "${level}" ]; then
  530. printf "%8s| %s\n" "*${level}*" "${line}"
  531. else
  532. printf "%8s| %s\n" "" "${line}"
  533. fi
  534. done
  535. printf "\nFor more information, try to run %s in debug mode, see \`%s -h\`\n" "${NAME}" "$(basename "$0")"
  536. ) | mail -s "${NAME} - log" "${MAILADDR}"
  537. fi
  538. # }}}
  539. # {{{ Cleanup logs and exit()
  540. if [ -s "${LOGERR}" ]; then
  541. rc=1
  542. else
  543. rc=0
  544. fi
  545. # Clean up log files
  546. rm -f "${LOG_FILE}"
  547. exit ${rc}
  548. # }}}
  549. # vim: foldmethod=marker foldlevel=0 foldenable