Chiunque abbia mai fatto il backup di un server di posta sa che l'imperativo è "Ridurre il downtime!".
Ho recentemente modificato lo script di backup che utilizzavo per il server Zimbra (preso da quelli disponibili qui) in modo da utilizzare gli snapshot di LVM e rsnapshot. Adesso il downtime è limitato al tempo di stop/start di Zimbra e al tempo di snapshot di LVM (circa 2 minuti in totale, di cui pochi istanti di snapshot) e posso con calma effettuare il backup utilizzando rsnapshot che permette di effettuare backup incrementali con uno "spreco" di spazio veramente minimo.

La parte riguardante rsnapshot è molto semplice: si deve effettuare il backup della cartella in cui viene montato lo snapshot, ovvero /mnt/tmp_backup/ZimbraBackup/. Il resto della configurazione è standard.

Ecco invece lo script da inserire in /etc/cron.daily/:

#!/bin/bash

# Where the temporary snapshot will be mounted
TEMPDIR="/mnt/tmp_backup"

# The size of the snapshot volume
VOLSIZE="10G"  
# Dynamic volsize, change the mountpoint to fit your conf
#VOLSIZE=`df -h | grep /opt | awk '{print $4}'`

# what to backup, in my case: /dev/vg0/opt
VG="vg0"  
LV="opt"

# snapshot name
LV_SNAP="ZimbraBackup"

# lvcreate and lvremove commands path
lvcreate_cmd="/sbin/lvcreate"  
lvremove_cmd="/sbin/lvremove"

# Choose the backup
#
# monthly => first day of month
# weekly => on sunday
# daily => daily :)

BACKUP_TYPE="daily"

if [ `date +%d` == "01" ]; then  
 BACKUP_TYPE="monthly"
elif [ `date +%u` == "7" ]; then  
 BACKUP_TYPE="weekly"
fi

echo -e "Backup started at `date`"

# Stop the Zimbra services
before="$(date +%s)"  
echo -e "Stopping the Zimbra services..."  
/etc/init.d/zimbra stop || exit
echo -e "Creating a snapshot called $LV_SNAP"

# depending on the time it takes to create the backup and the traffic of your server, you may need to increase the 2G value
$lvcreate_cmd -L$VOLSIZE -s -n $LV_SNAP /dev/$VG/$LV

# Create a mountpoint to mount the logical volume to
echo -e "Creating a mountpoint for the LV..."  
mkdir -p $TEMPDIR/$LV_SNAP

# Mount the logical volume to the mountpoint
echo -e "Mounting the snapshot..."

# WARNING: if you use xfs you MUST add nouuid option here!
mount -o ro,nouuid /dev/$VG/$LV_SNAP $TEMPDIR/$LV_SNAP/

# Start the Zimbra services
echo -e "Restarting the Zimbra services..."  
/etc/init.d/zimbra start || echo -e "ERROR restarting zimbra"

# Calculates and outputs amount of time the server was down for
after="$(date +%s)"  
elapsed="$(expr $after - $before)"  
hours=$(($elapsed / 3600))  
elapsed=$(($elapsed - $hours * 3600))  
minutes=$(($elapsed / 60))  
seconds=$(($elapsed - $minutes * 60))  
echo -e "Server was down for: $hours hours $minutes minutes $seconds seconds"

# Launch rsnapshot to backup
echo -e "Executing $BACKUP_TYPE backup..."  
echo -e "rsnapshot $BACKUP_TYPE"  
rsnapshot $BACKUP_TYPE

# Unmount /tmp/$lv_zimbra and remove the logical volume
echo -e "Unmounting and removing the snapshot."  
umount /dev/$VG/$LV_SNAP  
$lvremove_cmd --force /dev/$VG/$LV_SNAP
echo -e "$lvremove_cmd /dev/$VG/$LV_SNAP"

# Done!
echo -e "Backup ended at `date`"

blog comments powered by Disqus