mirror of https://github.com/pditzel/dbb.git

pditzel
18.48.2017 e94a20bc0805bcf3178f1c227a74dc5ef942869e
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
#!/bin/bash
 
################################################################################
#                                                                              #
# Author: Patrick Ditzel (patrick@central-computer.de)                         #
# Lizenz:  GNU GENERAL PUBLIC LICENSE v3                                       #
#                                                                              #
################################################################################
 
################################################################################
# Set up the backupenvironment and "global" functions
################################################################################
 
INSTALLATION_PATH_PREFIX=/usr/local
 
function set_config {
    if [ -r /etc/dbb.cfg ]; then
        # Check if configuration is found in /etc. If yes set configvar
        BACKUPCFG=/etc/dbb.cfg
    elif [ -r ~/.dbb.cfg ]; then
        # If config is found in the backupuser home directory set it into the configvar
        BACKUPCFG=~/.dbb.cfg
    else
        echo "No configuration file is found, please create one" | /usr/bin/logger -s -i -t databasebackup
    fi
}
 
function check_backup_env {
    # Check if the configuration exists and is not empty
    if [ -r $BACKUPCFG ] && [ -s $BACKUPCFG ]; then
        # If true then read it
        source $BACKUPCFG
        if [ -d $BACKUPCFG.d ]; then
            if [ ! "$(find $BACKUPCFG.d/*.cfg)" ]; then
                echo "Configurationdirectory for modules exist but it is empty" | /usr/bin/logger -s -i -t databasebackup
            else
                echo "Read module configuration" | /usr/bin/logger -s -i -t databasebackup
                for MODULECFG in $(find $BACKUPCFG.d/*.cfg); do source "$BACKUPCFG".d/"${MODULECFG##*/}"; done
            fi
        else
            echo "Configurationdirectory does not exists, can't load any configurationfile" | /usr/bin/logger -s -i -t databasebackup
        fi
    else
        # If not throw an errormessage
        echo "The configfile does not exists or is empty" | /usr/bin/logger -s -i -t databasebackup
        echo "Please create the $BACKUPCFG or write your settings into" | /usr/bin/logger -s -i -t databasebackup
        exit 1
    fi
    if [ "$ENABLE_DEBUG" = "TRUE" ]; then
        # If debugoutput is enabled show the configurationfile without comments
        echo "##################################################################################" | /usr/bin/logger -s -i -t databasebackup
        while read -r configline; do
            echo "$configline" | grep -v '^$' | grep -v '^#' | /usr/bin/logger -s -i -t databasebackup
        done <<< "$(cat $BACKUPCFG)"
        for MODCFGLINE in $(find $BACKUPCFG.d/*.cfg); do
            echo "Debuginfo for $MODCFGLINE" | /usr/bin/logger -s -i -t databasebackup
            while read -r modconfigline; do
                echo "$modconfigline" | grep -v '^$' | grep -v '^#' | /usr/bin/logger -s -i -t databasebackup
            done <<< "$(cat "$MODCFGLINE")"
        done
        echo "################################################################################" | /usr/bin/logger -s -i -t databasebackup
    fi
    # Check if the target directory for the backupfiles exists
    if [ ! -d "$BACKUP_DIR" ]; then
        # If not create it
        mkdir -p "$BACKUP_DIR"
    fi
    # Check if the directory for tempfiles exists
    if [ ! -d "$TMP_DIR" ]; then
        # If not create it
        mkdir -p "$TMP_DIR"
    fi
 
}
 
function set_logger {
    # Check if log to syslog is enabled
    if [ "$ENABLE_SYSLOG" = "TRUE" ]; then
        # If true then define the logger
        LOGGER="/usr/bin/logger -s -i -t databasebackup"
    else
        # If not cat it out on stdout
        LOGGER="/bin/cat"
    fi
}
 
function debug {
        DEBUGMSG=$1
        if [ "$ENABLE_DEBUG" = "TRUE" ]; then
                echo "<debuginformation>: $DEBUGMSG" | $LOGGER
        fi
}
 
function load_dbbmodules {
    # Function to load the dbb-module corresponding to the config-files found in the dbb.cfg.d-directory
    debug "function: load_dbbmodules, VAR BACKUPCFG.d: $BACKUPCFG.d"
    debug "function: load_dbbmodules, VAR INSTALLATION_PATH_PREFIX/lib/dbb-modules: $INSTALLATION_PATH_PREFIX/lib/modules"
    if [ -d $BACKUPCFG.d ] && [ -d $INSTALLATION_PATH_PREFIX/lib/dbb-modules ]; then
        if [ ! "$(ls -A $BACKUPCFG.d)" ] && [ ! "$(ls -A $INSTALLATION_PATH_PREFIX/lib/dbb-modules)" ]; then
            debug "Configurationdirectory or directory for modules exist but it is empty"
        else
            for MODULE in $(find $BACKUPCFG.d/*.cfg); do
                source $INSTALLATION_PATH_PREFIX/lib/dbb-modules/"$(echo "${MODULE##*/}" | cut -d "." -f1)"
                debug "Load module: $INSTALLATION_PATH_PREFIX/lib/dbb-modules/$(echo "${MODULE##*/}" | cut -d "." -f1)"
                # Check the dependenncies defined in the modulefunction -> should be defined in configfiles
                check-"$(echo "${MODULE##*/}" | cut -d "." -f1)"-deps
            done
        fi
    else
        debug "Configurationdirectory does nort exists, can't load any configurationfile"
    fi
}
 
function is_interactive {
    SCRPT=$(basename "$0")
    debug "$SCRPT"
 
    if [ "$SCRPT" = "dbbi" ]; then
        # If the script is called for interactive use we have to chenge the calls of the functions
        # Setting the $LOGGER für interactive use to /bin/cat
        LOGGER="/bin/cat"
        # Here we set teh environment variable for interactive use
        debug "dbbi (DataBase Interactive Backup) is called"
        RADIOACTIVE=TRUE
    elif [ "$SCRPT" = "dbb" ]; then
        # Set the $LOGGER
        LOGGER="/usr/bin/logger -s -i -t databasebackup"
        # If the script is used noninteractive we have also to set the environmet variable with this information
        debug "dbb (DataBase Backup) is called"
        RADIOACTIVE=FALSE
        # unset the $LOGGER because this will be set later within the informatione in the configfile
        unset LOGGER
    else
        # If the switch between interactive and noninteractive does not work: tell it but before set the $LOGGER
        LOGGER="/usr/bin/logger -s -i -t databasebackup"
        debug "An error occured - don't know if to use interactive or noninteractive"
        exit 1
    fi
}
 
function backup_file_handler {
    # translate the vars to make it more readable
    BFH_TMP_DIR=$1
    BFH_BACKUPDIR_DIR=$2
    BFH_FILE=$3
    # If enabled put out some debug infos
    debug "BFH_TMP_DIR: $BFH_TMP_DIR"
    debug "BFH_BACKUPDIR_DIR: $BFH_BACKUPDIR_DIR"
    debug "FILE: $BFH_FILE"
    # Check if the script should keep a filehistorie
    if [ "$KEEP_BACKUP_FILES" = "TRUE" ]; then
        debug "Keep history"
        # Set some vars to manage the files
        BACKUP_DAY=$(date +%x)
        REMOVE_NUMBER=$(( $BACKUP_FILES_DAYS + 1 ))
        BFH_FILE_PREFIX_NAME_TO_REMOVE=$(date -d "$REMOVE_NUMBER days ago" "+%x")
        # ... and if it is turned on give some debig info
        debug "BACKUP_DAY: $BACKUP_DAY"
        debug "REMOVE_NUMBER: $REMOVE_NUMBER"
        debug "FILE_PREFIX_NAME_TO_REMOVE: $BFH_FILE_PREFIX_NAME_TO_REMOVE-$BFH_FILE"
        # Check if there is an backupfile from the current day
        if [ -f "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"_"$BFH_FILE" ]; then
            # If yes append miniutes and seconds to the date-profix of the filename
            debug "File $BFH_BACKUPDIR_DIR/$BACKUP_DAY_$BFH_FILE already exists. Rename the new one."
            DATE_TIME_SUFFIX=$(date +%H:%M:%S)
            # ... and move it into the targetdir
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"_"$DATE_TIME_SUFFIX"_"$BFH_FILE"
        else
            # If there is no backupfile of the current day move it to the backupfolder
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BACKUP_DAY"_"$BFH_FILE"
        fi
        # Check if there are files older then the days to keep set in the config
        if [ -f "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"-"$BFH_FILE" ]; then
            # if yes remove it
            rm "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"_"$BFH_FILE"
            # Also remove the files with the extended prefix in the name
            # If there is ab file with the extende prefix then there has to be a file with tne normal prefix
            rm "$BFH_BACKUPDIR_DIR"/"$BFH_FILE_PREFIX_NAME_TO_REMOVE"_????????_"$BFH_FILE"
        else
            # If no file exists do nothing but some debuginfo
            debug "File $BFH_BACKUPDIR_DIR/$BFH_FILE_PREFIX_NAME_TO_REMOVE-$BFH_FILE does not exists, so can not remove it."
        fi
    else
        # If we do not keep a filehistory do the following
        # Check if the targefile exists
        if [ -f "$BFH_BACKUPDIR_DIR"/"$BFH_FILE" ]; then
            debug "$BFH_FILE exists ... make a diff"
            # Check if there are differences betwenn last backup and the actual one
            diff "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BFH_FILE" > /dev/null 2>&1
            if [ $? -ne 0 ]; then
                # If yes then move it to the backupfolder
                debug "Differences found between old and new $BFH_FILE -> moving to BACKUP_DIR"
                mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BFH_FILE"
            else
                # If not do nothing
                debug "No differences found between old an new $BFH_FILE"
            fi
        else
            # If there is a new databasedumpfile move it to the backupfolder
            debug "New Backupfile $BFH_FILE -> moving to $BFH_BACKUPDIR_DIR"
            mv "$BFH_TMP_DIR"/"$BFH_FILE" "$BFH_BACKUPDIR_DIR"/"$BFH_FILE"
        fi
    fi
}
 
function check_global_deps {
    # Check the dependencies for the global part of dbb
    if [ ! -e /usr/bin/sudo ]; then
        debug "It seems that you dont have sudo installed. Please install sudo and restart"
        exit 1
    else
        debug "Global dependencies are OK."
    fi
}
 
################################################################################
 
# The mainfunktion
function main {
    # Check if the script runs interactive or not
    is_interactive
    if [ "$RADIOACTIVE" = "TRUE" ]; then
        debug "Unsing dbbi (dbb interactive = dbbi) is for future use"
    fi
    if [ "$RADIOACTIVE" = "FALSE" ]; then
        # Set up the configuration for the noninteractive mode
        set_config
        # Configure logging (from configurationfil)e
        set_logger
        # Check if the backupenvironment is setup properly
        check_backup_env
        debug "Check global dependencies"
        check_global_deps
        debug "Load dbb-modules"
        load_dbbmodules
        # Run modul-main-functions
        for MODULEMAIN in $(find $BACKUPCFG.d/*.cfg); do
            debug "run $MODULEMAIN-main - function of module '$(echo "${MODULEMAIN##*/}" | cut -d "." -f1)'"
            "$(echo "${MODULEMAIN##*/}" | cut -d "." -f1)"-main
        done
            # The final action: remove the dumps
        rm -rf "$TMP_DIR"
    fi
 
}
 
main