By davecormier on
This is my first sizable bash script (if sizable it is). Looking for advice, critiques and...
It allows a user to copy an existing multisite installation and convert it to a new one with the content truncated out of it. I'm going to start using it for my stuff in the morning... any comments more than welcome!
Do not trust this draft on your production server
#! /bin/bash
SCRIPTUSER="root"
MyUSER="id"
MyPASS="pass"
GroomPrefix="tempprefix_"
MyHOST="localhost"
SitesHome="/var/www/drupal/sites"
Prefix=""
MultisitePrefix="mysite.com"
date=`date +%Y%m%d`
log_filename=/home/user/addsite.log
ThemePrefix="theme_"
if [ $(whoami) != $SCRIPTUSER ]; then
echo "Must be "$SCRIPTUSER "to run $0"
exit 1;
fi
#Choose a database to copy from
echo -n "Pick a site to copy from: "
read -e MasterDatabase
#Set the database Prefix
MasterDatabasePrefix="${MasterDatabase}_"
#Choose the website name you wish to create
echo -n "Pick a site to create mynewsite not mysite.com/mynewsite: "
read -e NewSite
#Check to see if database exists
DBS=`mysql -u$MyUSER -p$MyPASS -Bse 'show databases'| egrep -v 'information_schema|mysql'`
for db in $DBS; do
if [ "$db" = "$NewSite" ]
then
echo "This database already exists : exiting now"
exit
fi
done
#Dump the info from the MasterDatabase into an sql file
mysqldump -u ${MyUSER} -p${MyPASS} ${MasterDatabase} > ${NewSite}.sql
#Change the database prefix to match the new site, dump to temp
sed -e 's/'"$MasterDatabasePrefix"'/'"$NewSite"'_/g' ${NewSite}.sql > TempDatabase.sql
#Remove the sql file (this should be cleaned up)
rm ${NewSite}.sql
#Create a temporary database for working on the database
mysqladmin -u$MyUSER -p$MyPASS create TempDatabase
mysql -u$MyUSER -p$MyPASS TempDatabase < TempDatabase.sql
#Change the prefix from the database grooming file and then export that file
sed -e 's/'"$GroomPrefix"'/'"$NewSite"'_/g' groom_db_template.sql > groom_db_template_temp.sql
#use the new grooming file to truncate the database of non-standard content and export to new sql file
mysql -f -u$MyUSER -p$MyPASS TempDatabase < groom_db_template_temp.sql
mysqldump -u ${MyUSER} -p${MyPASS} TempDatabase > ${NewSite}.sql
#take new sql file and use it to populate new database. delete tmp database
mysqladmin -u$MyUSER -p$MyPASS create $NewSite
mysql -u$MyUSER -p$MyPASS $NewSite < ${NewSite}.sql
mysqladmin -f -u$MyUSER -p$MyPASS drop TempDatabase
#Remove the sql files (this should be cleaned up)
rm groom_db_template_temp.sql
rm TempDatabase.sql
#copy files etc from master site to new location
cp -ar ${SitesHome}/${MultisitePrefix}.$MasterDatabase ${SitesHome}/${MultisitePrefix}.$NewSite
# Make files directories writeable
# It checks if it exists and then modifies
if [ -e ${SitesHome}/${MultisitePrefix}.${NewSite}/files ]; then
chmod 777 ${SitesHome}/${MultisitePrefix}.${NewSite}/files
else
echo $SitesHome"/"$MultisitePrefix"."$NewSite"/files does not exists"
fi
if [ -e ${SitesHome}/${MultisitePrefix}.${NewSite}/files/tmp ]; then
chmod 777 ${SitesHome}/${MultisitePrefix}.${NewSite}/files
else
echo $SitesHome"/"$MultisitePrefix"."$NewSite"/files/tmp does not exist"
fi
if [ -e ${SitesHome}/${MultisitePrefix}.${NewSite}/files/images ]; then
chmod 777 ${SitesHome}/${MultisitePrefix}.${NewSite}/files
else
echo $SitesHome"/"$MultisitePrefix"."$NewSite"/files/images does not exist"
fi
if [ -e ${SitesHome}/${MultisitePrefix}.${NewSite}/files/images/import ]; then
chmod 777 ${SitesHome}/${MultisitePrefix}.${NewSite}/files
else
echo $SitesHome"/"$MultisitePrefix"."$NewSite"/files/images/import does not exist"
fi
if [ -e ${SitesHome}/${MultisitePrefix}.${NewSite}/files/images/temp ]; then
chmod 777 ${SitesHome}/${MultisitePrefix}.${NewSite}/files
else
echo $SitesHome"/"$MultisitePrefix"."$NewSite"/files/images/temp does not exists"
fi
#Clean out the existing files and create new theme file
rm -r ${SitesHome}/${MultisitePrefix}.${NewSite}/files/*
if [ -e ${SitesHome}/${MultisitePrefix}.${NewSite}/themes/upei_${MasterDatabase} ]; then
mv ${SitesHome}/${MultisitePrefix}.${NewSite}/themes/${ThemePrefix}${MasterDatabase} ${SitesHome}/${MultisitePrefix}.${NewSite}/themes/upei_${NewSite}
else
echo $SitesHome"/"$MultisitePrefix"."$NewSite"/themes/"${ThemePrefix}${MasterDatabase}" does not exist"
fi
# find $db_url and replace with # $db_url
#
cd ${SitesHome}/${MultisitePrefix}.${NewSite}/
mv settings.php settings.php.${date}
sed "s@db_url@db_url = 'mysql://${MyUSER}:${MyPASS}\@${MyHOST}/${NewSite}'\; #@" <settings.php.${date} >settings.php.${date}b
# Do the same for the prefix
sed "s@db_prefix@db_prefix = '${NewSite}_'\; #@" <settings.php.${date}b >settings.php
chmod 444 ${SitesHome}/${MultisitePrefix}.${NewSite}/settings.php
# create a new symlink linking /files/bucket to the sites folder
if [ -e ${SitesHome}/../files/${NewSite} ]; then
ln -s ${SitesHome}/${MultisitePrefix}.${NewSite}/files ${SitesHome}/../files/${NewSite}
else
echo $SitesHome"../files"$Newsite"does not exist."
fi
ln -s ${SitesHome}/../ ${SitesHome}/../$NewSite
echo `date` OK we are finished >> ${log_filename}
groom_db_template.sql contents below.
truncate tempprefix_cache;
truncate tempprefix_cache_content;
truncate tempprefix_cache_filter;
truncate tempprefix_cache_menu;
truncate tempprefix_cache_page;
truncate tempprefix_cache_views;
truncate tempprefix_node;
truncate tempprefix_url_alias;
truncate tempprefix_sessions;
delete from tempprefix_users where uid > 2;
truncate tempprefix_node_access;
truncate tempprefix_node_comment_statistics;
truncate tempprefix_node_counter;
truncate tempprefix_node_revisions;
truncate tempprefix_accesslog;
truncate tempprefix_comments;
truncate tempprefix_content_field_email;
truncate tempprefix_content_field_faculty_photo;
truncate tempprefix_content_field_link;
truncate tempprefix_content_field_office;
truncate tempprefix_content_field_phone;
truncate tempprefix_content_field_title;
truncate tempprefix_content_type_event;
truncate tempprefix_content_type_faculty;
truncate tempprefix_content_type_image;
truncate tempprefix_content_type_notice;
truncate tempprefix_content_type_page;
truncate tempprefix_content_type_staff;
truncate tempprefix_content_type_story;
truncate tempprefix_content_type_webform;
cheers. dave.
Comments
good stuff
This looks useful, good work :)
--
John Forsythe
Need reliable Drupal hosting?
you might find this interesting
I've been working on something similar and have found this post interesting: a more secure drupal [multisite] install.