Taking a snapshot

I think it’s the case for many people that stuff you learn at work becomes useful at home too. Surprisingly enough, that’s turned out to be true for me as an Alfresco admin. A couple of months ago, my wife started her own business, and needed an online space to share files and collaborate with colleagues, consultants, and contractors. It of course immediately occurred to me that Alfresco CE would be the perfect solution for this, so I spent a weekend getting that set up for her.

At work, we have an pretty sophisticated infrastructure set up to easily handle live backups of our Alfresco server, but doing something equivalent for my wife’s little company was complete overkill.

So, I needed a way to back up her Alfresco instance (a pretty vanilla install of 3.4CE running on CentOS) to an FTP server. The way we do that is to shut down the Alfresco server at 2:30 each morning, back everything up to the FTP server, then start Alfresco back up again. Not surprisingly, I don’t want to be awake every morning at 2:30, so I wrote the shell script below to be executed by cron at the right time.

First, a few likely obvious, but important, notes:

  • Make sure all of the paths to all of the utilities and various bits of Alfresco are set properly in your copy of the script.
  • Please read all of the comments in the script–there are some more important notes there.
  • Please note that this script is provided without warranty of any kind from me, my employer, or anyone else–use at your own risk! If you lose data due to this script, I’ll be sympathetic, but I can’t be responsible. It’s probably a very good idea to fully understand this script before using it.
  • As with any backup scheme, it’s a very good idea to periodically do a test to ensure that you can recover your system to full working order from your backups.
### MySQL Server Login Info ###
# mysql server
### FTP SERVER Login info ###
#can also use ftp:// if you have to (not advisable for security reasons)

#$BAK is where the backup files are created/stored until they're tarred and
#gzipped for transfer over to the FTP server.  Note that this script will not
#automatically create this directory.
NOW=$(date +"%Y-%m-%d")

echo "Starting backup at $(date)"

#shut down Alfresco's tomcat, but not mysql
/bin/sh /opt/alfresco/alfresco.sh stop tomcat

echo "Sleep for 10 minutes, to be sure tomcat has fully stopped"
/bin/sleep 600

#kill off soffice process (will be restarted during alfresco startup process)
killall -v /opt/alfresco/openoffice/program/soffice.bin

echo "Alfresco Status is now:"
alfrescoStatus=`/bin/sh /opt/alfresco/alfresco.sh status`
echo $alfrescoStatus

#make backup directory, just in case
[ ! -d $BAK ] && mkdir -p $BAK || /bin/rm -f $BAK/*
#back up the databases
DBS="$($MYSQL -u $MUSER -h $MHOST -P $MPORT -p$MPASS  --socket=/opt/alfresco/mysql/tmp/mysql.sock -Bse 'show databases')"

echo "Databases found are $DBS"

for db in $DBS
 FILE=$BAK/$db.$NOW-$(date +"%T").sql
 $MYSQLDUMP -u $MUSER -h $MHOST -P $MPORT -p$MPASS --socket=/opt/alfresco/mysql/tmp/mysql.sock $db > $FILE

echo "Database backup files are  $ALL_SQL_FILES"

#make a tar file
#Note that the /opt/alfresco/alfresco_maintenance directory is where this
#script lives on my system, so I'm making sure that the script gets backed up
#properly each night.  You'll also notice that I'm backing up the website and
#website configs while I'm at it--that's of course optional.
TAR_FILE=$BAK/alfresco_backup-$NOW-$(date +"%T").tar
echo "Creating tar file $TAR_FILE"
tar -cf $TAR_FILE /var/www/html /var/www/error /etc/httpd /opt/alfresco/alfresco_maintenance /opt/alfresco/alf_data /opt/alfresco/tomcat/shared/classes /opt/alfresco/tomcat/conf /opt/alfresco/tomcat/scripts  $ALL_SQL_FILES

#gzip tar file for transmission
echo "Compressing $TAR_FILE with $GZIP to create $GZIP_FILE"

echo "Backup file is:" 
ls -lh $GZIP_FILE

#remove the old backup files
for i in {3..20}
   OLD=$(/bin/date -d "$i days ago" +"%Y-%m-%d")
   #echo "$i days ago was: $OLD"
   REMOVE_COMMAND="mrm alfresco_backup-$OLD*.tar.gz; $REMOVE_COMMAND"

#echo "Remove command is: $REMOVE_COMMAND"

#send the file to be backed up.  Replace the 'cd /' command at the start with the
#directory where the backups should be stored on the FTP server.  Make sure
#that directory exists--this script won't create it!
echo "Connecting to $FTPS: Removing older backups and sending $GZIP_FILE"
#echo "/usr/bin/lftp command is: lftp -u $FTPU,$FTPP -e \"cd /; $REMOVE_COMMAND; put $GZIP_FILE; quit\" $FTPS"
/usr/bin/lftp -u $FTPU,$FTPP -e "cd /; $REMOVE_COMMAND; put $GZIP_FILE; quit" $FTPS

#start up Alfresco's tomcat again
/bin/sleep 60
/bin/sh /opt/alfresco/alfresco.sh start tomcat

echo "Backup complete at $(date)"

/bin/sleep 60
echo "Alfresco Status is now:"
alfrescoStatus=`/bin/sh /opt/alfresco/alfresco.sh status`
echo $alfrescoStatus
This entry was posted in Scripts. Bookmark the permalink.

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 )

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s