Zimbra开源版备份脚本

 

以下是Zimbra开源版备份脚本,如果想要更好的自动备份,可以使用 Zimbra网络版。

1.使用LVM备份Zimbra

如果您将Zimbra安装在了你自己的本地机器上,可以使用以下脚本:

 

#!/bin/bash

  

time=`date +%Y-%m-%d_%H-%M-%S`

 

# Modify the following variables according to your installation

#########################################

 

# backup_dir - directory to backup to

backup_dir=/path/to/backups/$time

 

# vol_group - the Volume Group that contains $zimbra_vol

vol_group=PUT_VOL_GROUPNAME_HERE

 

# zimbra_vol - the Logical Volume that contains /opt/zimbra

zimbra_vol=PUT_ZIMBRA_VOLNAME_HERE

 

# zimbra_vol_fs - the file system type (ext3, xfs, ...) in /opt/zimbra

zimbra_vol_fs=PUT_ZIMBRA_FILE_SYSTEM_TYPE_HERE

 

# lvcreate and lvremove commands path - 

lvcreate_cmd=`which lvcreate`

lvremove_cmd=`which lvremove`

  

# Do not change anything beyond this point

#########################################

 

# Test for an interactive shell

if [[ $- != *i* ]]

   then say() { echo -e $1; }

     # Colors, yo!

     GREEN="\e[1;32m"

     RED="\e[1;31m"

     CYAN="\e[1;36m"

     PURPLE="\e[1;35m"

   else say() { true; } # Do nothing

fi

 

# Output date

say $GREEN"Backup started at "$RED"`date`"$GREEN"."

 

# Stop the Zimbra services

say $CYAN"Stopping the Zimbra services..."

say $PURPLE"  This may take several minutes."

/etc/init.d/zimbra stop

 

# Create a logical volume called ZimbraBackup

say $GREEN"Creating a LV called ZimbraBackup:"$PURPLE

$lvcreate_cmd -L1000M -s -n ZimbraBackup /dev/$vol_group/$zimbra_vol

 

# Create a mountpoint to mount the logical volume to

say $GREEN"Creating a mountpoint for the LV..."

# WARNING: this is insecure!

mkdir -p /tmp/ZimbraBackup

 

# Mount the logical volume to the mountpoint

say $GREEN"Mounting the LV..."

# WARNING: remove nouuid option if the filesystem is not formatted as XFS !!!   

mount -t $zimbra_vol_fs -o nouuid,ro /dev/$vol_group/ZimbraBackup /tmp/ZimbraBackup/

 

# Start the Zimbra services

say $CYAN"Starting the Zimbra services..."

# WARNING: it's safer not to put this command in background

/etc/init.d/zimbra start &

 

# For testing only

#say $RED"Press Enter to continue...\e[0m"

#read input

 

# Create the current backup

say $GREEN"Creating the backup directory and backup..."

mkdir -p $backup_dir

tar zcvf $backup_dir/zimbra.backup.tar.gz /tmp/ZimbraBackup/zimbra/ 2&> /dev/null

 

# Unmount /tmp/ZimbraBackup and remove the logical volume

say $GREEN"Unmounting and removing the LV."$PURPLE

umount /tmp/ZimbraBackup/

$lvremove_cmd --force /dev/$vol_group/ZimbraBackup

 

# Done!

say $GREEN"Zimbra backed up to "$CYAN$backup_dir$GREEN"!"

say $GREEN"Backup ended at "$RED"`date`"$GREEN".\e[0m"

 

 

2、使用LVM and rsync详细参考:

https://github.com/sergevanginderachter/sysops/blob/master/zimbrackup/ba...

 

 

 

3、使用LVM和duplicity的另外一个脚本

 

前面的例子使用 tar 和 rsync。 两者都有缺点:

他们不支持增量备份

你需要做额外的脚本加密的备份和清除旧的备份

 

我修改了第一个脚本使用duplicity代替了普通的tar, 这种方式可以灵活使用你喜欢的任何协议(scp, rsync, ftp)上传备份,你不担心旧的备份,可以节省大量的磁盘空间!

 

获得脚本: http://www.nervous.it/2007/01/zimbra-lvm-backup-with-duplicity-volume/ 

获得duplicity: http://duplicity.nongnu.org/

 

4、Perl方式(不包括LVM )

ZCS的工具目前包含用Perl写的一个冷备份脚本。 它还支持备份旋转。 此脚本不使用LVM。

当前位置:zimbraColdBackup - Ver0.02beta

 

