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/
Advertisements


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s