I. Tạo thư mục backup, ví dụ, với quyền root:
mkdir /backup
Thư mục /backup này sẽ chứa các folder backup được đặt tên theo ngày, đồng thời chứa các script và file userlist.
II. Tạo danh sách user cần backup
cd /backup
touch backup-userlist.txt

Liệt kê email các user cần sao lưu mailbox. Bạn cũng có thể lấy danh sách user bằng lệnh zmprov -l gaa > /backup/backup-userlist.txt.
user1@zimilab.com
user2@zimilab.com
user3@zimilab.com
III. Tạo script full backup
touch full-backup-mailbox.sh
chmod +x full-backup-mailbox.sh
Nội dung script như sau: Ta sẽ backup dữ liệu 365 ngày kể từ thời điểm chạy script. Bạn có thể thay đổi theo nhu cầu.
##!/bin/bash
clear

## Backup Format
FORMAT=tgz

## Backup location
ZBACKUP=/backup

## Folder name for backup and using date
DATE=`date +”%d%m%y”`

## Backup location separate by date
ZDUMPDIR=$ZBACKUP/full-$DATE

## zmmailbox location
ZMBOX=/opt/zimbra/bin/zmmailbox

### Backup Option ###

## Based on few day ago until today, example 365 days ago

HARI=`date –date=’365 days ago’ +”%m/%d/%Y”`
query=”&query=after:$HARI”

## Based on certain date , example 21 Jan 2015.

#query=”&query=date:01/21/2015″

## Based from/to certain date. Example Backup Mailbox before 21 Jan 2015 and after 10 Jan 2015

#query=”&query=after:01/10/2015 before:01/21/2015″

if [ ! -d $ZDUMPDIR ]; then
mkdir -p $ZDUMPDIR
fi

## Looping Account Zimbra
#for account in `su – zimbra -c ‘zmprov -l gaa | sort’`
for account in `cat $ZBACKUP/backup-userlist.txt`
do
echo “Processing mailbox $account full backup…”
$ZMBOX -z -m $account getRestURL “//?fmt=${FORMAT}$query” > $ZDUMPDIR/$account.${FORMAT}
done

echo “Zimbra Mailbox backup has been completed successfully.”

IV. Tạo script incremental backup (tùy chọn)
touch inc-backup-mailbox.sh
chmod +x inc-backup-mailbox.sh
Nội dung script như sau: Nếu cho chạy mỗi ngày thì sẽ backup dữ liệu 1 ngày kể từ thời điểm chạy script.
##!/bin/bash
clear

## Backup Format
FORMAT=tgz

## Backup location
ZBACKUP=/backup

## Folder name for backup and using date
DATE=`date +”%d%m%y”`

## Backup location separate by date
ZDUMPDIR=$ZBACKUP/inc-$DATE

## zmmailbox location
ZMBOX=/opt/zimbra/bin/zmmailbox

### Backup Option ###

## Based on few day ago until today, example 1 days ago

#HARI=`date –date=’1 days ago’ +”%m/%d/%Y”`
#query=”&query=after:$HARI”

## Based on certain date , example 21 Jan 2015.

#query=”&query=date:01/21/2015″

## Based from/to certain date. Example Backup Mailbox before 21 Jan 2015 and after 10 Jan 2015

#query=”&query=after:01/10/2015 before:01/21/2015″

if [ ! -d $ZDUMPDIR ]; then
mkdir -p $ZDUMPDIR
fi

## Looping Account Zimbra
#for account in `su – zimbra -c ‘zmprov -l gaa | sort’`
for account in `cat $ZBACKUP/backup-userlist.txt`
do
echo “Processing mailbox $account incremental backup…”
$ZMBOX -z -m $account getRestURL “//?fmt=${FORMAT}$query” > $ZDUMPDIR/$account.${FORMAT}
done

echo “Zimbra Mailbox backup has been completed successfully.”