5、简单的shell脚本,通过ssh的rsync

 

#!/bin/bash

 

# Zimbra Backup Script

# Requires that you have ssh-keys: https://help.ubuntu.com/community/SSHHowto#Public%20key%20authentication

# This script is intended to run from the crontab as root

# Date outputs and su vs sudo corrections by other contributors, thanks, sorry I don't have names to attribute!

# Free to use and free of any warranty!  Daniel W. Martin, 5 Dec 2008

## Adapted for rsync over ssh instead of ncftp by Ace Suares, 24 April 2009 (Ubuntu 6.06 LTS)

 

# the destination directory for local backups

DESTLOCAL=/backup/backup-zimbra

# the destination for remote backups

DESTREMOTE="yourserver:/backup/backup-zimbra"

 

# Outputs the time the backup started, for log/tracking purposes

echo Time backup started = $(date +%T)

before="$(date +%s)"

# a backup dir on the local machine. This will fill up over time!

BACKUPDIR=$DESTLOCAL/$(date +%F-%H-%M-%S)

 

# Live sync before stopping Zimbra to minimize sync time with the services down

# Comment out the following line if you want to try single cold-sync only

rsync -avHK --delete --backup --backup-dir=$BACKUPDIR /opt/zimbra/ $DESTLOCAL/zimbra

 

# which is the same as: /opt/zimbra /backup 

# Including --delete option gets rid of files in the dest folder that don't exist at the src 

# this prevents logfile/extraneous bloat from building up overtime.

# the backupdir will hold all files that changed or where deleted during the previous backup

 

# Now we need to shut down Zimbra to rsync any files that were/are locked

# whilst backing up when the server was up and running.

before2="$(date +%s)"

 

# Stop Zimbra Services

/etc/init.d/zimbra stop

#su - zimbra -c"/opt/zimbra/bin/zmcontrol stop"

#sleep 15

# Kill any orphaned Zimbra processes

#kill -9 `ps -u zimbra -o "pid="`

pkill -9 -u zimbra

 

# Only enable the following command if you need all Zimbra user owned

# processes to be killed before syncing

# ps auxww | awk '{print $1" "$2}' | grep zimbra | kill -9 `awk '{print $2}'`

 

# Sync to backup directory

rsync -avHK --delete --backup --backup-dir=$BACKUPDIR /opt/zimbra/ $DESTLOCAL/zimbra

 

# Restart Zimbra Services

#su - zimbra -c "/opt/zimbra/bin/zmcontrol start"

/etc/init.d/zimbra start

 

# Calculates and outputs amount of time the server was down for

after="$(date +%s)"

elapsed="$(expr $after - $before2)"

hours=$(($elapsed / 3600))

elapsed=$(($elapsed - $hours * 3600))

minutes=$(($elapsed / 60))

seconds=$(($elapsed - $minutes * 60))

echo SERVER WAS DOWN FOR: "$hours hours $minutes minutes $seconds seconds"

 

# Create a txt file in the backup directory that'll contains the current Zimbra

# server version. Handy for knowing what version of Zimbra a backup can be restored to.

# su - zimbra -c "zmcontrol -v > $DESTLOCAL/zimbra/conf/zimbra_version.txt"

# or examine your /opt/zimbra/.install_history

 

# Display Zimbra services status

echo Displaying Zimbra services status...

su - zimbra -c "/opt/zimbra/bin/zmcontrol status"

# /etc/init.d/zimbra status # seems not to work

# backup the backup dir (but not the backups of the backups) to remote

rsync -essh -avHK --delete-during $DESTLOCAL/zimbra $DESTREMOTE

 

# Outputs the time the backup finished

echo Time backup finished = $(date +%T)

 

# Calculates and outputs total time taken

after="$(date +%s)"

elapsed="$(expr $after - $before)"

hours=$(($elapsed / 3600))

elapsed=$(($elapsed - $hours * 3600))

minutes=$(($elapsed / 60))

seconds=$(($elapsed - $minutes * 60))

echo Time taken: "$hours hours $minutes minutes $seconds seconds"

 

# end

 

6、压缩和加密方式备份Shell脚本

 

此脚本具有以下特点:

好用!

强大的加密备份!

压缩的归档

创建后的归档可选异地复制

归档的完整性检查MD5校验

每周备份周期率 - 1-6变化

完整备份的电子邮件报告

对错误的电子邮件通知

备份文件列表(每周完整备份报告附件)

