Drupal: Difference between revisions
mNo edit summary |
Add information about managing config and database backups after a successful deployment and installation. |
||
| Line 43: | Line 43: | ||
</syntaxhighlight></blockquote> | </syntaxhighlight></blockquote> | ||
==== Capturing Server Resources Before Your Next Rebuild ==== | |||
After installing Drupal on NixOS using the admin UI, you will want to export the MYSQL database of your website and export the Drupal site configuration. Doing both steps makes it possible to implement continuous integration and continuous delivery on the environment you just created. | |||
Backing up your Drupal configuration (with git or similar) and database is recommended, especially after a successful install of a new Drupal instance.<blockquote>'''Warning:''' When using a Drupal package that has an existing set of configuration in the config sync directory (or using the configRoot option in nix), it is '''mandatory''' to import a working SQL database from another working installation. Failing to do so will cause your website to not function correctly. | |||
The advised development workflow for starting a Drupal website (or websites) on NixOS is to do the following | |||
# Install a new instance of Drupal on NixOS. The configRoot option should be disabled. | |||
# Once the installation is complete, export the database, and export the configuration. See the sections below on the recommended way to do both of these tasks. | |||
# Integrate the contents of the config sync directory (located, by default, at /var/lib/drupal/<hostname>/config) back into your upstream Drupal package. | |||
# During your next rebuild, make sure to import any configuration changes by visiting '''Manage > Configuration > Development > Configuration synchronization''' in the Drupal admin. | |||
# If you intend on deploying this exact same site elsewhere, take regular backups of your database. | |||
# Deploy your saved database and the source code into a new instance of Drupal on NixOS at the same time to avoid a chicken-and-egg scenario. | |||
</blockquote> | |||
===== Exporting The Database ===== | |||
As root, you can export the database to a local backup using bash script like this. | |||
<syntaxhighlight lang="bash"> | |||
DATE="$(date +'%F-%T')" | |||
BACKUP_DIR="/var/lib/drupal/sql-backups/$DRUPAL_HOSTNAME" # Replace the hostname with your Drupal hostname from services.drupal.sites.<hostname>. | |||
mkdir -p "$BACKUP_DIR" | |||
# Replace the variables in this line with the values defined in your services.drupal configuration. | |||
mariadb-dump -u root -$DB_NAME -P $DB_PORT -h $DB_HOST | gzip > "$BACKUP_DIR/$DATE-$DRUPAL_HOSTNAME-auto-backup.sql.gz" | |||
</syntaxhighlight> | |||
<blockquote>'''Note:''' The <code>$DB_NAME</code>, <code>$DB_PORT</code>, and <code>$DB_HOST</code> variables in the above script need to be replaced with actual values from your Drupal configuration. By default, these values are <code>drupal</code>, <code>3306</code>, and <code>localhost</code>, respectively. | |||
Likewise, replace instances of <code>$DRUPAL_HOSTNAME</code> with the hostname you defined under <code>services.drupal.sites.<hostname></code></blockquote> | |||
===== Exporting The Configuration ===== | |||
Refer to this guide on Drupal.org for how to [https://www.drupal.org/docs/administering-a-drupal-site/configuration-management/managing-your-sites-configuration export your configuration yamls]. | |||
Drupal uses the files in the config sync directory as a way to implement the concept of "migrations" that you find in many other web frameworks like Ruby on Rails or Django. Whenever you change certain settings in the backend of your website, Drupal can export those database changes into a file to preserve that change in code. Config changes made on a lower environment can then be pushed up to a higher environment as code and imported to change the database in a predictable way. | |||
Configuration on a Drupal instance in NixOS will be synchronized to the location defined by the <code>configSyncDir</code> option in your nix configuration. By default, this location is set to <code>/var/lib/drupal/<hostname>/config/sync</code>. | |||
When you export your Drupal configuration on NixOS, make sure to save this configuration in another location immediately afterward, or it may be overridden on the next rebuild or on the next reboot. | |||
Generally, you should integrate the config you export from the NixOS environment back into a git repo that also contains your Drupal source code. This will ensure that the Drupal instance you deploy into NixOS can import any config changes from git. | |||
If you Drupal package come with configuration in a config sync directory (or similar), make sure to tell NixOS where to find it with this expression.<syntaxhighlight lang="nix"> | |||
# Tell NixOS where to look for the config sync directory in the Drupal package. | |||
services.drupal.sites."my-host-name.local".configRoot = "/config"; | |||
# The contents of the /config directory (defined above) will be copied into the path defined by the configSyncDir setting MINUS the trailing "/sync" path, if it exists. | |||
services.drupal.sites."my-host-name.local".configSyncDir = "/var/lib/drupal/my-host-name.local/config/sync"; | |||
</syntaxhighlight>If you use the default setting for <code>configSyncDir</code>, you can simply add the first line of code and substitue your hostname. | |||
== Configuration == | == Configuration == | ||