Skip to content

Instantly share code, notes, and snippets.

@ErHaWeb
Last active October 27, 2025 16:07
Show Gist options
  • Save ErHaWeb/cd74ca40afea8f6c230dc9dc563cfebc to your computer and use it in GitHub Desktop.
Save ErHaWeb/cd74ca40afea8f6c230dc9dc563cfebc to your computer and use it in GitHub Desktop.
The script automates TYPO3 migration: it imports dumps and SQL files, executes upgrade and database commands, cleans up orphaned and deleted records, updates the reference index, and uses dbdoctor. A lock file prevents parallel runs, and all steps are logged. Use at your own risk. You must know what you are doing.
#!/usr/bin/env bash
# It is recommended to execute this script with the following command:
# bash upgrade/run_migration.sh > var/log/run_migration-`date +%Y-%m-%d-%H-%M-%S`.log
# screen -S upgrade -L -Logfile var/log/run_migration-`date +%Y-%m-%d-%H-%M-%S`.log -d -m bash upgrade/run_migration.sh
#
# recover the background task with:
# screen -r upgrade
#
# unlink from task with
# Ctrl+A d
set -euo pipefail
source ".env"
LOCK_FILE="${BASE_DIR}/run_migration.lock"
log() {
echo "[$(date)] ${2:-"INFO"}: ${1}"
}
error_exit() {
log "An error occurred in the script on line $1" "ERR"
rm -f "${LOCK_FILE}"
exit 1
}
# USAGE: db_import_file $FILE | Import a (gzipped) .sql.gz file
db_import_file() {
local SQL_FILE=$1; shift;
if [ -f "${SQL_FILE}" ]; then
if [[ ${SQL_FILE} =~ \.t?gz$ ]]; then
log "Uncompressing and importing ${SQL_FILE} into '${DB_NAME}' on ${DB_USER}@${DB_HOST}"
zcat "${SQL_FILE}" | mysql --default_character_set=utf8mb4 -h "${DB_HOST}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}"
else
log "Importing ${SQL_FILE} into '${DB_NAME}' on ${DB_USER}@${DB_HOST}"
mysql --default_character_set=utf8mb4 -h "${DB_HOST}" -u "${DB_USER}" -p"${DB_PASS}" "${DB_NAME}" < "${SQL_FILE}"
fi
log "-> Done."
else
log "File '${SQL_FILE}' could not be found..." "ERROR"
fi
}
# Create lock file if it doesn't exist, else throw an error
if [ ! -f "${LOCK_FILE}" ]; then
touch "${LOCK_FILE}"
else
error_exit "Error: Lock file already exists!"
fi
trap 'error_exit $LINENO' ERR
log "Import database"
db_import_file "${BASE_DIR}/backup/${DUMP_FILE}"
log "Run SQL commands before upgrade wizard"
db_import_file "${BASE_DIR}"/upgrade/before-upgrade.sql
log "Update database schema"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "*.add,*.change"
log "Clear cache"
"${BASE_DIR}/vendor/bin/typo3" cache:flush
log "Run upgrade wizard"
"${BASE_DIR}/vendor/bin/typo3" upgrade:run --no-interaction
log "Run SQL commands after upgrade wizard"
db_import_file "${BASE_DIR}"/upgrade/after-upgrade.sql
log "Run fix container parent for connected mode"
"${BASE_DIR}/vendor/bin/typo3" container:fixContainerParentForConnectedMode
log "DB changes: Drop already existing fields with zzz_deleted_ prefix"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "field.drop" --verbose
log "DB changes: Drop already existing tables with zzz_deleted_ prefix"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "table.drop" --verbose
log "DB changes: Add missing fields"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "field.add" --verbose
log "DB changes: Apply pending field changes"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "field.change" --verbose
log "DB changes: Add zzz_deleted_ prefix for fields to be deleted"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "field.prefix" --verbose
log "DB changes: Drop fields with zzz_deleted_ prefix"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "field.drop" --verbose
log "DB changes: Add missing tables"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "table.add" --verbose
log "DB changes: Apply pending table changes"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "table.change" --verbose
log "DB changes: Add zzz_deleted_ prefix for tables to be deleted"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "table.prefix" --verbose
log "DB changes: Drop tables with zzz_deleted_ prefix"
"${BASE_DIR}/vendor/bin/typo3" database:updateschema "table.drop" --verbose
log "Cleanup orphanrecords"
"${BASE_DIR}/vendor/bin/typo3" cleanup:orphanrecords -vv
log "Cleanup deletedrecords"
"${BASE_DIR}/vendor/bin/typo3" cleanup:deletedrecords -v
log "Cleanup missingrelations"
"${BASE_DIR}/vendor/bin/typo3" cleanup:missingrelations -vv --update-refindex
log "Cleanup flexforms"
"${BASE_DIR}/vendor/bin/typo3" cleanup:flexforms -vv
log "Update Reference Index"
"${BASE_DIR}/vendor/bin/typo3" referenceindex:update
log "Run dbdoctor"
"${BASE_DIR}/vendor/bin/typo3" dbdoctor:health -m execute -f "${BASE_DIR}/upgrade/dbdoctor-$(date +%Y-%m-%d-%H-%M-%S).sql" -n
log "Update Reference Index again"
"${BASE_DIR}/vendor/bin/typo3" referenceindex:update
log "Migration complete!"
# Remove lock file
rm -f "${LOCK_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment