Developers Documentation

×

Warning

301 error for file:https://clearos.com/dokuwiki2/lib/exe/css.php?t=dokuwiki&tseed=82873f9c9a1f5784b951644363f20ef8

User Tools

Site Tools


Setting up Backups for Zarafa

This guide covers a strategy for backing up your Zarafa email server. There are to items that are critical to backup.

  • Zarafa Database
  • Zarafa attachments and objects

This guide is a modification of the original Zarafa wiki and includes changes which perform the following:

  • Takes into account ClearOS specific configurations (Zarafa database with system-mysql, unique paths for mysql tools)
  • Scavenges the server.cfg for the current zarafa admin password.

Backing up the system-mysql Zarafa database

First, create a location to store the backups. In this scenario, I'm using ClearCenter's remote backup to already backup my flexshares. So I create a flexshare called zarafa-backup but I don't assign any protocols to it (ie. no-file, web, or ftp access). I give rights to the 'it' group for permissions. Next, I create this file called /etc/zarafabackup.sh which contains the following:

#!/bin/bash
#

# Modify the variables below to your need

# Mysql Credentials
MyUSER="zarafa"
MyPASS=`grep "mysql_password = " /etc/zarafa/server.cfg | sed 's/^mysql_password = //'`
MyHOST="localhost"
MYSQL="/usr/clearos/sandbox/usr/bin/mysql"

# Owner of mysql backup dir
OWNER="flexshares"
# Group of mysql backup dir
GROUP="it"
# Backup Dest directory, change this if you have someother location
DEST="/var/flexshare/shares/zarafa-backups/dbbackup"

# Backup all existing databases
DBS="$($MYSQL -u $MyUSER -h $MyHOST -p$MyPASS -Bse 'show databases')"
# Or specify which databases to backup
#DBS="mysql zarafa"

# DO NOT BACKUP these databases
IGGY="test"

# mysqldump parameters
#DUMP_OPTS="-Q --skip-lock-tables --single-transaction"
DUMP_OPTS="-Q --skip-lock-tables --single-transaction"

# Send Result EMail
SEND_EMAIL=1
NOTIFY_EMAIL="me@example.com"
NOTIFY_SUBJECT="Zarafa Backup Notification"

# Delete old backups
DELETE_OLD_BACKUPS=1
DELETE_BACKUPS_OLDER_THAN_DAYS=5

# Usually there is no need to modify the variables below

# Linux bin paths, change this if it can't be autodetected via which command
MYSQLDUMP="/usr/clearos/sandbox/usr/bin/mysqldump"
GREP="$(which grep)"
CHOWN="$(which chown)"
CHMOD="$(which chmod)"
GZIP="$(which gzip)"
MAIL="$(which mail)"
FIND="$(which find)"
DF="$(which df)"

# Get hostname
HOST="$(hostname)"

# Get data in yyyy-mm-dd format
NOW="$(date +"%Y%m%d")"

# Function for generating Email
function gen_email {
  DO_SEND=$1
  TMP_FILE=$2
  NEW_LINE=$3
  LINE=$4
  if [ $DO_SEND -eq 1 ]; then
    if [ $NEW_LINE -eq 1 ]; then
      echo "$LINE" >> $TMP_FILE
    else
      echo -n "$LINE" >> $TMP_FILE
    fi
  fi
}

# Main directory where backup will be stored
if [ ! -d $DEST ]; then 
  mkdir -p $DEST
  # Only $OWNER.$GROUP can access it!
  $CHOWN $OWNER:$GROUP -R $DEST
  $CHMOD 0750 $DEST
fi

# Create backup directory
MBD="$DEST/$NOW"
if [ ! -d "$MBD" ]; then
  mkdir "$MBD"
  # Only $OWNER.$GROUP can access it!
  $CHOWN $OWNER:$GROUP -R $MBD
  $CHMOD 0750 $MBD
fi

# Temp Message file
TMP_MSG_FILE="/tmp/$RANDOM.msg"
if [ $SEND_EMAIL -eq 1 -a -f "$TMP_MSG_FILE" ]; then
  rm -f "$TMP_MSG_FILE"
fi

set -o pipefail

# Start backing up databases
STARTTIME=$(date +%s)
for db in $DBS
do
    skipdb=-1
    if [ "$IGGY" != "" ];
    then
	for i in $IGGY
	do
	    [ "$db" == "$i" ] && skipdb=1 || :
	done
    fi
    
    if [ "$skipdb" == "-1" ] ; then
	FILE="$MBD/$db.$HOST.$NOW"
	# do all inone job in pipe,
	# connect to mysql using mysqldump for select mysql database
	# and pipe it out to gz file in backup dir :)
        $MYSQLDUMP $DUMP_OPTS -u $MyUSER -h $MyHOST -p$MyPASS $db | $GZIP -9 > "$FILE.gz"
        ERR=$?
        if [ $ERR != 0 ]; then
	  NOTIFY_MESSAGE="Error: $ERR, while backing up database: $db"	
	else
	  NOTIFY_MESSAGE="Successfully backed up database: $db"
	fi	
        gen_email $SEND_EMAIL $TMP_MSG_FILE 1 "$NOTIFY_MESSAGE"
        echo $NOTIFY_MESSAGE
    fi
done
ENDTIME=$(date +%s)
DIFFTIME=$(( $ENDTIME - $STARTTIME ))
DUMPTIME="$(($DIFFTIME / 60)) minutes and $(($DIFFTIME % 60)) seconds."

# Empty line in email and stdout
gen_email $SEND_EMAIL $TMP_MSG_FILE 1 ""
echo ""

# Log Time
gen_email $SEND_EMAIL $TMP_MSG_FILE 1 "mysqldump took: ${DUMPTIME}"
echo "mysqldump took: ${DUMPTIME}"

# Empty line in email and stdout
gen_email $SEND_EMAIL $TMP_MSG_FILE 1 ""
echo ""

# Delete old backups
if [ $DELETE_OLD_BACKUPS -eq 1 ]; then
  find "$DEST" -maxdepth 1 -mtime +$DELETE_BACKUPS_OLDER_THAN_DAYS -type d | $GREP -v "^$DEST$" | while read DIR; do
    gen_email $SEND_EMAIL $TMP_MSG_FILE 0 "Deleting: $DIR: "
    echo -n "Deleting: $DIR: "
    rm -rf "$DIR" 
    ERR=$?
    if [ $ERR != 0 ]; then
      NOTIFY_MESSAGE="ERROR"
    else
      NOTIFY_MESSAGE="OK"
    fi
    gen_email $SEND_EMAIL $TMP_MSG_FILE 1 "$NOTIFY_MESSAGE"
    echo "$NOTIFY_MESSAGE"
  done
fi

# Empty line in email and stdout
gen_email $SEND_EMAIL $TMP_MSG_FILE 1 ""
echo ""

# Add disk space stats of backup filesystem
if [ $SEND_EMAIL -eq 1 ]; then
  $DF -h "$DEST" >> "$TMP_MSG_FILE"  
fi
$DF -h "$DEST"

# Sending notification email
if [ $SEND_EMAIL -eq 1 ]; then
  $MAIL -s "$NOTIFY_SUBJECT" "$NOTIFY_EMAIL" < "$TMP_MSG_FILE"
  rm -f "$TMP_MSG_FILE"
fi

In this file I will only need to change the following lines:

  • OWNER=“flexshares”
  • GROUP=“it”
  • DEST=“/var/flexshare/shares/zarafa-backups/dbbackup”
  • NOTIFY_EMAIL=“me@example.com”

If the path for your destination does not exist, create it now:

mkdir /var/flexshare/shares/zarafa-backups/dbbackup

Scheduling your backup

Make sure your backup script is executable:

chmod 700 /root/zarafabackup.sh

Then add the daily backup script to your crontab using crontab -e.

crontab -e

Add this line to the end of the crontab (Shift+g o):

0	21	*	*	*	/root/zarafabackup.sh

Save the file. ( :wq ). This will execute your backup at 9PM each day (21:00).

Zarafa attachments and objects

This part of the backup is simple because you need only grab all of the data in a particular directory, namely /var/lib/zarafa.

If you are using ClearCenter's remote backup utility you can either set this directory as a custom backup source or bindmount the directory into another location that you are currently backing up. For example, if you are currently backing up your flexshares you can use the method above and simply make a directory to bind mount the data over.

mkdir /var/flexshare/shares/zarafa-backup/attachments

Then modify your /etc/fstab:

/var/lib/zarafa /var/flexshare/shares/zarafa-backups/attachments none bind,rw 0 0

Then mount the directory.

mount /var/flexshare/shares/zarafa-backups/attachments && ls /var/flexshare/shares/zarafa-backups/attachments
content/en_us/kb_o_setting_up_backups_for_zarafa.txt · Last modified: 2014/12/22 14:27 (external edit)

https://clearos.com/dokuwiki2/lib/exe/indexer.php?id=content%3Aen_us%3Akb_o_setting_up_backups_for_zarafa&1710838449