快速部署安装和设置选项(安装所需的软件和设置ENV 如SSH PKI AUTH和cronjobs)

 

 

#!/bin/bash

## *** Info ***

# USAGE:    -h or --help for help & usage.

#           -f or --full for Full backup.

#           -d or --diff for Diff backup.

#           -V or --version for version info.

#           --INSTALL for script install and setup.

#

# This is a backup script for the FOSS version of Zimbra mail server.

# The script is free and open source and for use by anyone who can find a use for it.

#

# THIS SCRIPT IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS

# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT

# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR

# A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT

# HOLDERS AND/OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,

# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,

# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;

# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED

# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,

# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF

# THE USE OF THIS DOCUMENT, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#

# CONTRIBUTORS:

# heinzg of osoffice.de (original author)

# Quentin Hartman of Concentric Sky, qhartman@concentricsky.com (refactor and cleanup)

#

# What this script does:

# 1. Makes daily off-line backups, at a service downtime of ~ < 2 min.

# 2. Weekly backup cycle - 1 full backup & 6 diffs.

# 3. Predefined archive sizes, for writing backups to CD or DVD media...

# 4. Backup archive compression.

# 5. Backup archive encryption.

# 6. Backup archive integrity checks and md5 checksums creation.

# 7. Automated DR - Off-site copy of backup archives via ssh.

# 8. Install and setup function for needed software (Ubuntu Systems only)

# 9. Weekly eMail report & eMail on error - including CC address.

#

# This script makes use of following tools:

# apt-get, cron, dar, dpkg, mailx, md5sum, rsync, ssh, uuencode, wget, zimbra mta.

#

# We have opted to use a pre-sync directory to save on "down time", but this 

# causes one to have huge additional space usage.

# But hard drives are cheep today!

#

# What is still to come or needs work on:

# 1. Recovery option

# 2. Better documentation

 

##------- CONFIG -------#

# Edit this part of the script to fit your needs.

 

#--- Directories ---#

# Please add the trailing "/" to directories!

ZM_HOME=/opt/zimbra/ # where zimbra lives

SYNC_DIR=/tmp/fakebackup/ # intermediate dir for hot/cold syncs. must have at least as much free space as ZM_HOME consumes

ARCHIVEDIR=/Backup/zimbra_dars/ # where to store final backups

TO_MEDIA_DIR=/Backup/burn/ # where to put fulls for archiving to media

 

#--- PROGRAM OPTIONS ---# 

RSYNC_OPTS="-aHK --delete --exclude=*.pid" # leave these unless you are sure you need something else

 

#--- ARCHIVE NAMES ---#

BACKUPNAME="Zimbra_Backup" # what you want your backups called

FULL_PREFIX="FULL" # prefix used for full backups

DIFF_PREFIX="DIFF" # prefix used for differential backups

BACKUPDATE=`date +%d-%B-%Y` # date format used in archive names

BACKUPWEEK=`date +%W` # Week prefix used for backup weekly rotation and naming

 

#--- ARCHIVE SIZE ---#

ARCHIVESIZE="4395M" # storage media size, for full-backup archiving

COMPRESS="9" # valid answers are 1 - 9 ( 9 = best )

 

#--- Encryption Options ---#

CRYPT="yes" # valid answers are "yes" or "no"

PASSDIR=/etc/`basename $0`/ # the directory the encryption hash is stored in. 

PASSFILE="noread" # the file containing the password hash

 

#--- Log Settings ---#

EMAIL="your@mailaddress.local" # the address to send logs to

EMAILCC="" # another address to send to

LOG="/var/log/zim_backup.log" # log location

 

#--- SSH REMOTE DR COPY ---#

# This option will secure copy your archives to a remote server via 'scp'

DRCP="no" # valid answers are "yes" or "no" 

SSHUSER="you" # recommend creating a user on the remote machine just for transferring backups

SSHKEY="rsa" # recommended answers are "rsa" or "dsa" but "rsa1" is also valid.

REMOTEHOST="remote.server.fqdn" # can use IP too

REMOTEDIR="/tmp/" # where you want your backups saved.

 

#--- Use Hacks? ---#

# Built in hacks to fix common problems

#Hack to start Stats, even run zmlogprocess if needed

STATHACK="yes" # valid answers are "yes" or "no"

 

 

## ~~~~~!!!! SCRIPT RUNTIME !!!!!~~~~~ ##

# Best you don't change anything from here on, 

# ONLY EDIT IF YOU KNOW WHAT YOU ARE DOING