diff --git a/localdump.sh b/localdump.sh index 745502c6d3bfc79132f0bf2d3f006f1a00ef30eb..8859830a08a99570a704a47f57ed13f1a6738536 100755 --- a/localdump.sh +++ b/localdump.sh @@ -39,14 +39,8 @@ . $(dirname $0)/includes/dbdetect.class.sh || exit 1 - # if [ -r ~/.backup.conf ]; then - # . ~/.backup.conf - # fi - if [ ! -r "${JOBFILE}" ]; then - color error - echo ERROR: missing config file ${JOBFILE}. - color reset + color.echo error "ERROR: missing config file ${JOBFILE}." exit 1 fi @@ -79,7 +73,7 @@ gzip -9 -f "${_outfile}" fetchrc else - cecho error "ERROR occured while dumping - no gzip of $_outfile" + color.echo error "ERROR occured while dumping - no gzip of $_outfile" fi # echo -n "__DB__$SERVICENAME INFO: backup to " # ls -l "$_outfile"* 2>&1 @@ -98,9 +92,9 @@ h3 "CLEANUP ${BACKUP_TARGETDIR} older $BACKUP_KEEP_DAYS days ..." echo find "${BACKUP_TARGETDIR}" -mtime +$BACKUP_KEEP_DAYS -delete -print - color cmd + color.preset cmd find "${BACKUP_TARGETDIR}" -mtime +$BACKUP_KEEP_DAYS -delete -print - color reset + color.reset if [ $(find "${BACKUP_TARGETDIR}" -type f | wc -l) -eq 0 ]; then echo "INFO: the directory is empty - deleting it" @@ -125,9 +119,7 @@ function create_targetdir(){ mkdir -p "${BACKUP_TARGETDIR}" 2>/dev/null if [ ! -d "${BACKUP_TARGETDIR}" ]; then - color error - echo FATAL ERROR: directory ${BACKUP_TARGETDIR} was not created - color reset + color.echo "error" "FATAL ERROR: directory ${BACKUP_TARGETDIR} was not created" exit 1 fi } @@ -154,23 +146,17 @@ # ------------------------------------------------------------ # get a list of existing dumper scripts function get_services(){ - - # - # ls -1 ${BACKUP_PLUGINDIR}/*.sh | sed "s#${BACKUP_PLUGINDIR}/##" | sed "s#\.sh##" | sort - for config in $(dbdetect.getConfigs); do if dbdetect.exists $config; then echo "$( dbdetect.getProfile $config )" fi done - } # ------------------------------------------------------------ # show directory infos with count of files and used space # show used space and count of files and dirs function show_info_backup_target(){ - if [ -d "${BACKUP_TARGETDIR}" ]; then h3 "INFO about backup target ${BACKUP_TARGETDIR}" @@ -193,6 +179,12 @@ # FUNCTIONS 4 RESTORE # ---------------------------------------------------------------------- + # ------------------------------------------------------------ + # restore: show profiles from that exist backups + # global string BACKUP_BASEDIR base directory of all backups + function listBackupedServices(){ + find "${BACKUP_BASEDIR}" -mindepth 1 -maxdepth 1 -type d | sed "s#^${BACKUP_BASEDIR}/##g" | sort + } # ------------------------------------------------------------ # restore: show databases that can be restored @@ -207,16 +199,14 @@ fi cd - >/dev/null else - color error - echo ERROR: ${BACKUP_TARGETDIR} does not exist - here are no backups to restore. - color reset + color.echo error "ERROR: ${BACKUP_TARGETDIR} does not exist - here are no backups to restore." echo - echo You can try to restore dumps: + echo "You can try to restore dumps:" echo "1) Restore dump files from a backup set" - echo " `dirname $0`/restore.sh $BACKUP_BASEDIR" + echo " $(dirname $0)/restore.sh $BACKUP_BASEDIR" echo "2) Copy restored dumps into $BACKUP_TARGETDIR" echo "3) Start database restore again" - echo " `dirname $0`/localdump.sh restore [service]" + echo " $(dirname $0)/localdump.sh restore [service]" echo exit 1 @@ -238,9 +228,7 @@ sBasename=$(basename $1) sDb=$(echo ${sBasename} | sed "s#__[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]-[0-9][0-9].*##g") if [ -z $sDb ]; then - color error - echo ERROR: db name was not detected from file $1 - color reset + color.echo error "ERROR: db name was not detected from file $1" exit 1 fi echo $sDb @@ -290,8 +278,7 @@ echo " -h|--help show this help" echo echo "PARAMETERS:" - echo " operation - one of check|backup|restore; optional parameter; default is" - echo " 'backup'." + echo " operation - one of check|backup|restore; optional parameter" echo " backup dump all databases/ schemes of a given service" echo " check show info only if the service is available" echo " restore import a dump into same or new database" @@ -299,11 +286,25 @@ echo " profile - name of database profiles" echo " You get a list of all available services without parameter" echo " Use ALL for bulk command" - echo " file - filename of db dump to restore to origin database scheme" + echo " file - filename of db dump to restore to origin database scheme" echo echo "EXAMPLES:" - echo "$_self backup [Name_of_service]" - echo "$_self restore [Name_of_service] [[file-to-restore]]" + echo " $_self backup" + echo " $_self backup ALL" + echo " Backup all databases of all found services" + echo + echo " $_self backup mysql" + echo " Backup all Mysql databases." + echo + echo " $_self restore" + echo " Start interactive restore of a database of any service." + echo + echo " $_self restore sqlite" + echo " Start interactive restore of an sqlite database." + echo + echo " $_self restore <file-to-restore> [<database-name>]" + echo " Restore a given dump file to the origin database scheme or" + echo " to a new/ other database with the given name." } @@ -320,7 +321,7 @@ ;; esac; done - mode="backup" + mode="" case "$1" in backup|check|restore|shell) mode=$1 @@ -329,9 +330,7 @@ esac if [ -z "$mode" ]; then - color error - echo "ERROR: missing parameter." - color reset + color.echo error "ERROR: missing parameter for operation." echo showhelp echo @@ -343,14 +342,12 @@ # ----- db init - BACKUP_BASEDIR=$(_j_getvar ${JOBFILE} "dir-localdumps") + BACKUP_BASEDIR=$(_j_getvar "${JOBFILE}" "dir-localdumps") # check if [ -z "$BACKUP_BASEDIR" ]; then - color error - echo ERROR: missing config for backup target. + color.echo error "ERROR: missing config for backup target." echo There must be an entry dir-localdumps in ${JOBFILE} - color reset exit 1 fi BACKUP_PLUGINDIR=$(dirname $0)/plugins/localdump @@ -370,11 +367,10 @@ h1 $(date) IML BACKUP :: LOCALDUMP :: $* - echo "mode = $mode" export SERVICENAME=$1 - BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME} - BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} ) + # BACKUP_TARGETDIR=${BACKUP_BASEDIR}/${SERVICENAME} + # BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} ) case "$mode" in # ------------------------------------------------------------ @@ -390,7 +386,7 @@ ;; # ------------------------------------------------------------ backup) - if [ "$SERVICENAME" = "ALL" ]; then + if [ "$1" = "ALL" ] || [ -z "$1" ]; then services=$(get_services) echo AUTO: calling local backup scripts for all known services @@ -453,31 +449,55 @@ restore) h1 "RESTORE DATABASE" - if [ -z $2 ]; then + if [ -z $1 ] || [ ! -f "$1" ]; then + + parService="$1" + + # ----- interactive selections + + h2 "Select profile that has a dump" + if [ -z "${parService}" ]; then + listBackupedServices + color.print input "Restore for profile name >" + read -r parService + test -z "$parService" && exit 1 + else + echo "Taken from command line: $parService" + fi - # ----- file selection + # ----- check if profile exists + if ! dbdetect.setProfile "${parService}"; then + color.echo error "ERROR: profile [${parService}] is not known here (or database service is stopped)." + echo + echo "Existing services:" + get_services + exit 1 + fi + if [ ! -d "${BACKUP_BASEDIR}/${parService}" ]; then + color.echo error "ERROR: profile ${parService} has no backup yet." + exit 1 + fi - h2 "select database" + + BACKUP_TARGETDIR="${BACKUP_BASEDIR}/${parService}" + + h2 "Select database" listBackupedDBs - color input - echo -n "name of db to restore >" - color reset - read fileprefix + color.print input "name of db to restore >" + read -r fileprefix + test -z "$fileprefix" && exit 1 echo - h2 "select a specific dump for that database" + h2 "Select a specific dump for that database" listBackupedDBs $fileprefix - color input - echo -n "backupset to import >" - color reset - read dbfile + color.print input "backupset to import >" + read -r dbfile + test -z "$dbfile" && exit 1 echo - color input sTargetDb=$(guessDB ${dbfile}) - echo -n "new database name [$sTargetDb] >" - color reset - read sTargetDb + color.print input "New database name [$sTargetDb] >" + read -r sTargetDb if [ -z $sTargetDb ]; then sTargetDb=$(guessDB ${dbfile}) fi @@ -485,17 +505,15 @@ sDumpfile="${BACKUP_TARGETDIR}/${dbfile}" else - sDumpfile=$2 - sTargetDb=$3 + sDumpfile=$1 + sTargetDb=$2 fi shift 2 # ----- start restore if [ ! -f "${sDumpfile}" ]; then - color error - echo ERROR: ${sDumpfile} is not a file - color reset + color.echo error "ERROR: [${sDumpfile}] is not a file" rc=$rc+1 else @@ -510,61 +528,55 @@ BACKUP_SCRIPT=$( get_service_script ${SERVICENAME} ) BACKUP_PARAMS=$( dbdetect.getParams ) - - # ------ set env eval $( dbdetect.setenv ) - . $BACKUP_SCRIPT $mode "${sDumpfile}" "${sTargetDb}" if [ $? -ne 0 -o $rc -ne 0 ]; then - color error - echo ERROR: $mode failed. See ouput above. :-/ - color reset + color.echo error "ERROR: $mode failed. See ouput above. :-/" else - color ok - echo OK, $mode was successful. - color reset + color.echo ok "OK, $mode was successful." fi # ------ unset env eval $( dbdetect.unssetenv ) else - color error - echo "ERROR: Profile $PROFILENAME was detected but its database service is not available." - color.reset + color.echo error "ERROR: Profile $PROFILENAME was detected but its database service is not available." fi fi ;; # ------------------------------------------------------------ - shell) - - export BACKUP_TARGETDIR - . $BACKUP_SCRIPT - ( - mycmd= - echo - echo "Starting interactive shell..." - echo - echo "STATUS: STILL ALPHA as long existing db plugins are not rewritten." - echo - echo "INFO: Try ${SERVICENAME}.help to see database specific commands." - echo "INFO: Type exit and return to leave the shell." - echo - while [ ! "$mycmd" = "exit" ]; do - echo -n "[${SERVICENAME}]" - color input - echo -n " $( pwd )" - color reset - echo -n " % " - read -r mycmd - if [ ! "$mycmd" = "exit" ];then - color cmd - eval $mycmd - color reset - fi - done - ) + # shell) + + # export BACKUP_TARGETDIR + # . $BACKUP_SCRIPT + # ( + # mycmd= + # echo + # echo "Starting interactive shell..." + # echo + # echo "STATUS: STILL ALPHA as long existing db plugins are not rewritten." + # echo + # echo "INFO: Try ${SERVICENAME}.help to see database specific commands." + # echo "INFO: Type exit and return to leave the shell." + # echo + # while [ ! "$mycmd" = "exit" ]; do + # echo -n "[${SERVICENAME}]" + # color.print input " $( pwd )" + # echo -n " % " + # read -r mycmd + # if [ ! "$mycmd" = "exit" ];then + # color.preset cmd + # eval $mycmd + # color.reset + # fi + # done + # ) + # ;; + + # ----- start restore + *) + color.echo error "ERROR: unknown command [$mode]" ;; esac