 | Use this section to share information and/or script code for automatically installing and/or replicating CiviCRM (and host CMS) sites. |
Automated Installer for a Pre-configured CiviCRM Site
Contributed by Larry Hedrick of DharmaTech.org
The following 2 scripts are used to install a drupal/civicrm system. When the scripts complete a user will be able to login as civiadmin with pw civiadmin01. The script works from a tar file built from a working template and uses a database import file built with mysql dump. A new user is created in the /etc/passwd file and a perl script is used to generate an encrypted password.
There is a template settings.php and civicrm.settings.php file which has marker strings which are replaced with new values for the new site using the sed editor.
The install script also appends the name of the new user account to a file. This file is holds the names of sites. A cron job will then loop through the names doing a tar file backup and a mysql dump each day.
This is a work in progress. If others can offer suggestions or see problems please post or send email to lhedrick at dharmatech dot org.
#!/bin/sh
client_name=$1
mysql_host=mysql.dharmatech.org
mysql_admin_user=dtmysqladmin
mysql_admin_pw=xyzzy.
template_path=/home/software/template
script_path=/home/software/scripts
cron_daily_path=/home/software/cron.daily
client_path=/home/${client_name}
drupal_tar_file=${template_path}/drupal_current.tar.gz
civicrm_restore_file=${template_path}/civicrm_current
if [ "$#" -lt 1 ]
then
echo -e "usage:newclient newclient_name"
exit
fi
#test for root
user=`whoami`
if [ "$user" != "root" ]
then
echo "must be root"
exit
fi
#see if this is a new user name
egrep -q ^${client_name} /etc/passwd
if [ $? -eq 0 ]
then
echo -e "${client_name} already exists".
exit
fi
#check for drupal tar file
if [ ! -f $drupal_tar_file ]
then
echo "drupal tar file ${drupal_tar_file} does not exsist"
exit
fi
#check for civicrm mysql base restore file
if [ ! -f ${civicrm_restore_file} ]
then
echo "civicrm mysql restore file ${civicrm_restore_file} does not exsist"
exit
fi
#the civicrm admininistrator will always be civiadmin
#which is different then the client name
#the -p option requires and encrypted passwd
#call the perlcrypt script to generate the encoded password which will
#be placed into /etc/shadow
useradd -m -p `${script_path}/perlcrypt ${client_name}01` ${client_name}
echo -e "user ${client_name} added with password ${client_name}01"
mkdir /home/$client_name/public_html
chmod 755 /home/${client_name}/public_html
echo -e "loading current drupal files to public_html"
#tar in drupal files (using the template tar file
cd /home/${client_name}/public_html
tar -xp --ungzip <${drupal_tar_file}
cd /home/${client_name}
chown -R www-data:www-data public_html
mysql -h ${mysql_host} -u ${mysql_admin_user} -p${mysql_admin_pw} <<INLINE_END
create database $client_name;
GRANT SELECT, INSERT, UPDATE, DELETE, CREATE, DROP, INDEX, \
ALTER, CREATE TEMPORARY TABLES, LOCK TABLES \
ON ${client_name}.* \
TO '${client_name}'@'${mysql_host}' IDENTIFIED BY '${client_name}01';
exit
INLINE_END
#load civicrm base database template
mysql -h ${mysql_host} -u ${client_name} -p${client_name}01 ${client_name} <${civicrm_restore_file}
#cleanout the drupal sessions table since this will be a new site
mysql -h ${mysql_host} -u ${client_name} -p${client_name}01 ${client_name} <<INLINE_END
truncate table sessions;
exit
INLINE_END
#copy in the default configuration files
cd /home/${client_name}/public_html/sites/default
rm -f settings.php
rm -f civicrm.settings.php
cp ${template_path}/settings.php ./
cp ${template_path}/civicrm.settings.php ./
chown www-data settings.php
chown www-data civicrm.settings.php
cd /home/${client_name}/public_html
cp ${template_path}/dothtaccess ./.htaccess
#edit the drupal and civicrm configuration files
cd /home/${client_name}/public_html/sites/default
echo -e "s/client_name/${client_name}/g" >/tmp/sedscript
echo -e "s/client_pw/${client_name}01/" >>/tmp/sedscript
echo -e "s/mysql_host/${mysql_host}/" >>/tmp/sedscript
sed -f /tmp/sedscript settings.php >/tmp/settings.php
mv /tmp/settings.php settings.php
echo -e "s/client_name/${client_name}/g" >/tmp/sedscript
echo -e "s/client_pw/${client_name}01/" >>/tmp/sedscript
echo -e "s/client_mail_password/${client_mail_password}/" >>/tmp/sedscript
echo -e "s/mysql_host/${mysql_host}/" >>/tmp/sedscript
sed -f /tmp/sedscript civicrm.settings.php >/tmp/civicrm.settings.php
mv /tmp/civicrm.settings.php civicrm.settings.php
chmod 755 settings.php
chmod 755 civicrm.settings.php
#check on group setting
chown www-data settings.php
chown www-data civicrm.settings.php
#edit the .htaccess file
cd /home/${client_name}/public_html
echo -e "s/htaccess_client_name/${client_name}/" >/tmp/sedscript
sed -f /tmp/sedscript .htaccess >/tmp/.htaccess
rm -f /tmp/sedscript
mv /tmp/.htaccess .htaccess
chmod 755 .htaccess
chown www-data .htaccess
#setup backup cronjob
#cp civibackup /etc/cron.daily/${client_name}_civibackup
echo -e "adding${client_name} to the daily cron backup list"
egrep -q ^${client_name} ${cron_daily_path}/client_backup_list
if [ $? -eq 0 ]
then
echo -e "${client_name} already exists"
exit
else
echo -e "${client_name}">>${cron_daily_path}/client_backup_list
mkdir /backup/civibackup/${client_name} 2>/dev/null
fi
echo -e "install complete"
echo -e "point your browser to:"
echo -e "justice.dharmatech.org/~${client_name}"
echo -e "login as user \"civiadmin\" PW \"civiadmin01\""
--------------- perl scrip to create encrypted passwords ---------------
#!/usr/bin/perl
#this script will generate a crypt(3) string from a supplied password
#we are using T7 for the salt value just for the hell of it instead
#of using a random 2 digit alpha-numeric value
$string = $ARGV[0];
$salt = "T7";
my $encrypted_password = crypt($string,$salt);
print "$encrypted_password";