Sie sind auf Seite 1von 4

Backup Using LVM

#!/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..."
mkdir -p /tmp/ZimbraBackup

# Mount the logical volume to the mountpoint


say $GREEN"Mounting the LV..."
mount -t $zimbra_vol_fs -o nouuid,ro /dev/$vol_group/ZimbraBackup
/tmp/ZimbraBackup/

# Start the Zimbra services


say $CYAN"Starting the Zimbra services..."
/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"
Backup Simple con FTP

#!/bin/bash

# Zimbra Backup Script


# Requires ncftp to run
# 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

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


echo Time backup started = $(date +%T)
before="$(date +%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 /opt/zimbra/ /backup/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.

# 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


su - zimbra -c"/opt/zimbra/bin/zmcontrol stop"
sleep 15

# Kill any orphaned Zimbra processes


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

# 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 /opt/zimbra/ /backup/zimbra

# Restart Zimbra Services


su - zimbra -c "/opt/zimbra/bin/zmcontrol 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 > /backup/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"

# Create archive of backed-up directory for offsite transfer


# cd /backup/zimbra
tar -zcvf /tmp/mail.backup.tgz -C /backup/zimbra .

# Transfer file to backup server


ncftpput -u <username> -p <password> <ftpserver> /<desired dest. directory>
/tmp/mail.backup.tgz

# 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"

One further note: I have observed some odd behavior in this and other scripts that, when run from
the command line work flawlessly, but when run from crontab the script may get ahead of itself
and, for example, try to ftp the file before tar is done creating it; resulting in a useless backup.
Loading the script into crontab with the parameters to create a log file, for example
. /etc/zimbra.backup > /temp/zbackup.log 2>&1

seems to solve this problem (while creating the log, or showing the output on the screen, the script
seems to follow the sequence more carefully), while giving you a line-by-line record of the backup
procedure. In my installation with just over 3GB backed up, the logfile is 2.5 mb and is overwritten
each night.
NB You may find that using su on your operating system has problems and some services don't start
or stop correctly. If that's the case use 'sudo -u zimbra' in the following format for the commands:
sudo -u zimbra zmcontrol start

Das könnte Ihnen auch gefallen