Sei sulla pagina 1di 2

#!

/bin/bash
###
# $Id: zimbra_user_backup.sh 2 2008-04-11 12:04:32Z wimpr1m $
# Requirements : bash, whoami, su, echo, tee, cut, grep, mkdir, chown, rsync, du
, zimbra 5.0.x
#
###
# Set the as required
zimbra_cache="/opt/zimbra_cache"
zimbra_backup="/opt/zimbra_backup"
# Logs the parsed message to console and a logfile.
# $1 [string] : messge to log
function log {
echo `date` : ${1} | tee -a ${zimbra_log_directory}/zimbra_backup.log
}
# Executes the passed command as the 'zimbra' user.
# $1 [string] : command to execute.
function zimbra_run {
su - ${zimbra_user} -p -c "${1}"
}
# chown the passed file/directory as the zimbra user and group.
# $1 [string] : file/directory path
function zimbra_chown {
if [ ! -e ${1} ]; then
log "WARNING! I can't chown ${1} because it does not exist."
else
chown ${zimbra_user}:${zimbra_user} ${1}
fi
}
# rsync the passed file/directory path to the rsync cache.
# $1 [string] : file/directory path
function zimbra_rsync {
if [ ! -e ${1} ]; then
log "WARNING! I can't rsync ${1} because it does not exist."
else
local DIRS=`ls -1 ${1}`
for DIR in ${DIRS}
do
# Skip the incoming directory
if [ ${DIR} != "incoming" ]; then
log "Syncing ${1}/${DIR}/${mailbox_id} to ${zimb
ra_cache}/${current_account}${1}/${DIR}"
mkdir -p ${zimbra_cache}/${current_account}${1}/
${DIR}/${mailbox_id}
rsync -aHK --delete ${1}/${DIR}/${mailbox_id} ${
zimbra_cache}/${current_account}${1}/${DIR}
log "Size : `du -s -h ${zimbra_cache}/${current_
account}${1}/${DIR} | cut -f1`"
fi
done
fi
}
# Sets the current account to the passed account status
# $1 [string] : account status
function zimbra_set_account_status {
zimbra_run "${zimbra_home}/bin/zmprov ma ${current_account} zimbraAccoun
tStatus ${1}"
local current_account_status=`zimbra_run "${zimbra_home}/bin/zmprov ga $
{current_account} | grep zimbraAccountStatus:" | cut -d' ' -f2`
log "${current_account} is now in ${current_account_status} mode."
if [ "${1}" != "${current_account_status}" ]; then
log "ERROR! ${current_account} status did not change status corr
ectly. Status should be ${1} but is actually ${current_account_status}"
fi
}
# Create query.sql from the passed parameter
# $1 [string] : MySQL query
function zimbra_create_sql {
echo ${1} > ${zimbra_tmp_directory}/query.sql
}
# Executes query.sql against the passed database and stores output in
# $zimbra_sql_result less the header. Needs tweaking for wider use.
# $1 [string] : database
function zimbra_execute_sql {
zimbra_sql_result=""
zimbra_sql_result=`su - ${zimbra_user} -p -c "${mysql_directory}/bin/mys
ql -S ${mysql_socket} -u ${zimbra_mysql_user} --password=${zimbra_mysql_password
} < ${zimbra_tmp_directory}/query.sql | tail -n1"`
rm ${zimbra_tmp_directory}/query.sql
}
# Executes mysqldump and the zimbra MySQL user using the passed parameters
# $1 [string] : mysqldump paramters
function zimbra_dump_sql {
log "Dumping ${1}"
zimbra_run "${mysql_directory}/bin/mysqldump -f -S ${mysql_socket} -u ${
zimbra_mysql_user} --password=${zimbra_mysql_password} ${1}"
}
if [ ! -d "/opt/zimbra" ]; then
echo "ERROR! I couldn't find '/opt/zimbra'"
exit 1
else
# Source the Zimbra environment variables.
# - http://wiki.zimbra.com/index.php?title=Zmlocalconfig
source /opt/zimbra/bin/zmshutil || exit 1
zmsetvars
# Make sure the backup and cache directories exists.
mkdir -p ${zimbra_backup}
mkdir -p ${zimbra_cache}
zimbra_chown ${zimbra_backup}
fi

Potrebbero piacerti anche