Backup SugarCRM to Amazon S3

Our Sugar environment is in Amazon so why not back the thing up in Amazon. We also have decided to sync the cache/uploads folder so that we can scale sideways, that’s the next thing to do ūüôā

You will need s3cmd installed to use this, handily s3cmd installation instructions

As per usual this will change as little errors get taken care of so please note that this is the 1st effort at the script so be a bit careful

#!/bin/bash
 #
 # Backup Script
 #
#Constants
 #PATH=/usr/local/bin:/usr/bin:/bin:
 #DATE=`date +%Y-%m-%d_%Hh%Mm`
 DATE=`date +%Y-%m-%d`
 S3LOGDIR="S3backup"
 LOGDIR="/var/log"
# Directory for logs
 # Use the standard log file directories
 # Create required directories
 if [ -d "$LOGDIR/$S3LOGDIR" ]; # Check log Directory exists.
 then
 echo "directory exists"
 else
 echo "the directory does not exist"
 mkdir -p "$LOGDIR/$S3LOGDIR"
 fi;
 #Clean up the error file before the script starts
 rm -f $LOGDIR/$S3LOGDIR/s3errfile
#----------------------------------------------------------------------
 # Change the directories below
# Directory to backup
 BACKUPSRCDIR="/var/www/html/crm"
#Main S3 bucket
 S3Bucket="s3://xxxxxxxxx-backup"
 S3LiveBucket="CRM_Live"
#Directories that will be synced using s3
 S3SyncBucket="CRM_Live_Uploads"
 LocalSyncDir="$BACKUPSRCDIR/cache/upload"
 #File Names
 BACKUPFILE="CRM-"$DATE".tar"
 #End the variables that need entering
 #----------------------------------------------------------------------
#Backup the main directories, leave the cache upload directory
 #echo $BACKUPFILE
#Check that the S3 connection is available
 s3cmd ls $S3Bucket/ 2>&1>$LOGDIR/$S3LOGDIR/s3errfile
 if [[ -s $LOGDIR/$S3LOGDIR/s3errfile ]] ; then
 echo "connection OK"
 else
 echo "S3 bucket connection failed"
 exit 1
 fi ;
#tar the dirctory excluding the cache/upload folder as this will be
#use the s3cmd rsync command as this is where the uploaded document
#are stored
tar -cvf $BACKUPFILE --exclude "cache/uploads*" --exclude "cache/uploads/*" $BACKUPSRCDIR
#Put the tar file into S3 for safe keeping
s3cmd put $BACKUPFILE $S3Bucket/$S3LiveBucket/
#Clear the backup file
rm -f $BACKUPFILE
#Sync the big folders to S3
s3cmd sync $LocalSyncDir $S3Bucket/$S3SyncBucket/

Duplicate Sugar CRM instance for Development

If you have email archiving enabled, you need to disable it before attempting this copy, also disable any special scheduled jobs that you may have.

This copy should be done out of hours to ensure that there is no impact, and if you could shut the Apache server down this would be even better.

The steps that we will follow are:

  • Shut the web server down
  • Copy the Sugar live folder outside the web server path
  • Change the config files
  • Clone the MySQL database
  • Copy the test Sugar folder back into the web servers path
The assumption here is that the folder that SugarCrm is installed in is called crm and the test / devlopment environment will be crmdev. The database is called sugarcrm_pro and sugarcrm_prodev respectively.
sudo su
cd /
mkdir sugarcrm_temp

If you can stop the Apache server

service httpd stop

Copy the crm installation folder in /var/www/html to a new folder name

cp -R /var/www/html/crm /sugarcrm_temp/crmdev

You then need to edit the config.php file and ammend the DB variable to match the new database that will be created in the next steps.

cd /sugarcrm_temp/crmdev
vi config.php

Look for the lines around dbconfig and change the db_name to match the database below.

We now need to create the database in Mysql.

mysql -u username -p -h servername 
create database sugarcrm_prodev;

To ensure that the database has been created

show databases;
exit

if you already have cloned the database you will need to drop the database first.

drop database sugarcrm_prodev;
create database sugarcrm_prodev;

To ensure that the database has been created

show databases;
exit

We then just need to duplicate the database, this is a built in function and can be achieved with mysqldump

The basic syntax is

NOTE that this should all be on a single line, there should be no carriage return or line feed when pasting it into the command line (the wordpress editor also messes around with the — infront or password it should read – – so when copy and pasting make sure there is a single space in between the dashes)

mysqldump -u {source_username} --password={source_password} -h {source_server} {source_db_name} |
 mysql -u {dest_username} --password={dest_password} -h {dest_server} {dest_db_name}

unfortunately I can not give you an example with letting you know my username and passwords but the syntax is pretty easy just ensure that the {dest_db_name} matches the database name from above.

To ensure that you have copied the database correctly we can run a few tests.

mysql -u username -p -h servername 
use sugarcrm_pro;
select count(id) from contacts;

Take a note of the number

use sugarcrm_pro;
select count(id) from contacts;
exit

These should match perhaps the copy will be 1 or 2 less depending on how busy your database, if you shut you web server down these should match exactly.

Once the database has been cloned

cd /sugarcrm_temp
mv crmdev /var/www/html/
cd /var/www/html/
chown -R apache:apache crmdev

You will need to change the apache:apache to match the user and group that runs the web server in your environment.

Remember to restart the schedules and the email archiving  in he live environment.

NOTE the address changes so you will need to re-issue the email address, I created a vcard and then just sent it out to all the user so that they could update the information.

Example of vcard file

BEGIN:VCARD
VERSION:3.0
FN:CRM
N:;CRM;;;
EMAIL;TYPE=INTERNET:xxxxxxxx@sugarondemand.com
END:VCARD

Save this as a vcf file and you are good to go


Change Sugar CRM URL

I have just installed Sugar CRM in the Amazon cloud. More about the Amazon cloud will be coming in later in Amazon section.

The main issue that I had was that the URL is that of the server and I needed it to be the URL of the ELB (Elastic Load Balancer) in other words the URL was set to the hostname of the server.

To change this is relatively simple.

Navigate to

/var/www/html/XXXX

where XXXX is the directory where your Sugar installation is.

Edit the config.php file with your favourite editor and locate the hostname variable. Change this to what ever URL that you want, typically the CNAME entry from your DNS records.

A word of warning make sure you DNS is updated and proper-gated before change this setting.

Restart the HTTPD service and you should have the correct URL in place.

The steps

cd /var/www/html/XXXX

cp config.php config.php_DDMMYY

(sudo) vi config.php

Change the Host_name variable

Change the site_url variable

Then either

service httpd restart

or

./etc/init.d/httpd restart