Skip to content

Instantly share code, notes, and snippets.

@sherakama
Last active September 7, 2018 21:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sherakama/451f88592476e77c48e64d1ed0688e19 to your computer and use it in GitHub Desktop.
Save sherakama/451f88592476e77c48e64d1ed0688e19 to your computer and use it in GitHub Desktop.
sites-lando-clone.sh
#!/bin/csh
##################
# Variables
##################
# Path to a place to store your sites.
WEBSERVERROOT=/httpdocs #no trailing slash
# Path to your lando configuration file for Drupal 7
LANDOCONFIG=/Users/admin/scripts/su/d7.lando.yml
# The shortname of the site you are cloning
SHORTNAME=$1
# Wether to include files from the archive dump on the server.
INCLUDEFILES=$2
# Your sunet id
SUNETID="sheamck"
# Lando DB User (Also used in settings.php)
DBUSER="drupal7"
# Lando DB Pass (Also used in settings.php)
DBPASS="drupal7"
# Lando DB Name (Also used in settings.php)
DBNAME="drupal7"
# Lando DB Server (Also used in settings.php)
DBSERVER="database"
# The user on your computer who should own the files.
OWNER="admin"
# The group on your computer who should own the files.
GROUP="staff"
#############
# Prompts
#############
# Must provide a shortname or you cannot continue.
if [ -z "$1" ]; then
echo "You must provide a shortname in parameter #1"
exit;
fi
# If dir exists empty it.
if [ -d $WEBSERVERROOT/$SHORTNAME ]; then
echo "Are you sure you want remove all files and rebuild in $WEBSERVERROOT/$SHORTNAME"
select yepnope in "Yes, I know what I am doing!" "No way. Die die die."; do
case $yepnope in
'Yes, I know what I am doing!' )
echo "Removing all files in $WEBSERVERROOT/$SHORTNAME"
sudo rm -Rf $WEBSERVERROOT/$SHORTNAME
break;;
'No way. Die die die.' )
echo 'Terminating.';
exit;;
esac
done
fi
################
# RUN
################
# Get resources from server.
echo "Starting dump on server..."
if [ "$INCLUDEFILES" = "no-files" ]; then
drush @sse.$SHORTNAME ard --destination=/afs/ir/group/webservices/backups/$SUNETID-copy.tar.gz --tar-options="--exclude=sites/default/files" --overwrite
else
drush @sse.$SHORTNAME ard --destination=/afs/ir/group/webservices/backups/$SUNETID-copy.tar.gz --overwrite
fi
# Get the files from the server.
echo "Copying archive to local..."
scp $SUNETID@sites2.stanford.edu:/afs/ir/group/webservices/backups/$SUNETID-copy.tar.gz $WEBSERVERROOT/$SUNETID-copy.tar.gz
# Unzip
cd $WEBSERVERROOT
echo "Expanding archive on local..."
# sudo drush arr $SUNETID-copy.tar.gz --db-url=mysql://$DBUSER:$DBPASS@$DBSERVER/$DBNAME --destination=$WEBSERVERROOT/$SHORTNAME --debug --overwrite
# Remove unzipped directory.
if [ -d $WEBSERVERROOT/$SUNETID-copy ]; then
echo "Cleaning up old archive expansion"
sudo rm -Rf $WEBSERVERROOT/$SUNETID-copy
fi
# Expand in to new directory.
echo "Creating $WEBSERVERROOT/$SUNETID-copy"
mkdir $WEBSERVERROOT/$SUNETID-copy
# Expand
echo "Unzipping tar to $WEBSERVERROOT/$SUNETID-copy"
tar -zxf $SUNETID-copy.tar.gz -C $WEBSERVERROOT/$SUNETID-copy
# Move public_html in to shortname dir.
echo "Copying public_html to $WEBSERVERROOT/$SHORTNAME"
sudo mv $WEBSERVERROOT/$SUNETID-copy/public_html $WEBSERVERROOT/$SHORTNAME
# Fix up a few permissions
echo "Fixing up a few file permissions"
sudo chown -Rf $OWNER:$GROUP $WEBSERVERROOT/$SHORTNAME
sudo chmod -Rf 0755 $WEBSERVERROOT/$SHORTNAME
# If the user specified no files then create the directory.
if [ "$INCLUDEFILES" = "no-files" ]; then
mkdir $WEBSERVERROOT/$SHORTNAME/sites/default/files
fi
sudo chmod -Rf 0777 $WEBSERVERROOT/$SHORTNAME/sites/default/files
# Clean up a few items
echo "Removing sites specific files."
sudo rm -Rf $WEBSERVERROOT/$SHORTNAME/.git
sudo rm $WEBSERVERROOT/$SHORTNAME/sites/default/settings.local.php
sudo rm $WEBSERVERROOT/$SHORTNAME/sites/default/settings.php
# Add the our files.
echo "Adding our configuration files."
cp $LANDOCONFIG $WEBSERVERROOT/$SHORTNAME/.lando.yml
cp $WEBSERVERROOT/$SHORTNAME/sites/default/default.settings.php $WEBSERVERROOT/$SHORTNAME/sites/default/settings.php
# Replace the shortname from RewriteBase.
sed -i .bak "s/\/$SHORTNAME/\//g" $WEBSERVERROOT/$SHORTNAME/.htaccess
# Comment out a setting that doesn't work with Lando's apache
sed -i .bak '/Header Set Cache-Control/s/^/#/' $WEBSERVERROOT/$SHORTNAME/.htaccess
# Copy the DB over from the extraction so we can import it later.
DBSHORT=$(echo $SHORTNAME | sed 's/\-/_/g')
DBDUMP="ds_$DBSHORT.sql"
echo "Copying database dump to $WEBSERVERROOT/$SHORTNAME/db.sql"
cp $WEBSERVERROOT/$SUNETID-copy/$DBDUMP $WEBSERVERROOT/$SHORTNAME/db.sql
# Set the DB credentials in settings.php
echo "Appending database credentials to settings.php"
cat << FOE >> ${WEBSERVERROOT}/${SHORTNAME}/sites/default/settings.php
\$databases = array(
'default' =>
array (
'default' =>
array (
'database' => '${DBNAME}',
'username' => '${DBUSER}',
'password' => '${DBPASS}',
'host' => '${DBSERVER}',
'port' => '3306',
'driver' => 'mysql',
'prefix' => '',
),
),
);
FOE
# Navigate to the directory
cd $WEBSERVERROOT/$SHORTNAME
# Replace lando shortname
echo "Changing shortname in lando config file."
LANDONAME=$(echo $DBSHORT | sed 's/\_//g')
sed -i .bak "s/\[shortname\]/${LANDONAME}/g" $WEBSERVERROOT/$SHORTNAME/.lando.yml
rm $WEBSERVERROOT/$SHORTNAME/.lando.yml.bak
# Start lando
lando stop
lando rebuild
# Import DB using lando
lando db-import db.sql
# Add custom fixes in order to get the site to work in the lando box.
echo "Starting Lando Drush Command Fixups."
lando drush cc drush
lando drush dis webauth -y
lando drush dis stanford_sites_systemtools -y
lando drush dis stanford_sites_helper -y
# If the user specified no files then enable dummyimage.
if [ "$INCLUDEFILES" = "no-files" ]; then
lando drush dl dummyimage -y
lando drush en dummyimage -y
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment