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

reformat code; fix restore permissions

parent c9f05d74
No related branches found
No related tags found
1 merge request!129Db Profiles
......@@ -16,9 +16,11 @@
# 2022-02-18 ..... WIP: use class like functions
# 2022-03-17 ..... WIP: add lines with prefix __DB__
# 2022-11-04 ah rename hooks
# 2024-03-14 ah v2.0: use profiles for local and remote databases
# ======================================================================
# --- variables:
# ARCHIVE_BASEDIR {string} base directory for db archive (couchdb2 only)
# BACKUP_BASEDIR {string} base directory for db dumps
# BACKUP_DATE {string} string with current timestamp; will be part of filename for backups
# BACKUP_KEEP_DAYS {int} count of days how long to keep db dumps below $BACKUP_BASEDIR
......@@ -56,6 +58,7 @@
typeset -i BACKUP_KEEP_DAYS=0
BACKUP_DATE=
LASTINPUT=
# ----------------------------------------------------------------------
# FUNCTIONS 4 DB-WRAPPER
......@@ -196,7 +199,6 @@
# param string optional: DB-Name for file filter to select from existing dumps;
function listBackupedDBs(){
if [ -d "${BACKUP_TARGETDIR}" ]; then
if [ -z $1 ]; then
# list all databases
find "${BACKUP_TARGETDIR}" -mindepth 1 -maxdepth 1 -type f -exec basename {} \; \
......@@ -207,7 +209,6 @@
# list dumps of a database
ls -ltr ${BACKUP_TARGETDIR}/${1}*gz | sed "s,${BACKUP_TARGETDIR}/,,g"
fi
else
color.echo error "ERROR: ${BACKUP_TARGETDIR} does not exist - here are no backups to restore."
echo
......@@ -218,7 +219,6 @@
echo "3) Start database restore again"
echo " $(dirname $0)/localdump.sh restore [profile]"
echo
exit 1
fi
}
......@@ -236,7 +236,9 @@
grep "^/" "$metafile" || grep "^ File: " "$metafile" | cut -c 9-
else
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" | sed "s#\.couchdbdump\.gz$##g" )
# ^ ^
# timestamp in backup file __/ for couchdb2 restore from archive __/
if [ -z $sDb ]; then
color.echo error "ERROR: db name was not detected from file $1"
exit 1
......@@ -245,7 +247,36 @@
fi
}
# ------------------------------------------------------------
# show a selection + a prompt and read the input
# - If the selection is just 1 line it will be returned
# - If the user presses just return the script will exit
# param string selection of items to select from
# param string prompt to show
function showSelectAndInput(){
local _selection="$1"
local _prompt="$2"
local _lines
typeset -i _lines; _lines=$( grep -c "." <<< "$_selection" )
if [ $_lines -eq "1" ]; then
echo "INFO: No interaction on a single choice. Using '$_selection'"
LASTINPUT="$_selection"
return 0
else
echo "$_selection"
color.print input "${_prompt} >"
read -r LASTINPUT
if [ -z "$LASTINPUT" ]; then
echo "No input given. Aborting."
exit 1
fi
fi
}
# ------------------------------------------------------------
# read .meta file (that contains output of stats) and restore last owner and file permissions
# param string filename of db dump
# param string restored database file
......@@ -259,8 +290,9 @@
# _sPerm _sUser _sGroup
local _sPerm=$( grep "^Access: (" "${sMyMeta}" | cut -f 2 -d '(' | cut -f 1 -d '/')
if [ -n "$_sPerm" ]; then
local _sUser=$( grep "^Access: (" "${sMyMeta}" | cut -f 3 -d '/' | cut -f 1 -d ')' | tr -d ' ')
local _sGroup=$( grep "^Access: (" "${sMyMeta}" | cut -f 4 -d '/' | cut -f 1 -d ')' | tr -d ' ')
local _sUser=$( grep "^Access: (" "${sMyMeta}" | cut -f 3 -d '(' | cut -f 1 -d '/' | tr -d ' ')
local _sGroup=$( grep "^Access: (" "${sMyMeta}" | cut -f 4 -d '(' | cut -f 1 -d '/' | tr -d ' ')
echo -n "Restoring file owner $_sUser:$_sGroup and permissions $_sPerm ... "
chown "$_sUser:$_sGroup" "${sTargetfile}" && chmod "$_sPerm" "${sTargetfile}"
......@@ -275,46 +307,45 @@
function showhelp(){
local _self
_self=$( basename "$0" )
echo
echo "LOCALDUMP detects existing local databases and dumps them locally."
echo "It is included in the backup.sh to dump all before a file backup will store them."
echo
echo "It can be started seperately for manual database backups or for restore"
echo
echo "SYNTAX:"
echo " $_self [OPTIONS] <operation> <profile [more_profiles]>"
echo
echo "OPTIONS:"
echo " -h|--help show this help"
echo
echo "PARAMETERS:"
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"
echo " Without a filename it starts an interactive mode"
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
echo "EXAMPLES:"
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."
cat <<EOH
LOCALDUMP detects existing local databases and dumps them locally.
It is included in the backup.sh to dump all before a file backup will store
them. It can be started seperately for manual database backups or for restore.
SYNTAX:
$_self [OPTIONS] <operation> <profile [more_profiles]>
OPTIONS:
-h|--help show this help
PARAMETERS:"
operation - one of check|backup|restore; optional parameter
backup dump all databases/ schemes of a given service
check show info only if the service is available
restore import a dump into same or new database
Without a filename it starts an interactive mode
profile - name of database profiles
You get a list of all available services without parameter
Use ALL for bulk command
file - filename of db dump to restore to origin database scheme
EXAMPLES:
$_self backup
$_self backup ALL
Backup all databases of all found services
$_self backup mysql
Backup all Mysql databases.
$_self restore
Start interactive restore of a database of any service.
$_self restore sqlite
Start interactive restore of an sqlite database.
$_self restore <file-to-restore> [<database-name>]
Restore a given dump file to the origin database scheme or
to a new/ other database with the given name.
EOH
}
......@@ -350,7 +381,6 @@
exit 1
fi
# ----- init vars
BACKUP_BASEDIR=$(_j_getvar "${JOBFILE}" "dir-localdumps")
......@@ -447,7 +477,6 @@
cleanup_backup_target
show_info_backup_target
else
echo "SKIP: profile '$PROFILENAME' "
......@@ -482,17 +511,8 @@
h2 "Select profile that has a dump"
if [ -z "${parService}" ]; then
if [ "$( listBackupedServices | wc -l )" -eq 1 ]; then
parService="$( listBackupedServices )"
echo "Selecting the only existing profile: $parService"
fi
fi
if [ -z "${parService}" ]; then
listBackupedServices
color.print input "Restore for profile name >"
read -r parService
test -z "$parService" && exit 1
showSelectAndInput "$( listBackupedServices )" "Restore for profile name"
parService="$LASTINPUT"
else
echo "Taken from command line: $parService"
fi
......@@ -501,21 +521,14 @@
if ! dbdetect.setProfile "${parService}"; then
color.echo error "ERROR: profile [${parService}] is not known here (or database service is stopped)."
echo
echo "Existing database profiles:"
get_database_profiles | nl
echo
exit 1
fi
# ----- check if dump exists in archive and in backup
if [ -d "${BACKUP_BASEDIR}/${parService}" ] && [ -d "${ARCHIVE_BASEDIR}/${parService}" ]; then
echo
echo "Backup ${BACKUP_BASEDIR}"
echo "Archive ${ARCHIVE_BASEDIR}"
color.print input "Select a source directory >"
read -r backupsource
test -z "$backupsource" && exit 1
BACKUP_BASEDIR="${backupsource}"
showSelectAndInput "$(echo "${BACKUP_BASEDIR}"; echo "${ARCHIVE_BASEDIR}")" "Select a source directory"
BACKUP_BASEDIR="$LASTINPUT"
else
# just one test needed because BACKUP_BASEDIR is BACKUP_BASEDIR
test -d "${ARCHIVE_BASEDIR}/${parService}" && BACKUP_BASEDIR="${ARCHIVE_BASEDIR}"
......@@ -529,18 +542,14 @@
BACKUP_TARGETDIR="${BACKUP_BASEDIR}/${parService}"
h2 "Select database"
listBackupedDBs
color.print input "name of db to restore >"
read -r fileprefix
test -z "$fileprefix" && exit 1
h2 "Select a database schema"
showSelectAndInput "$(listBackupedDBs)" "Name of database to restore"
fileprefix="$LASTINPUT"
echo
h2 "Select a specific dump for that database"
listBackupedDBs $fileprefix
color.print input "backupset to import >"
read -r dbfile
test -z "$dbfile" && exit 1
showSelectAndInput "$(listBackupedDBs $fileprefix)" "Backupset to import"
dbfile="$LASTINPUT"
echo
sTargetDb=$(guessDB ${dbfile})
......@@ -634,3 +643,4 @@
exit $rc
# ----------------------------------------------------------------------
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment