feat: add single-database restore mode to restore.sh
This commit is contained in:
+55
@@ -86,6 +86,61 @@ cmd_file() {
|
||||
log "Restored file available at: $final"
|
||||
}
|
||||
|
||||
detect_client() {
|
||||
if docker exec "$DB_CONTAINER" sh -c 'command -v mariadb' >/dev/null 2>&1; then
|
||||
CLIENT_BIN="mariadb"
|
||||
else
|
||||
CLIENT_BIN="mysql"
|
||||
fi
|
||||
}
|
||||
|
||||
get_root_creds() {
|
||||
if [[ -n "${MYSQL_ROOT_PASSWORD:-}" ]]; then
|
||||
DB_PASS="$MYSQL_ROOT_PASSWORD"
|
||||
elif [[ -r "$ROOT_PASSWORD_FILE" ]]; then
|
||||
DB_PASS="$(< "$ROOT_PASSWORD_FILE")"
|
||||
else
|
||||
die "no root DB password: set MYSQL_ROOT_PASSWORD or create $ROOT_PASSWORD_FILE (chmod 600)"
|
||||
fi
|
||||
}
|
||||
|
||||
confirm_or_abort() {
|
||||
local dbname="$1" typed
|
||||
[[ "$YES" == true ]] && return 0
|
||||
echo "This will DROP/overwrite database '$dbname'. Type the database name to confirm:"
|
||||
read -r typed
|
||||
[[ "$typed" == "$dbname" ]] || die "confirmation did not match '$dbname' - aborting"
|
||||
}
|
||||
|
||||
restore_single_db() {
|
||||
local sqlfile="$1" dbname="$2"
|
||||
[[ -s "$sqlfile" ]] || die "dump file missing or empty: $sqlfile"
|
||||
run_cmd docker exec -e MYSQL_PWD="$DB_PASS" "$DB_CONTAINER" \
|
||||
"$CLIENT_BIN" -u root -e "CREATE DATABASE IF NOT EXISTS \`$dbname\`;"
|
||||
log "[RUN] docker exec -i ... $CLIENT_BIN -u root $dbname < $sqlfile"
|
||||
docker exec -i -e MYSQL_PWD="$DB_PASS" "$DB_CONTAINER" \
|
||||
"$CLIENT_BIN" -u root "$dbname" < "$sqlfile"
|
||||
}
|
||||
|
||||
cmd_db() {
|
||||
local dbname="$1" dumpfile dest
|
||||
resolve_archive
|
||||
step "Restoring database '$dbname' from archive $RESOLVED_ARCHIVE"
|
||||
if [[ "$DRY_RUN" == true ]]; then
|
||||
log "[DRY-RUN] would extract ${ARCHIVE_TARGET_PATH}/${DUMP_SUBDIR}/${dbname}.sql from ${REPO}::${RESOLVED_ARCHIVE}"
|
||||
log "[DRY-RUN] would DROP/recreate database '$dbname' and import the dump using root credentials"
|
||||
return 0
|
||||
fi
|
||||
confirm_or_abort "$dbname"
|
||||
dest="/tmp/restore-db-$$"
|
||||
dumpfile="$(extract_path "${DUMP_SUBDIR}/${dbname}.sql" "$dest" "$RESOLVED_ARCHIVE" | tail -n1)"
|
||||
detect_client
|
||||
get_root_creds
|
||||
restore_single_db "$dumpfile" "$dbname"
|
||||
log "Database '$dbname' restored from $dumpfile"
|
||||
rm -rf "$dest"
|
||||
}
|
||||
|
||||
usage() {
|
||||
cat <<'EOF'
|
||||
Usage:
|
||||
|
||||
Reference in New Issue
Block a user