autopostgresqlbackup 18 KB

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