Migrate a Composer-based Drupal Site to a Build Tools Site
Migrate a Drupal site created via Pantheon Dashboard (or Terminus) to a Build Tools-based site.
This guide shows you how to migrate a Composer-based Drupal site (site created via Pantheon dashboard or Terminus) to a Build Tools-based site.
If this is not your scenario, see Drupal Migration Guides for additional upgrade paths.
Overview
Drupal sites on Pantheon have Integrated Composer built-in to manage site dependencies. A Drupal site with Build Tools also provides site dependency management, as well as an external repository and a Continuous Integration workflow setup.
The goals of this migration are to:
Create a new Drupal site in Pantheon using the Terminus Build Tools plugin
Import your existing codebase, database, and files into your new site
Will This Guide Work for Your Site?
Confirm that your site meets the following requirements before you continue:
Ensure your site has the Pantheon
drupal-composer-managed
repository in its upstream.Use Terminus to confirm the
drupal-composer-managed
UpstreamRun the command
terminus site:info $SITE
to display the site's basic information and properties.The following is an abridged example of the output for a site upstream set to
drupal-composer-managed
:terminus site:info $SITE ------------------ ------------------------------------------------------------------------------------- ID abdc3ea1-fe0b-1234-9c9f-3cxeAA123f88 Name anita-drupal Label AnitaDrupal Created 2019-12-02 18:28:14 Framework drupal8 ... Upstream 897fdf15-992e-4fa1-beab-89e2b5027e03: https://github.com/pantheon-upstreams/drupal-composer-managed.git... ------------------ -------------------------------------------------------------------------------------
The following values indicate that a site is using a
drupal-composer-managed
upstream:The
Framework
isdrupal8
The
Upstream
includeshttps://github.com/pantheon-upstreams/drupal-composer-managed.git
- You are able to create a new Drupal site using Terminus Build Tools
Before You Begin
Clone your existing site to your local environment following the git clone
command from the dashboard.
The existing site's commit history will no longer exist after migrating to the new site.
Create a New Terminus Build Tools Drupal Site
Follow the Terminus Build Tools Documentation to create a new Drupal site:
terminus build:project:create --git=github --team='My Agency Name' d9 my-buildtools-site
Wait for the site to be created and for the first build to complete.
Prepare the Local Environment
Review our documentation on Git, Composer, and Terminus, and install and configure them on your local computer. Pantheon requires Composer 2 at minimum.
Mac users can use Homebrew to install Git, Composer, and PHP, along with their required dependencies. Note: Terminus 3 should be used for PHP >= 8.0. Restart the shell or terminal environment after entering the following command:
brew install git composer php
Windows users can install Composer and Git, and may need to install XAMPP or similar to satisfy some dependencies.
Get a local copy of both your new site (from the external repository) and your existing site codebase.
Set the following temporary variables in your terminal session to match your folders location and sites names:
export SOURCE=/absolute/path/to/source/site/codebase export DESTINATION=/absolute/path/to/codebase/cloned/from/pantheon export SOURCE_SITE_NAME=my-source-site export DESTINATION_SITE_NAME=my-buildtools-site
Copy Existing Configuration
Copy any existing configuration from the source site and update the source path as needed to match your configuration folder:
rsync -avz $SOURCE/config/ $DESTINATION/config/ --delete --delete-after
# From $DESTINATION:
git add config -A
git commit -m "Pull in configuration from source site"
It is possible that the Drupal site might have relocated the configuration path to a different location. You can find your config.yaml
files are via:
terminus drush $SOURCE_SITE_NAME.dev -- status --fields=config-sync
In some cases no files are copied through this step. This is not cause for concern.
Add Contributed and Custom Code
This section describes how to replicate your selection of contributed modules and themes, and any custom modules or themes your development team has created in your new project structure.
Contributed Code
The goal of this process is to have Composer manage all the site's contrib modules, contrib themes, core upgrades, and libraries (referred to as contributed code). The only items from the existing site that should remain in the Git repository are custom code, custom themes, and custom modules that are specific to the existing site.
Modules and Themes
Your site should already be managing contributed modules and themes through Composer. Follow the steps below to migrate these items to a new site.
Open the source site
composer.json
.Run the
composer require
command for each module and theme in the$DESTINATION
directory:composer require drupal/PROJECT_NAME:^VERSION
You can require multiple packages in the same command if desired.
Other Composer Packages
If you added non-Drupal packages to your site via Composer, use the following steps:
Run the command
composer require
to migrate each package.Use the following command to display the differences between the versions of
composer.json
:diff -Nup --ignore-all-space $SOURCE/composer.json $DESTINATION/composer.json
Libraries
Libraries can be handled similarly to modules, but the specifics depend on how your library code was included in the source site. If you're using a library's API, you may have to do additional work to ensure that it functions properly.
Do not forget to commit your changes during these steps.
Custom Code
Manually copy custom code from the existing site repository to the Composer-managed directory.
Modules and Themes
Run the following commands to move modules:
mkdir -p $DESTINATION/web/modules/custom cp -r $SOURCE/web/modules/custom $DESTINATION/web/modules/custom # From $DESTINATION: git add web/modules/ git commit -m "Copy custom modules"
Run the following commands to move themes:
mkdir -p $DESTINATION/web/themes/custom cp -r $SOURCE/web/themes/custom $DESTINATION/web/themes/custom # From $DESTINATION: git add web/themes/ git commit -m "Copy custom themes"
Use the above commands to move custom code (if any) to your new site.
settings.php
Your existing site may have customizations to settings.php
or other configuration files. Given that both sites ($SOURCE
and $DESTINATION
) have been created from the same upstream, it is ok to replace the $DESTINATION
settings.php
with the one coming from the $SOURCE
site:
cp $SOURCE/web/sites/default/settings.php $DESTINATION/web/sites/default/settings.php
# Review changes and commit as needed
The resulting settings.php
should not have a $databases
array.
Additional Composer Configuration
Any additional Composer configuration that you have added to your site should be ported over to the new composer.json
file. This can include configurations related to repositories, minimum-stability, or extra sections.
Use the
diff
command to get the information you need to copy:diff -Nup --ignore-all-space $SOURCE/composer.json $DESTINATION/composer.json
Commit your changes as needed.
Push to the External Repository Master Branch
Push to the master branch in the external repository:
git push origin master
Confirm that the Continuous Integration workflow succeeds in committing your code changes to the Pantheon site.
Add Your Database
The Database import requires a single .sql
dump that contains the site's content and configurations.
Create a
.sql
dump using the mysqldump utility.Compress the resulting archive with gzip to reduce the size for a faster transfer:
mysqldump -uUSERNAME -pPASSWORD DATABASENAME > ~/db.sql gzip ~/db.sql
- Replace
USERNAME
with a MySQL user with permissions to access your site's database. - Replace
PASSWORD
with the MySQL user's password. To force a password prompt, move-p
to the end of the command and leave it blank. This prevents your MySQL password from being visible on your terminal. - Replace
DATABASE
with the name of your site database within MySQL. ~/db.sql
defines the output target to a file nameddb.sql
in your user's home directory. Adjust to match your desired location.
- Replace
The resulting file will be named db.sql.gz
You can use either the Pantheon Dashboard or a MySQL client to add your site's database.
Select the Dev environment in the Site Dashboard.
Select Database / Files.
Click Import and add your archive accordingly (based on file size):
If your archive is under 100MB, upload the file directly:
Navigate to the MySQL database field > click File > Choose File.
Select your local archive file > click Import.
Note: If you recently imported the database and need to re-import, refresh the page and use a new filename for the database file.
If your archive is less than 500MB, import it from URL:
Navigate to the MySQL database field.
Click URL.
Paste a publicly accessible URL for the
.sql.gz
file > click Import.Info:NoteChange the end of Dropbox URLs from
dl=0
todl=1
to import your archive correctly.
The following instructions allow you to add database archives larger than 500MBs using the command line MySQL client. You can also use a GUI client like Sequel Ace or Navicat. For more information, see Accessing MySQL Databases.
Navigate to the Pantheon Site Dashboard.
Open the Dev environment.
Click Connection Info.
Copy the Database connection string.
The Database connection string will look similar to this:
mysql -u pantheon -p{random-password} -h dbserver.dev.{site-id}.drush.in -P {site-port} pantheon
cd
into the directory containing your.sql
file in your terminal.Paste the connection string and append it with:
< database.sql
.Your command will look like:
mysql -u pantheon -p{random-password} -h dbserver.dev.{site-id}.drush.in -P {site-port} pantheon < database.sql
If you encounter a connection-related error, the DB server could be in sleep mode. To resolve this, load the site in your browser to wake it up, and try again. For more information, see Troubleshooting MySQL Connections.
The
.sql
file is imported to the Dev environment after you run the command.
Back Up the tokens.json file
Connect to your site using SFTP command or credentials from your dashboard and get a backup of the following file:
files/private/.build-secrets/tokens.json
Use the SFTP
get
command to download the file to your local directory (this is only for SFTP command line use):echo "get files/private/.build-secrets/tokens.json" | $(terminus connection:info $DESTINATION_SITE_NAME.dev --format=string --field=sftp_command)
Upload Your Files
Files refer to everything stored inside sites/default/files
. This usually consists of uploaded images, generated stylesheets, aggregated scripts, etc. Files are not under Git version control and are stored separately from the site's code.
You can use the Pantheon Dashboard, SFTP, or Rsync to upload your site's files.
Follow the steps below to export a tar.gz
or .zip
file of your directory files.
Export a
tar.gz
or.zip
file of your files directory:Navigate to your Drupal site's root directory to run this command, which will create an archive file in your user's home directory:
cd sites/default/files tar -czf ~/files.tar.gz .
Select the Dev environment in the Site Dashboard.
Select Database / Files.
Click Import and add your archive accordingly (based on file size):
If your archive is under 100MB, you can upload the file directly:
Click File in the MySQL database field > Choose File.
Select your local archive file and click Import.
Note: If you recently imported the database and need to re-import, refresh the page and use a new filename for the database file.
If your archive is less than 500MB, you can import it from URL:
Click URL in the Archive of site files field.
Paste a publicly accessible URL for the archive and click Import.
- Change the end of the Dropbox URLs from
dl=0
todl=1
to ensure that your archive imports correctly.
- Change the end of the Dropbox URLs from
Rsync is an excellent method for transferring a large number of files. After performing an initial rsync, subsequent jobs will only transfer the latest changes. This can help minimize the amount of time a site is in an unpredictable state (or offline) during the final step of a migration, as it allows you to bring over only new content, rather than re-copying every single file.
We recommend looking into the Terminus Rsync Plugin as a helper when doing these operations, as the number of command line arguments and specifics of directory structure make it easy for human error to impact your operation.
To sync your current directory to Pantheon, run the following command:
terminus rsync . my_site.dev:files
When using Rsync manually, the script below is useful for dealing with transfers that are interrupted due to connectivity issues. It uploads files to your Pantheon site's Dev environment. If an error occurs during transfer, it waits two minutes and picks up where it left off:
#!/bin/bash # manual-rsync-script.sh # runs Rsync and waits two minutes if it doesn't work ENV='dev' SITE='SITEID' read -sp "Your Pantheon Password: " PASSWORD if [[ -z "$PASSWORD" ]]; then echo "Whoops, need password" exit fi while [ 1 ] do sshpass -p "$PASSWORD" rsync --partial -rlvz --size-only --ipv4 --progress -e 'ssh -p 2222' ./files/* --temp-dir=../tmp/ $ENV$SITE@appserver.$ENV.$SITE.drush.in:files/ if [ "$?" = "0" ] ; then echo "rsync completed normally" exit else echo "Rsync failure. Backing off and retrying..." sleep 180 fi done
Restore tokens.json file
Connect to your site using SFTP command or credentials from your dashboard to restore the backup of the
tokens.json
file:files/private/.build-secrets/tokens.json
Use the SFTP
put
command to upload the file from your local directory (only if using the SFTP command line):Info:NoteYou must run this from the directory where the
tokens.json
backup was downloaded.echo "put files/private/.build-secrets/tokens.json" | $(terminus connection:info $DESTINATION_SITE_NAME.dev --format=string --field=sftp_command)
You should now have all three of the major components of your site imported into your new site and CI should be working.
Clear the caches on the Pantheon Dashboard.
Troubleshooting
Provided Host Name Not Valid
Update your settings.php
file with a trusted host setting, if you receive the following error message:
The provided host name is not valid for this server
Refer to the Trusted Host Setting documentation for more information.
Working With Dependency Versions
Version compatibility issues can occur when packages pulled by Composer are updated along with their dependencies. If this happens, you will need to manually alter the version constraints on a given package in the require
or require-dev
section of composer.json
to update the packages. Refer to the updating dependencies section of the Composer documentation for more information.
Troubleshoot package updates by running composer update
. This updates composer.lock
to the latest available packages. Package updates are constrained by version requirements in composer.json
.