PageRenderTime 55ms CodeModel.GetById 43ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

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