How to update Drupal core and modules safely with Composer and Drush
Drupal security advisories are published for core and contributed modules and are often exploited within days. A Composer-based site can be updated in a few commands, as long as you back up first and run the database updates.
Back up code and database
Keep the backup outside the web root.
cd /path/to/drupal drush sql:dump --gzip --result-file=~/drupal-$(date +%F).sql tar czf ~/drupal-files-$(date +%F).tgz --exclude=web/sites/default/files/php .Put the site in maintenance mode
Visitors see a notice while the code and database are out of step.
drush state:set system.maintenance_mode 1 --input-format=integer drush cache:rebuildSee what is outdated
Composer lists the packages with updates; focus on security releases first.
composer outdated "drupal/*" drush pm:securityUpdate core and modules
The with-all-dependencies flag updates what core needs.
composer update "drupal/core-*" --with-all-dependencies composer update drupal/MODULE --with-dependenciesRun database updates and clear caches
This applies the schema changes that come with the update.
drush updatedb --yes drush config:import --yes # only if you use configuration management drush cache:rebuildReopen and check
Take the site out of maintenance mode and read the status report.
drush state:set system.maintenance_mode 0 --input-format=integer drush core:requirements --severity=1
Common mistakes
- Running composer as root: file permissions break.
- Updating a module that is not compatible with the new core: read its release notes.
- Skipping drush updatedb: the site shows errors until it runs.
How to undo it
Restore the files and the database from the backup, or git checkout composer.lock and run composer install.
Sources
Commands are for Debian and Ubuntu unless noted. Test on a copy first and keep a backup: you are responsible for changes on your own systems.