Files that can be excluded from backup If you need to restore a Drupal 8 site from a backup, which files are essential?


core -> - 
modules -> +
profiles -> + 
sites/default/ -> +
sites/default/files/config_* -> - 
sites/default/files/css -> -
sites/default/files/js -> -
sites/default/files/php -> -
sites/default/files/styles -> - 
sites/default/files/field -> + (and any other user uploads)
themes -> +
vendor -> -


"-" means not needed, because Drupal core or cached files; "+" means files necessary to restore a Drupal 8 site.

For a backup script that allows to restore a site on another server in case of a server crash, can I leave out all cache data, or should I backup some of them to run Drupal without problems?
The key directories are modules, themes, and profiles. You also want to backup all settings.php files, and uploaded files as well. Although, I would separate the two into two backup files, since, depending on the site, the uploaded files directory could be in the low MBs or hundreds to even GB of files. Give them both a readable date timestamp for a filename.

Also, backup your composer.json and composer.lock files. If you control your site build using Drupal Project or Acquia BLT which has a composer driven approach, composer.json will contain the information needed to rebuild your site (this does not include data, only code). Then it's just a matter of backing up custom modules and custom themes, since composer can fetch all the code and rebuild core.