autopostgresqlbackup 18 KB

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