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