/backup-manager

http://github.com/sukria/Backup-Manager · #! · 297 lines · 255 code · 42 blank · 0 comment · 0 complexity · 0952232730319c69e3c2cb0b3f7e604c MD5 · raw file

  1. #! /usr/bin/env bash
  2. # Copyright Š 2005-2016 The Backup Manager Authors
  3. # See the AUTHORS file for details.
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU General Public License
  7. # as published by the Free Software Foundation; either version 2
  8. # of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. # This is the main backup-manager script.
  19. #
  20. # $Revision$
  21. # $Date$
  22. # $Author$
  23. set -e
  24. RELEASE="true"
  25. VERSION="0.7.12"
  26. #Set prefix for paths
  27. BIN_PREFIX=/usr/bin
  28. LIB_PREFIX=/usr/lib
  29. VAR_PREFIX=/var/lib
  30. #Set prefix for paths
  31. BIN_PREFIX=/usr/bin
  32. LIB_PREFIX=/usr/lib
  33. VAR_PREFIX=/var/lib
  34. # All the paths we provide
  35. libdir="$LIB_PREFIX/backup-manager"
  36. vardir="$VAR_PREFIX/backup-manager"
  37. bmu="$BIN_PREFIX/backup-manager-upload"
  38. bmp="$BIN_PREFIX/backup-manager-purge"
  39. # Find which lockfile to use
  40. # If we are called by an unprivileged user, use a lockfile inside the user's home;
  41. # else, use /var/run/backup-manager.lock
  42. systemlockfile="/var/run/backup-manager.lock"
  43. userlockfile="$HOME/.backup-manager.lock"
  44. if [[ "$UID" != 0 ]]; then
  45. lockfile="$userlockfile"
  46. else
  47. lockfile="$systemlockfile"
  48. fi
  49. # Default value for logger, as we didn't read the config file yet
  50. BM_LOGGER_LEVEL="info"
  51. # Load the backup-manager's library
  52. source $libdir/externals.sh
  53. source $libdir/gettext.sh
  54. source $libdir/logger.sh
  55. source $libdir/dialog.sh
  56. source $libdir/files.sh
  57. source $libdir/md5sum.sh
  58. source $libdir/backup-methods.sh
  59. source $libdir/upload-methods.sh
  60. source $libdir/burning-methods.sh
  61. source $libdir/actions.sh
  62. source $libdir/dbus.sh
  63. debug "Libraries loaded successfuly from \"$libdir\"."
  64. # Initialize defautls values of arguments
  65. verbosedebug="false"
  66. verbose="false"
  67. version="false"
  68. warnings="true"
  69. force="false"
  70. upload="false"
  71. burn="false"
  72. help="false"
  73. md5check="false"
  74. purge="false"
  75. conffile="/etc/backup-manager.conf"
  76. # The "no" flags
  77. nopurge="false"
  78. noburn="false"
  79. noupload="false"
  80. debug "Version : $VERSION"
  81. # Set useful global variables and initial
  82. # checks
  83. bm_init_today
  84. bm_dbus_init
  85. bm_dbus_send_event "startup" "Version : $VERSION"
  86. bm_dbus_send_progress 0 "Initializing"
  87. # Catch signals for a nice exit.
  88. trap clean_exit SIGINT SIGTERM SIGKILL
  89. # Parse the command line
  90. debug "Processing the command line"
  91. while [[ $# -ge 1 ]]; do
  92. case $1 in
  93. -h|--help)
  94. usage
  95. ;;
  96. -m|--md5check)
  97. md5check="true"
  98. ;;
  99. -p|--purge)
  100. purge="true"
  101. ;;
  102. --no-purge)
  103. nopurge="true"
  104. ;;
  105. -b|--burn)
  106. burn="true"
  107. # parse the second argument as a date if
  108. # it does not begin with a dash (-).
  109. if [[ -n "$2" ]] &&
  110. [[ "${2}" == "${2#-}" ]]; then
  111. # test if the date is a valid date
  112. if [[ $(echo "$2" | grep "^[[:digit:]]\{8\}$") ]] ; then
  113. export BM__BURNING_DATE="$2"
  114. shift
  115. else
  116. error "The -b option must be followed by a valid date (YYYYMMDD)."
  117. fi
  118. fi
  119. ;;
  120. --no-burn)
  121. noburn="true"
  122. ;;
  123. -u|--upload)
  124. upload="true"
  125. ;;
  126. --no-upload)
  127. noupload="true"
  128. ;;
  129. -d|--debug)
  130. verbosedebug="true"
  131. verbose="true"
  132. ;;
  133. -v|--verbose)
  134. verbose="true"
  135. ;;
  136. --version)
  137. echo "Backup Manager $VERSION"
  138. _exit 0
  139. ;;
  140. --no-warnings)
  141. warnings="false"
  142. ;;
  143. -f|--force)
  144. force="true"
  145. ;;
  146. -c|--conffile)
  147. # in this case, $2 should be the conffile !
  148. if [[ -f $2 ]]; then
  149. conffile=$2
  150. else
  151. error "The -c option must be followed by an existing filename."
  152. usage
  153. fi
  154. # we shift here to avoid processing the file path
  155. shift
  156. ;;
  157. *)
  158. echo "Unknown option $1"
  159. usage
  160. break
  161. ;;
  162. esac
  163. shift
  164. done
  165. info "Backup Manager $VERSION - Copyright (c) 2004-2015 Alexis Sukrieh"
  166. # Display some more info if we're doing an action
  167. if [[ "$noupload" == "true" ]]\
  168. || [[ "$noburn" == "true" ]]\
  169. || [[ "$nopurge" == "false" ]]; then
  170. curr_time=`date +%Y-%m-%d%t%T`
  171. info "Process started at $curr_time"
  172. fi
  173. debug "Loading configuration file : \"$conffile\"."
  174. source $conffile
  175. # Override config's log level if specified by command line
  176. if [[ "$verbose" == "true" ]]; then
  177. if [[ "$verbosedebug" == "true" ]]; then
  178. BM_LOGGER_LEVEL="debug"
  179. else
  180. BM_LOGGER_LEVEL="info"
  181. fi
  182. fi
  183. if [[ "$warnings" == "false" ]]; then
  184. BM_LOGGER_LEVEL="error"
  185. fi
  186. # Sanitize will try to find deprecated vartiables,
  187. debug "Sanitizing the configuration file."
  188. source $libdir/sanitize.sh
  189. debug "Initializing environment"
  190. bm_init_env
  191. debug "Checking if logger is available"
  192. check_logger
  193. debug "Getting lock"
  194. get_lock
  195. check_filetypes
  196. # For security reasons, change the umask
  197. # for the backup-manager session.
  198. # Every file created by the process will be -rw------
  199. BM_UMASK=$(umask)
  200. umask 0077
  201. debug "Running pre-command"
  202. exec_pre_command || error "Unable to exec the pre-command"
  203. create_directories
  204. if [[ "$upload" == "true" ]]; then
  205. debug "Running the upload methods"
  206. upload_files
  207. _exit 0
  208. fi
  209. if [[ "$burn" == "true" ]]; then
  210. debug "Running the burning methods"
  211. burn_files
  212. _exit 0
  213. fi
  214. if [[ "$md5check" == "true" ]]; then
  215. debug "Runing the MD5 checks"
  216. check_cdrom_md5_sums
  217. _exit 0
  218. fi
  219. if [[ "$purge" == "true" ]]; then
  220. debug "Purging the repository"
  221. clean_repositories
  222. _exit 0
  223. fi
  224. # Default process : doing everything unless --no-flags
  225. # are given.
  226. if [[ "$nopurge" != "true" ]]; then
  227. debug "Purging the repository"
  228. bm_dbus_send_progress 10 "Cleaning repositories"
  229. clean_repositories
  230. fi
  231. debug "Building archives"
  232. bm_dbus_send_progress 20 "Building archives"
  233. make_archives
  234. if [[ "$noupload" != "true" ]]; then
  235. debug "Running the upload methods"
  236. bm_dbus_send_progress 60 "Uploading backups"
  237. upload_files
  238. fi
  239. if [[ "$noburn" != "true" ]]; then
  240. debug "Running the burning methods"
  241. bm_dbus_send_progress 80 "Burning backups"
  242. burn_files
  243. fi
  244. debug "Running post-command"
  245. bm_dbus_send_progress 90 "Cleaning up"
  246. exec_post_command || error "Unable to exec post-command."
  247. debug "Releasing lock"
  248. release_lock
  249. debug "Exiting"
  250. umask $BM_UMASK >/dev/null
  251. curr_time=`date +%Y-%m-%d%t%T`
  252. info "Process finished at $curr_time"
  253. bm_dbus_send_progress 100 "Finished"
  254. bm_dbus_send_event "shutdown" "0"
  255. exit 0