Speeding up Extracting a Site from Multisite

Last year I wrote about Extracting a Site from Multisite. The real tedious part is manually moving themes, plugins and uploads relating to a particular subsite over to a standard WordPress install. Luckily this can be automated using bash and Rclone.

Rclone makes scripting over sftp enjoyable.

Rclone supports practically any remote storage provider imaginable. That includes support for sftp. In order to do scripting I recommend adding all WordPress sites to Rclone in the name format of sftp-{sitename}.

SSH and WP-CLI can figure out which folders are needed.

The following script uses a small SSH wrapper sshwp.sh which makes working with WP Engine a bit easier. This magically figures out themes, plugins and uploads then transfers them between servers.

function anchor_wpengine_legacy_multisite_to_single_site {

  # Prompts for multisite site name, single site name and subsite id.
  read -p $'\e[31mMultisite site name (Source)\e[0m: ' site_name_source
  read -p $'\e[31mSingle site name (Destination)\e[0m: ' site_name_destination
  read -p $'\e[31mSubsite ID\e[0m: ' subsite_id

  # Check and sync legacy multisite upload files
  folder_check=$( { rclone lsd sftp-${site_name_source}:wp-content/blogs.dir/${subsite_id}/files/ ; } 2>&1 )
  if [[ $folder_check != *"directory not found"* ]]; then 
    rclone sync sftp-${site_name_source}:wp-content/blogs.dir/${subsite_id}/files/ sftp-${site_name_destination}:wp-content/uploads/ -v
  fi

  # Check and sync modern multisite upload files
  folder_check=$( { rclone lsd sftp-${site_name_source}:wp-content/uploads/sites/${subsite_id}/ ; } 2>&1 )
  if [[ $folder_check != *"directory not found"* ]]; then 
    rclone sync sftp-${site_name_source}:wp-content/uploads/sites/${subsite_id}/ sftp-${site_name_destination}:wp-content/uploads/ -v
  fi

  subsite_url=$( sshwpe.sh $site_name_source "wp site list --site__in=${subsite_id} --field=url" )
  active_themes=( $( sshwpe.sh $site_name_source "wp theme list --url=${subsite_url} --status=parent --field=name; wp theme list --url=${subsite_url} --status=active --field=name" ) )
  active_plugins=( $( sshwpe.sh $site_name_source "wp plugin list --status=active-network --field=name; wp plugin list --url=${subsite_url} --status=active --field=name" ) )

  for theme in ${active_themes[@]}; do 
    rclone sync sftp-${site_name_source}:wp-content/themes/${theme} sftp-${site_name_destination}:wp-content/plugins/${theme} -v
  done
  
  for plugin in ${active_plugins[@]}; do 
    rclone sync sftp-${site_name_source}:wp-content/plugins/${plugin} sftp-${site_name_destination}:wp-content/plugins/${plugin} -v
  done

  sshwpe.sh $site_name_destination "wp plugin activate --all"

}
anchor_wpengine_legacy_multisite_to_single_site