V. Đưa vào crontab.
Chaỵ full backup mỗi 1:10am chủ nhật, chạy incremental backup mỗi 1:10am từ thứ 2 đến thứ 7. Xóa các thư mục backup cũ hơn 30 ngày hằng tuần
crontab -e
10 1 * * 0 /bin/sh /backup/full-backup-mailbox.sh > /dev/null 2>&1
10 1 * * 1-6 /bin/sh /backup/inc-backup-mailbox.sh > /dev/null 2>&1
1 1 * * * find /backup/* -type d -mtime +30 -exec rm -rf {} +

VI. Tạo script phục hồi
touch restore-userlist.txt
Nội dung:
user1@zimilab.com
user2@zimilab.com
Tạo script:
touch restore-mailbox.sh
chmod +x restore-mailbox.sh
Nội dung:
#!/bin/bash

##Usage: ./restore-mailbox.sh <backup folder name>
##Example: ./restore-mailbox.sh full-050522

ZBACKUP=/backup
BACKUPDIR=”$ZBACKUP/$1″;

clear

echo “Retrieve all zimbra user name…”

USERS=”$ZBACKUP/restore-userlist.txt”

for ACCOUNT in `cat $USERS`; do
NAME=`echo $ACCOUNT`
echo “Restoring $NAME mailbox…”
su – zimbra -c “zmmailbox -z -m $NAME postRestURL ‘//?fmt=tgz&resolve=skip’ $BACKUPDIR/$NAME.tgz”;
done
echo “All mailbox has been restored sucessfully”

Để chạy script này, bạn gọi script kèm tham số tên thư mục backup muốn restore.
./restore-mailbox.sh full-050522

Bonus: bạn nên backup ldap và mariadb để phòng trường hợp cần xây dựng lại server từ đầu.
###########################
Backup ldap và mysql. Với quyền root.
mkdir /backupzimbra
cd /backupzimbra

Tạo script như sau:
touch ldap-mysql-backup.sh
chmod +x ldap-mysql-backup.sh
Nội dung script:

#!/bin/bash

## Backup location
ZBACKUP=/backupzimbra
ZBACKUPLDAP=${ZBACKUP}/ldap
ZBACKUPMYSQL=${ZBACKUP}/db_backup

## Folder name for backup and using date
DATE=`date +”%d%m%y”`

## Ldap Backup location separate by date
ZLDAPDUMPDIR=$ZBACKUPLDAP/$DATE
ZMYSQLDUMPDIR=$ZBACKUPMYSQL/$DATE

if [ ! -d $ZLDAPDUMPDIR ]; then
mkdir -p $ZLDAPDUMPDIR
fi

if [ ! -d $ZMYSQLDUMPDIR ]; then
mkdir -p $ZMYSQLDUMPDIR
fi

#Rsync Zimbra while still online…reduces downtime
rsync -avHK –delete –exclude ‘data.mdb’ /opt/zimbra ${ZBACKUP}/

#Backup ldap database
#This line is necessary after the first database backup because mdb_copy won’t delete existing file
mv $ZLDAPDUMPDIR/data.mdb $ZLDAPDUMPDIR/data.mdb.bak

su – zimbra -c “/opt/zimbra/common/bin/mdb_copy /opt/zimbra/data/ldap/mdb/db $ZLDAPDUMPDIR”
su – zimbra -c “/opt/zimbra/libexec/zmslapcat $ZLDAPDUMPDIR”
su – zimbra -c “/opt/zimbra/libexec/zmslapcat -c $ZLDAPDUMPDIR”
su – zimbra -c “/opt/zimbra/libexec/zmslapcat -a $ZLDAPDUMPDIR”

#Backup mariadatabase FULL
su – zimbra -c “mariabackup –backup -S /opt/zimbra/data/tmp/mysql/mysql.sock –target-dir=$ZLDAPDUMPDIR –user=root –password=`zmlocalconfig -s -m nokey mysql_root_password`”

#Stop Zimbra Services
su – zimbra -c ‘zmcontrol stop’

sleep 30

#Rsync again while zimbra services are stopped
rsync -avHK –delete –exclude ‘data.mdb’ /opt/zimbra ${ZBACKUP}/

#Start zimbra services
su – zimbra -c ‘zmcontrol start’

exit