#!/command/with-contenv /bin/bash source /assets/functions/00-container source /assets/defaults/10-db-backup source /assets/functions/10-db-backup PROCESS_NAME="db-backup-restore" oldcolumns=$COLUMNS ######################################################################################## ### System Functions ### ######################################################################################## ### Colours # Foreground (Text) Colors cdgy="\e[90m" # Color Dark Gray clg="\e[92m" # Color Light Green clm="\e[95m" # Color Light Magenta cwh="\e[97m" # Color White # Turns off all formatting coff="\e[0m" # Color Off # Background Colors bdr="\e[41m" # Background Color Dark Red bdg="\e[42m" # Background Color Dark Green bdb="\e[44m" # Background Color Dark Blue bdm="\e[45m" # Background Color Dark Magenta bdgy="\e[100m" # Background Color Dark Gray blr="\e[101m" # Background Color Light Red boff="\e[49m" # Background Color Off if [ -z "${1}" ] ; then interactive_mode=true else case "$1" in "-h" ) cat < If you only enter some of the arguments you will be prompted to fill them in. Other arguments -h This help screen EOF exit 0 ;; "-i" ) echo "interactive mode" interactive_mode=true ;; * ) interactive_mode=false ;; esac fi control_c() { if [ -f "${restore_vars}" ] ; then rm -rf "${restore_vars}" ; fi print_warn "User aborted" exit } get_filename() { COLUMNS=12 prompt="Please select a file to restore:" options=( $(find "${DEFAULT_FILESYSTEM_PATH}" -type f -maxdepth 2 -not -name '*.md5' -not -name '*.sha1' -print0 | sort -z | xargs -0) ) PS3="$prompt " select opt in "${options[@]}" "Custom" "Quit" ; do if (( REPLY == 2 + ${#options[@]} )) ; then echo "Bye!" exit 2 elif (( REPLY == 1 + ${#options[@]} )) ; then while [ ! -f "${opt}" ] ; do read -p "What path and filename to restore: " opt if [ ! -f "${opt}" ] ; then print_error "File not found. Please retry.." fi done break elif (( REPLY > 0 && REPLY <= ${#options[@]} )) ; then break else echo "Invalid option. Try another one." fi done COLUMNS=$oldcolumns r_filename=${opt} } get_dbhost() { p_dbhost=$(basename -- "${r_filename}" | cut -d _ -f 3) if [ -n "${p_dbhost}" ]; then parsed_host=true print_debug "Parsed DBHost: ${p_dbhost}" if grep -q "${p_dbhost}" "${restore_vars}" ; then detected_host_num=$(grep "${p_dbhost}" "${restore_vars}" | head -n1 | cut -c 3,4) detected_host_value=$(grep "${p_dbhost}" "${restore_vars}" | head -n1 | cut -d '=' -f 2) fi fi if [ -z "${detected_host_value}" ] && [ -z "${parsed_host}" ]; then print_debug "Parsed DBHost Variant: 1 - No Env, No Parsed Filename" q_dbhost_variant=1 q_dbhost_menu=$(cat < 0 && REPLY <= ${#options[@]} )) ; then break else echo "Invalid option. Try another one." fi done COLUMNS=$oldcolumns r_filename=${opt} } #### SCRIPT START trap control_c INT bootstrap_variables restore_init cat << EOF ## ${IMAGE_NAME} Restore Script ## Visit ${IMAGE_REPO_URL} ## #################################################### EOF ## Question Filename if [ -n "${1}" ]; then if [ ! -f "${1}" ]; then get_filename else r_filename="${1}" fi else get_filename fi print_debug "Filename to recover '${r_filename}'" ## Question Database Type if [ -n "${2}" ]; then r_dbtype="${2}" else get_dbtype fi print_debug "Database type '${r_dbtype}'" ## Question Database Host if [ -n "${3}" ]; then r_dbhost="${3}" else get_dbhost fi print_debug "Database Host '${r_dbhost}'" ## Question Database Name if [ -n "${4}" ]; then r_dbname="${4}" else get_dbname fi print_debug "Database Name '${r_dbname}'" ## Question Database User if [ -n "${5}" ]; then r_dbuser="${5}" else get_dbuser fi print_debug "Database User '${r_dbuser}'" ## Question Database Password if [ -n "${6}" ]; then r_dbpass="${6}" else get_dbpass fi print_debug "Database Pass '${r_dbpass}'" ## Question Database Port if [ -n "${7}" ]; then r_dbport="${7}" else get_dbport fi print_debug "Database Port '${r_dbport}'" ## Parse Extension case "${r_filename##*.}" in bz* ) decompress_cmd='bz' print_debug "Detected 'bzip2' compression" ;; gz* ) decompress_cmd="z" print_debug "Detected 'gzip' compression" ;; xz* ) decompress_cmd="xz" print_debug "Detected 'xzip' compression" ;; zst* ) decompress_cmd='zstd' print_debug "Detected 'zstd' compression" ;; sql ) print_debug "Detected No compression" ;; * ) print_debug "Cannot tell what the extension is for compression" ;; esac ## Perform a restore case "${r_dbtype}" in mariadb | mysql ) print_info "Restoring '${r_filename}' into '${r_dbhost}'/'${r_dbname}'" pv ${r_filename} | ${decompress_cmd}cat | mysql -u${r_dbuser} -p${r_dbpass} -P${r_dbport} -h${r_dbhost} ${r_dbname} exit_code=$? ;; pgsql | postgres* ) print_info "Restoring '${r_filename}' into '${r_dbhost}'/'${r_dbname}'" export PGPASSWORD=${r_dbpass} pv ${r_filename} | ${decompress_cmd}cat | psql -d ${r_dbname} -h ${r_dbhost} -p ${r_dbport} -U ${r_dbuser} exit_code=$? ;; mongo ) cat << EOF Do you wish to drop any existing data before restoring? Y ) Yes N ) No Q ) Quit EOF echo -e "${coff}" read -p "$(echo -e ${clg}** ${cdgy}Enter Value \(${cwh}Y${cdgy}\) \| \(${cwh}N${cdgy}\) \| \(${cwh}Q${cdgy}\) : ${cwh}${coff})" q_menu_mongo_dropdb case "${q_menu_mongo_dropdb,,}" in "y" | "yes" | * ) mongo_dropdb="--drop" ;; "n" | "update" ) unset mongo_dropdb ;; "q" | "exit" ) print_info "Quitting Script" exit 1 ;; esac print_info "Restoring '${r_filename}' into '${r_dbhost}'/'${r_dbname}'" if [ "${ENABLE_COMPRESSION,,}" != "none" ] && [ "${ENABLE_COMPRESSION,,}" != "false" ] ; then mongo_compression="--gzip" fi if [ -n "${r_dbuser}" ] ; then mongo_user="-u=${r_dbuser}" fi if [ -n "${r_dbpass}" ] ; then mongo_pass="-p=${r_dbpass}" fi if [ -n "${DB_AUTH}" ] ; then mongo_auth_database="--authenticationDatabase=${DB_AUTH}" fi mongorestore ${mongo_compression} -d=${r_dbname} -h=${r_dbhost} --port=${r_dbport} ${mongo_dropdb} ${mongo_user} ${mongo_pass} --archive=${r_filename} ${mongo_auth_database} exit_code=$? ;; * ) print_info "Unable to restore DB of type '${r_dbtype}'" exit_code=3 ;; esac print_debug "Exit code: ${exit_code}" if [ "${exit_code}" = 0 ] ; then print_info "Restore complete!" else print_error "Restore reported errors" fi