Sync TYPO3 Production to Staging with a Single Local Command
In some of my TYPO3 projects, I sometimes like to sync all contents from the production system to the staging system. Long time, I connected to the servers and did this manually, which always was a waste of time. And sometimes I even forgot to sync some folders or synced large log tables or log files, which I did not need and costs extra time.
Requirements #
- helhum/typo3-console must be installed on both environments.
- You must be allowed to open SSH connections to both hostings. I recommend to use login via SSH key.
- Your production system needs to have SSH access to your staging system to copy some data
The Script #
Place the script anywhere in your project, e.g. in file src/local/sync-production-staging.sh
and make it executable.
To use it, you simply can run src/local/sync-production-staging.sh
.
The paths are optimized for a project, which as deployed with TYPO3 Surf. If you are using another tool with other paths, you need to adapt them.
#!/usr/bin/env bash
# This script copies the current production data to staging without the need to log in to one of the systems manually.
#
# Requirements:
# * you need to have SSH access to both of the systems
# * your production system needs to have SSH access to your staging system to copy some data
PRODUCTION_HOST='prod-user@www.example.org'
PRODUCTION_BASE='/var/www/production'
PRODUCTION_PHP_BIN='/usr/bin/php8.4'
STAGING_HOST='stage-user@staging.example.org'
STAGING_BASE='/var/www/staging/'
STAGING_PHP_BIN='/usr/bin/php8.4'
read -p "This deletes current staging contents and replaces it with current production contents. Are you sure? (y|n) " -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
# do jobs on production host
ssh -A $PRODUCTION_HOST "\
echo 'create and copy database dump'
$PRODUCTION_PHP_BIN $PRODUCTION_BASE/releases/current/vendor/bin/typo3 database:export -e '[bf]e_sessions' -e sys_file_processedfile -e sys_http_report -e sys_news -e sys_log -e sys_history -e tx_extensionmanager_domain_model_extension -e tx_formtodatabase_domain_model_formresult -e 'cache_*' -e 'cf_*' -e 'zzz_deleted_*' | gzip > $PRODUCTION_BASE/shared/temp-sync-production-to-staging.sql.gz
echo 'copy files from production to staging'
rsync -auv $PRODUCTION_BASE/shared/Data/fileadmin $PRODUCTION_BASE/shared/Data/media $STAGING_HOST:$STAGING_BASE/shared/Data --delete --exclude=_processed_
rsync $PRODUCTION_BASE/shared/temp-sync-production-to-staging.sql.gz $STAGING_HOST:$STAGING_BASE/shared/
"
# do jobs on staging host
ssh -A $STAGING_HOST "
echo 'import database on staging'
zcat $STAGING_BASE/shared/temp-sync-production-to-staging.sql.gz | $STAGING_PHP_BIN $STAGING_BASE/releases/current/vendor/bin/typo3 database:import
echo \"UPDATE sys_redirect SET source_host='staging.example.org' WHERE source_host='www.example.org'\" | $STAGING_PHP_BIN $STAGING_BASE/releases/current/vendor/bin/typo3 database:import
echo 'prepare TYPO3'
$STAGING_PHP_BIN $STAGING_BASE/releases/current/vendor/bin/typo3 database:updateschema
$STAGING_PHP_BIN $STAGING_BASE/releases/current/vendor/bin/typo3 cache:flush
"
fi
Update 2025/04/30 #
- Exclude tables
[bf]e_sessions
,sys_http_report
andtx_extensionmanager_domain_model_extension
from database export - Add
--delete
flag torsync
command
Update 2025/06/24 #
- Replace production host names with staging host names in
sys_redirect
table