The directory ${RED}${local_directory_absent}${COLOR_DEBUG} doesn't exist."
fi
unset local_directory_absent
unset debug_prefix
return"${return_is_directory_absent}"
}
# }}}
is_file_absent(){# {{{
local_file_absent="${1}"
debug_prefix="${2:-}"
## File exists by default
return_is_file_absent="1"
### Check if the file exists
# shellcheck disable=SC2086
if find ${local_file_absent} > /dev/null 2>&1;then
return_is_file_absent="1"
debug_message "${debug_prefix}is_file_absent − \
The file ${RED}${local_file_absent}${COLOR_DEBUG} exists."
else
return_is_file_absent="0"
debug_message "${debug_prefix}is_file_absent − \
The file ${RED}${local_file_absent}${COLOR_DEBUG} doesn't exist."
fi
unset local_file_absent
unset debug_prefix
return"${return_is_file_absent}"
}
# }}}
is_file_present(){# {{{
local_file_present="${1}"
debug_prefix="${2:-}"
## File doesn't exist by default
return_is_file_present="1"
### Check if the file exists
# shellcheck disable=SC2086
if find ${local_file_present} > /dev/null 2>&1;then
return_is_file_present="0"
debug_message "${debug_prefix}is_file_present − \
The file ${RED}${local_file_present}${COLOR_DEBUG} exists."
else
return_is_file_present="1"
debug_message "${debug_prefix}is_file_present − \
The file ${RED}${local_file_present}${COLOR_DEBUG} doesn't exist."
fi
unset local_file_present
unset debug_prefix
return"${return_is_file_present}"
}
# }}}
clean_temp_files(){# {{{
## Remove temp files if DEBUG is not set
if[ ! "${DEBUG}" -eq "0"];then
rm --recursive --force -- "${DUP_TMP_DIR}"
else
debug_message "| Temp files are available in ${RED}${DUP_TMP_DIR}${COLOR_DEBUG} for any debug."
fi
return"0"
}
# }}}
main(){# {{{
# Define all vars
define_vars
debug_message "--- MAIN BEGIN"
debug_message "| Temp files are availabe here : ${RED}${DUP_TMP_DIR}${COLOR_DEBUG} ."
# Requirements {{{
if[ ! $(command -v sqlite3)];then
debug_message "| Need to install script dependency (${RED}sqlite3${COLOR_DEBUG})."
sudo aptitude install sqlite3
fi
# }}}
# If dup_dir doesn't exists {{{
# AND exit with error
is_directory_absent "${dup_dir}""| "\
&& error_message "The directory (${dup_dir}) doesn't exists. Check your configuration or use -d|--dir option to give an existing directory. Use --help for more informations."11
# }}}
# If dup_info doesn't exists {{{
# AND exit with error
is_directory_absent "${dup_info}""| "\
&& debug_message "| Create the ${RED}${dup_info}${COLOR_DEBUG} directory."\
&& mkdir --parent -- "${dup_info}"
# }}}
# If mail_template doesn't exists {{{
# AND exit with error
is_file_absent "${mail_template}""| "\
&& error_message "Email template (${mail_template}) doesn't exists. Please provide a path to a valid file (--template option) or create a template in appropriate directory (${dup_info}). See --help for more informations."12
# }}}
# If db_path doesn't exists {{{
# Init a table in a new sqlite3 database file
if is_file_absent "${db_path}""| ";then
debug_message "| Init table (${RED}${SQLITE_TABLE_NAME}${COLOR_DEBUG}) in a new SQLite database file (${RED}${db_path}${COLOR_DEBUG})."
sqlite3 "${db_path}""CREATE table ${SQLITE_TABLE_NAME}( name TEXT, user TEXT, size INT, iteration INT, date TEXT, emailed TEXT, PRIMARY KEY (name, user, size, iteration, date, emailed) );"\
|| error_message "Can't init a new TABLE (${SQLITE_TABLE_NAME}) in SQLite database (${db_path})."13
fi
# }}}
# Move to dup_dir
debug_message "| Move to users backup base path (${RED}${dup_dir}${COLOR_DEBUG})."
cd -- "${dup_dir}" >/dev/null \
|| error_message "Can't move to directory (${dup_dir}). Check the permissions or try with 'sudo'."14
debug_message "||| Get backup current size (~${RED}${backup_dir_current_size}${COLOR_DEBUG}${min_size_unit})."
### }}}
### Get previous size {{{
if[$(sqlite3 "${db_path}""SELECT size FROM ${SQLITE_TABLE_NAME} WHERE name='${user_dir}/${backup_dir}' AND iteration='-1'"| wc --lines --) -eq "1"];then
backup_dir_previous_size=$(sqlite3 "${db_path}""SELECT size FROM ${SQLITE_TABLE_NAME} WHERE name='${user_dir}/${backup_dir}' AND iteration='-1'")
debug_message "||| Use backup previous size (${RED}${backup_dir_previous_size}${COLOR_DEBUG}${min_size_unit}) from SQLite database."
else
backup_dir_previous_size="0"
debug_message "||| Set backup previous size to zero (${RED}${backup_dir_previous_size}${COLOR_DEBUG}${min_size_unit})."
debug_message "||| The differences between current and previous sizes is not enough (~${RED}${backup_dir_compare_size}${COLOR_DEBUG}${min_size_unit})."
backup_dir_email="True"
else
debug_message "||| The differences between current and previous sizes is correct (~${RED}${backup_dir_compare_size}${COLOR_DEBUG}${min_size_unit})."
backup_dir_email="False"
fi
### }}}
### Increase iterations for registred informations {{{
sqlite3 "${db_path}""UPDATE ${SQLITE_TABLE_NAME} SET iteration = iteration - 1 WHERE name='${user_dir}/${backup_dir}'"\
|| error_message "Can't increase iterations on SQLite db file (${db_path}) for backup dir (${user_dir}/${backup_dir})."32
### }}}
### Add a new record {{{
sqlite3 "${db_path}""INSERT INTO ${SQLITE_TABLE_NAME} values( \
'${user_dir}/${backup_dir}', \
'${user_dir}', \
${backup_dir_current_size}, \
-1, \
'$(date +%Y/%m/%d-%H:%M)', \
'${backup_dir_email}'\
)" || error_message "Can't add a new on SQLite db file (${db_path})for backup dir (${user_dir}/${backup_dir})." 33
### }}}
### Build email if required {{{
if["${backup_dir_email}"="True"];then
### Use default email template to build email body for this user
The difference (~${backup_dir_compare_size}${min_size_unit}) is bellow the minimum expected size (${min_size}).
" >> "${user_email_template}"
### }}}
### Add all recorded informations from SQLite db {{{
if[$(sqlite3 "${db_path}""SELECT * FROM ${SQLITE_TABLE_NAME} WHERE name='${user_dir}/${backup_dir}'"| wc --lines --) -ge "1"];then
printf'%b'"\nInformations issues des tests précédents:
Recorded informations from previous tests:
" >> "${user_email_template}"
printf".mode box\nSELECT * FROM ${SQLITE_TABLE_NAME} WHERE name='${user_dir}/${backup_dir}' ORDER BY name, iteration\n"| sqlite3 "${db_path}" >> "${user_email_template}"
#sqlite3 "${db_path}" "SELECT * FROM ${SQLITE_TABLE_NAME} WHERE name='${user_dir}/${backup_dir}' ORDER BY name, iteration"