/lib/actions.sh

http://github.com/sukria/Backup-Manager · Shell · 223 lines · 165 code · 24 blank · 34 comment · 16 complexity · 72f1e8a5a6995db3af1f03b003b4c179 MD5 · raw file

  1. # Copyright © 2005-2016 The Backup Manager Authors
  2. #
  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. #
  19. # Main wrapper
  20. #
  21. # Loop on the backup methods
  22. function make_archives()
  23. {
  24. debug "make_archives()"
  25. for method in $BM_ARCHIVE_METHOD
  26. do
  27. case $method in
  28. mysql)
  29. backup_method_mysql "$method"
  30. ;;
  31. pgsql)
  32. backup_method_pgsql "$method"
  33. ;;
  34. tarball|tarball-incremental)
  35. backup_method_tarball "$method"
  36. ;;
  37. pipe)
  38. backup_method_pipe "$method"
  39. ;;
  40. svn)
  41. backup_method_svn "$method"
  42. ;;
  43. none|disabled)
  44. info "No backup method used."
  45. ;;
  46. *)
  47. error "No such backup method: \$BM_ARCHIVE_METHOD"
  48. ;;
  49. esac
  50. # Now make sure the md5 file is okay.
  51. if [[ -e $MD5FILE ]] &&
  52. [[ "$BM_REPOSITORY_SECURE" = "true" ]]; then
  53. chown $BM_REPOSITORY_USER:$BM_REPOSITORY_GROUP $MD5FILE ||
  54. warning "Unable to change the owner of \"\$MD5FILE\"."
  55. chmod $BM_ARCHIVE_CHMOD $MD5FILE ||
  56. warning "Unable to change file permissions of \"\$MD5FILE\"."
  57. fi
  58. done
  59. }
  60. # Loop on the upload methods
  61. function upload_files()
  62. {
  63. debug "upload_files()"
  64. for method in $BM_UPLOAD_METHOD
  65. do
  66. case $method in
  67. ftp|FTP)
  68. bm_upload_ftp
  69. ;;
  70. ssh|SSH|scp|SCP)
  71. bm_upload_ssh
  72. ;;
  73. ssh-gpg|SSH-GPG)
  74. bm_upload_ssh_gpg
  75. ;;
  76. rsync|RSYNC)
  77. bm_upload_rsync
  78. ;;
  79. rsync-snapshots|RSYNC-SNAPSHOTS)
  80. bm_upload_rsync_snapshots
  81. ;;
  82. s3|S3)
  83. bm_upload_s3
  84. ;;
  85. none|disabled)
  86. info "No upload method used."
  87. ;;
  88. *)
  89. warning "The upload method \"\$method\" is not supported; skipping."
  90. ;;
  91. esac
  92. done
  93. }
  94. # This will parse all the files contained in BM_REPOSITORY_ROOT
  95. # and will clean them up. Using clean_directory() and clean_file().
  96. function clean_repositories()
  97. {
  98. debug "clean_repositories"
  99. info "Cleaning \$BM_REPOSITORY_ROOT"
  100. clean_directory $BM_REPOSITORY_ROOT
  101. }
  102. # This will run the pre-command given.
  103. # If this command exit with non-zero status,
  104. # backup-manager will stop here.
  105. function exec_pre_command()
  106. {
  107. debug "exec_pre_command()"
  108. if [[ ! -z "$BM_PRE_BACKUP_COMMAND" ]]; then
  109. info "Running pre-command: \$BM_PRE_BACKUP_COMMAND."
  110. $BM_PRE_BACKUP_COMMAND
  111. if [ $? -eq 0 ]; then
  112. info "Pre-command succeeded."
  113. else
  114. warning "Pre-command failed. Stopping the process."
  115. _exit 15 "PRE_COMMAND"
  116. fi
  117. fi
  118. }
  119. function exec_post_command()
  120. {
  121. debug "exec_post_command()"
  122. if [[ ! -z "$BM_POST_BACKUP_COMMAND" ]]; then
  123. info "Running post-command: \$BM_POST_BACKUP_COMMAND"
  124. $BM_POST_BACKUP_COMMAND
  125. if [ $? -eq 0 ]; then
  126. info "Post-command succeeded."
  127. else
  128. warning "Post-command failed."
  129. _exit 15 "POST_COMMAND"
  130. fi
  131. fi
  132. }
  133. function bm_init_env()
  134. {
  135. debug "bm_init_env()"
  136. export TOOMUCH_TIME_AGO=`date +%d --date "$BM_ARCHIVE_TTL days ago"`
  137. # Use a single md5 file to store all archives. This lives here to make the
  138. # test suite work. It runs actions without touching the backup-manager
  139. # script
  140. export MD5FILE="${BM_REPOSITORY_ROOT}/${BM_ARCHIVE_PREFIX}-hashes.md5"
  141. if [[ -n "$HOME" ]]; then
  142. export BM__GPG_HOMEDIR="--homedir ${HOME}/.gnupg"
  143. else
  144. export BM__GPG_HOMEDIR="--homedir /root/.gnupg"
  145. fi
  146. check_logger
  147. }
  148. function bm_init_today()
  149. {
  150. debug "bm_init_today()"
  151. export TODAY=`date +%Y%m%d`
  152. }
  153. # be sure that zip is supported.
  154. function check_filetypes()
  155. {
  156. debug "check_filetypes()"
  157. case "$BM_TARBALL_FILETYPE" in
  158. "zip")
  159. if [[ ! -x "$zip" ]]; then
  160. error "The BM_TARBALL_FILETYPE conf key is set to \"zip\" but zip is not installed."
  161. fi
  162. ;;
  163. "tar.bz2" )
  164. if [[ ! -x "$bzip" ]]; then
  165. error "The BM_TARBALL_FILETYPE conf key is set to \"tar.bz2\" but bzip2 is not installed."
  166. fi
  167. ;;
  168. "tar.xz" )
  169. if [[ ! -x "$xz" ]]; then
  170. error "The BM_TARBALL_FILETYPE conf key is set to \"tar.xz\" but xz is not installed."
  171. fi
  172. ;;
  173. "tar.lzma" )
  174. if [[ ! -x "$lzma" ]]; then
  175. error "The BM_TARBALL_FILETYPE conf key is set to \"tar.lzma\" but lzma is not installed."
  176. fi
  177. ;;
  178. "dar" )
  179. if [[ ! -x "$dar" ]]; then
  180. error "The BM_TARBALL_FILETYPE conf key is set to \"dar\" but dar is not installed."
  181. fi
  182. ;;
  183. esac
  184. }
  185. function create_directories()
  186. {
  187. debug "create_directories()"
  188. if [[ ! -d $BM_REPOSITORY_ROOT ]]
  189. then
  190. info "The repository \$BM_REPOSITORY_ROOT does not exist, creating it."
  191. mkdir -p $BM_REPOSITORY_ROOT
  192. fi
  193. # for security reason, the repository should not be world readable
  194. # only BM_REPOSITORY_USER:BM_REPOSITORY_GROUP can read/write it.
  195. if [[ "$BM_REPOSITORY_SECURE" = "true" ]]; then
  196. chown $BM_REPOSITORY_USER:$BM_REPOSITORY_GROUP $BM_REPOSITORY_ROOT
  197. chmod $BM_REPOSITORY_CHMOD $BM_REPOSITORY_ROOT
  198. fi
  199. }