Skip to content
Snippets Groups Projects
Commit 5faf555a authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

update restore process

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