Select Git revision
localdump_mysql.sh
-
Hahn Axel (hahn) authoredHahn Axel (hahn) authored
sqlite.sh 3.07 KiB
#!/bin/bash
# ================================================================================
#
# LOCALDUMP :: SQLITE
# create gzipped plain text backups from each sqlite database
#
# --------------------------------------------------------------------------------
# ah - Axel Hahn <axel.hahn@iml.unibe.ch>
# ds - Daniel Schueler <daniel.schueler@iml.unibe.ch>
#
# 2018-02-02 ah,ds v1.0 first lines
# 2018-02-09 ah,ds v1.1 write a .meta file after successful backup
# 2022-03-17 v1.2 WIP: add lines with prefix __DB__
# 2024-02-20 ah v1.3 Handle profiles; restore into existing db file; restore file owner and perms
# 2024-10-02 ah v1.4 rename backup and restore function
# ================================================================================
if [ -z "$BACKUP_TARGETDIR" ]; then
echo "ERROR: you cannot start $(basename $0) directly"
rc=$rc+1
exit 1
fi
# --------------------------------------------------------------------------------
# FUNCTIONS
# --------------------------------------------------------------------------------
# start multiple sqlite3 backups
# files are taken from loaded profiles/sqlite*.ini - section [detect] -> files[]
function sqlite.backup(){
create_targetdir
for DATABASE_FILE in $( dbdetect.getFiles )
do
echo -n "__DB__${SERVICENAME} backup $DATABASE_FILE "
if [ ! -f "$DATABASE_FILE" ]; then
color error
echo "ERROR: given database file does not exist"
color reset
rc=$rc+1
else
TARGET=$(get_outfile ${DATABASE_FILE})
TARGET=${BACKUP_TARGETDIR}/$(echo ${TARGET} | sed "s#/#_#g").sql
META=${TARGET}.gz.meta
if sqlite3 "$DATABASE_FILE" .dump >"${TARGET}"; then
fetchrc
db._compressDumpfile "${TARGET}" && stat "$DATABASE_FILE" >"${META}"
ls -l ${TARGET}*
else
fetchrc
rm -f "${TARGET}"
fi
fi
done
}
# restore database dump file into database
# param string database dump file (gzipped)
# param string optional: database to import; default: database is parsed from file
function sqlite.restore(){
local sMyfile=$1
local sMyDb=$2
if [ -f "${sMyDb}" ]; then
# echo -n "" > "${sMyDb}"
echo "Deleting ${sMyDb} before restore..."
rm -f "${sMyDb}"
fi
echo -n "Restore ${sMyfile} ... "
color cmd
zcat "${sMyfile}" | sqlite3 "${sMyDb}"
fetchrc
if [ $myrc -eq 0 ]; then
restorePermissions "${sMyfile}" "${sMyDb}"
ls -l "${sMyDb}"
echo
else
color error
echo ERROR while restoring backup.
color reset
fi
}
# --------------------------------------------------------------------------------
# MAIN
# --------------------------------------------------------------------------------
# ----- check requirements
j_requireBinary "sqlite3" 1
if [ $rc -ne 0 ]; then
color.echo error "ERROR: Missing sqlite3 binary. Your Sqlite data cannot be dumped."
else
action=$1
shift 1
"${SERVICENAME}.$action" $*
fi
echo "__DB__$SERVICENAME INFO: $0 $* [$SERVICENAME] final returncode rc=$rc"
# --------------------------------------------------------------------------------