📜 ⬆️ ⬇️

Setting up backup and recovery of Zimbra OSE entirely and in separate boxes without using Zextras

1. Where to start


How do you start the backup? Planning. When backing up any system, you need to create a backup plan: what exactly, how often, how long to store, is there enough free space? From the answers to these questions follows the answer to the main question - how to back it up?

If there is a lot of free space, you can store the entire virtual machine. Make backups of the same Veeam on a schedule, and not think about the difficulties. But as for me, it’s a waste, I’m used to doing everything as concisely as possible and, if possible, economical. I, of course, have deployed Veeam, but I only backup them with systems that are either impossible or problematic and can be deployed from backup copies for a very long time.

Pro scripts controls the virtual environment just meaningly say nothing.

Zimbra has a zmmailbox tool. And, upon closer examination of its functionality, I realized that it would be more than enough for me. He can backup and restore boxes even on a live system. And I liked the ability to backup critical mailboxes separately from the backup of the entire system. Thus, the space occupied by backups will be limited by the size of the archived mailboxes multiplied by the number of “backup depth” days, and not by the volume of the entire system multiplied by the same number of days. In addition, with the backup of the entire system, in the case of Zimbra, it is extremely difficult to recover. It is much easier to copy a virtual machine using Veeam or virtual environment management tools (Hyper-V, ESXI, enter the necessary) immediately after setting up the system, and put it “on the shelf” so that at a critical moment you can quickly deploy almost nothing weighing VM and fill in it backups of boxes. In my opinion, this is the least expensive scenario in all respects.

2. Baseline:


Server OS : CentOS 7

About the OS
In fact, the difference between CentOS7 and any other system will be solely in the commands to the server to install packages, and possibly the location of some files. The work is carried out mainly with Zimbra cmdlets, so the configuration differences will be minimal.

Zimbra Domain : zimbramail.home.local
Username and password for access to the shared folder : ZimbraBackUp / 123123
Path to the shared folder : \\ BackUpServer1 \ ZM \
Path mount balls on Zimbra host : / mnt / ZM /

3. Customization


So, let's begin!

We will write backups to a server running Windows, in a shared folder. If you want, you can merge backup copies anywhere, but everything is set up in such a way that most backups are written exactly on BackUpServer1.home.local. So that:

1) Create a user in the domain to access the shared folder on this server so that you can mount it on the Zimbramail.home.local server. Username ZimbraBackUp, password, conditionally, 123123.

2) On the BackUpServer1 server, create a \ ZM \ directory in the repository, and share it with the ZimbraBackUp user for change. The path to the ball will be: \\ BackUpServer1 \ ZM \

3) Mount the ball to the server Zimbramail.home.local. In order for the folder to be mounted automatically, you need to correct the / etc / fstab file by adding the line to it:

//BackUpServer/ZM /mnt/ZM cifs user,uid=500,rw,suid,username=ZimbraBackUp,password=123123 0 0 

But first you need to check if the mount works:

 $ mount -t cifs //BackUpServer/ZM /mnt/ZM -o user=ZimbraBackUp,password=123123 

Often there is an error like this:

 mount: wrong fs type, bad option, bad superblock on //BackUpServer1/ZM/, missing codepage or helper program, or other error (for several filesystems (eg nfs, cifs) you might need a /sbin/mount.<type> helper program) In some cases useful info is found in syslog - try dmesg | tail or so. 

You can fix it by installing cifs-utils:

 $ yum install cifs-utils 

After setting, I recommend restarting the server.

4) Create 3 script files: FullBackUp.sh HandBackUp.sh Restore.sh

The first file will be used to automatically create backups on a schedule, the second to enable backup of individual mailboxes, or simply to manually launch "what if." And the third script is the restoration of one or all of the boxes at once. I tried to comment on the work of the scripts as much as possible and prescribed logging.

Content of the FullBackUp.sh file:
 #!/bin/bash #Куда положить бэкап Path="/mnt/ZM/BackUps" #Куда положить архив бэкапа ArchPath="/mnt/ZM/Archive" #Куда положить месячный бэкап MPath="/mnt/ZM/Mounthly" #Название домена Zimbra ZDomain="zimbramail.home.local" #Список ящиков MBoxes="/mnt/ZM/MBoxesList" #Текущая дата CDate=$(date +%d-%m-%Y) #Запоминаем день месяца MDay=$(date +%d) #Куда писать логи log="/mnt/ZM/BackUpLog.txt" echo -en "BackUp ALL MailBoxes started in $(date +%T)\n" >> $log #Проверка не существования каталога для резервного копирования if [ ! -d $Path ]; then #Создание каталога хранения резервных копий echo "Создание каталога хранения резервных копий..." mkdir -p $Path if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "BackUp dirctory was created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "BackUp dirctory was NOT created in $(date +%T)\n" >> $log fi else echo "Каталог хранения резервных копий существует, проверка существования каталога на сегодняшнее число..." fi #Првоерка не существования каталога на сегодняшнюю дату if [ ! -d $Path/$CDate ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo #Создание каталога хранения резервных копий на сегодняшнее число echo "Создание каталога хранения резервных копий на сегодняшнее число..." mkdir -p $Path/$CDate if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "BackUp CDate dirctory was created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "BackUp CDate dirctory was NOT created in $(date +%T)\n" >> $log fi else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo "Каталог хранения резервных копий существует, запись списка ящиков в файл..." fi #Запись списка ящиков в файл /opt/zimbra/bin/zmprov -l gaa $ZDomain > $MBoxes #Вывод результата записи списка if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Mail Boxes list created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Mail Box list is NOT created! in $(date +%T)\n" >> $log exit fi #создание резервных копий каждого ящика из списка for MailBox in $( cat $MBoxes); do echo "Создание резервной копии ящика $MailBox..." /opt/zimbra/bin/zmmailbox -z -m $MailBox getRestUrl "//?fmt=tgz" > $Path/$CDate/$MailBox if [ $? -eq 0 ]; then #Вывод результата создания резервной копии для каждого ящика echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Mail Box $MailBox BackUp created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Mail Box $MailBox BackUp is NOT created! in $(date +%T)\n" >> $log fi done #Очищаем файл со списком ящиков echo "Очистка файла со споском ящиков..." echo -n > $MBoxes if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "File $MBoxes clear\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "File $MBoxes can NOT be cleared\n" >> $log fi #Создание архива и работа с архивами #Проверка не существования каталога для архивирвоания if [ ! -d $ArchPath ]; then #Создание каталога хранения архивов echo "Создание каталога хранения архивов..." mkdir -p $ArchPath if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Archive dirctory was created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Archive dirctory was NOT created in $(date +%T)\n" >> $log fi else echo "Каталог хранения архивов существует, архививрование..." fi tar -czf $ArchPath/$CDate.tar $Path/$CDate if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Archive created in $(date +%T)\n" >> $log #Каждое первое число месяца копирование архива на долгосрочное хранение if [ "$MDay" = 1 ]; then #Проверка существования каталога хранения долгосрочных архивов if [ ! -d $ArchPath ]; then #Создание каталога хранения долговрочных архивов echo "Создание каталога хранения долгосроных архивов..." mkdir -p $ArchPath if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Long saving dirctory was created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Long saving dirctory was NOT created in $(date +%T)\n" >> $log fi else #Каталог существует echo "Каталог долгосрочного хранения архивов существует, копирование архива на долгосрочное хранение..." fi cp $ArchPath/$CDate.tgz $MPath/$CDate if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Archive is copied in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Archive is NOT copied in $(date +%T)\n" >> $log fi echo "Удаление старых резервных копий (старше 1 недели)..." #Удаление каталогов, содержащих резервные копии, старше недели find $Path -atime +7 | xargs rm -d if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Old BackUps files was deleted\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Old BackUps files is NOT deleted in $(date +%T)\n" >> $log fi #Удаление старых архивов бэкапов (старше 2 месяцев) echo "Удаление старых архивов резервных копий (старше 2 месяцев)..." find $Path -atime +61 | xargs rm -f if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Old BackUps archives was deleted\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Old BackUps archives is NOT deleted in $(date +%T)\n" >> $log fi fi else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Archive is NOT created in $(date +%T)\n" >> $log fi echo "BackUp job finished in $(date +%T)" #запись в лог-файл времени окончания резервного копирования echo -en "BackUp job finished in $(date +%T) $(date +%T)\n" >> $log echo -en "_____________________________________________\n" >> $log 

Contents of the HandBackUp.sh file:
 #!/bin/bash #Куда положить бэкап Path="/mnt/ZM/BackUps" #Куда положить архив бэкапа ArchPath="/mnt/ZM/Archive" #Название домена Zimbra ZDomain="zimbramail.home.local" #Список ящиков MBoxes="/mnt/ZM/MBoxesList" #Текущая дата CDate=$(date +%d-%m-%Y) #Куда писать логи log="/mnt/ZM/BackUpLog.txt" read -p "Введите имя почтового ящика (без указания домена), или ALL для резервного копирования всех почтовых ящиков: " A if [[ "$A" = "ALL" || "$A" = "all" ]]; then echo -en "BackUp started in $(date +%T)\n" >> $log #Запись списка ящиков в файл echo "Запись списка ящиков в файл..." /opt/zimbra/bin/zmprov -l gaa $ZDomain > $MBoxes if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Mail Boxes list created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Mail Box list is NOT created! in $(date +%T)\n" >> $log exit fi if ! [ -d $Path/$Date ]; then #Создание каталога резервного копирования mkdir -p $Path/$CDate/ echo -en "BackUp directory created in $(date +%T)\n" >> $log else #Создание резервных копий каждого ящика из списка echo "Создание резервных копий каждого ящика из списка" for MailBox in $( cat $MBoxes); do echo "Создание резервной копии ящика $MailBox..." /opt/zimbra/bin/zmmailbox -z -m $MailBox getRestUrl "//?fmt=tgz" > $Path/$CDate/$MailBox if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Mail Box $MailBox BackUp created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Mail Box $MailBox BackUp is NOT created! in $(date +%T)\n" >> $log fi done fi else MailBox="$A@$ZDomain" #Проверка существования запрошенного ящика echo "Проверка существования запрошенного ящика..." Result=$(/opt/zimbra/bin/zmprov getMailboxInfo $MailBox) if [ $? -eq 0 ]; then #Запрошенный ящик существует echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo "Запрошенный ящик $MailBox существует, резервное копирование..." echo -en "Required Mail Box $MailBox exist $(date +%T)\n" >> $log #Проверка существования каталога для резервного копирования if ! [ -d $Path/$Date ]; then #Создание резервной копии ящика mkdir -p $Path/$CDate/ echo -en "BackUp directory created in $(date +%T)\n" >> $log else #Создание резервной копии ящика $MailBox /opt/zimbra/bin/zmmailbox -z -m $MailBox getRestUrl "//?fmt=tgz" > $Path/$CDate/$MailBox if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Mail Box $MailBox BackUp created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Mail Box $MailBox BackUp is NOT created! in $(date +%T)\n" >> $log fi fi else #Запрошенный ящик не существовует - выход echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo "Запрошенный ящик $MailBox не существует. Завершение работы скрипта" echo -en "Required Mail Box $MailBox is not exist\n" >> $log exit fi fi read -p "Хотите запустить архивацию резервной копии ящика $MailBox? [N]: " F if [[ "$F" = "Y" || "$F" = "y" ]]; then #Проверка не существования каталога для архивирвоания if [ ! -d $ArchPath ]; then #Создание каталога хранения резервных копий echo "Создание каталога хранения архивов..." mkdir -p $ArchPath if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Archive dirctory was created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Archive dirctory was NOT created in $(date +%T)\n" >> $log fi else echo "Каталог хранения архивов существует, архививрование..." fi #Создание архива резервной копии echo "Архивирование резервной копии..." if [[ "$A" = "ALL" || "$A" = "all" ]]; then tar -czf $ArchPath/$CDate.tar $Path/$CDate if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Archive created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Archive is NOT created in $(date +%T)\n" >> $log fi else tar -czf $ArchPath/$MailBox.tar $Path/$CDate/$MailBox if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Archive created in $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Archive is NOT created in $(date +%T)\n" >> $log fi fi else echo "Архив создан не будет" echo -en "User decline archive creating\n" >> $log fi #Очищаем файл со списком ящиков echo "Очистка файла со споском ящиков..." echo -n > $MBoxes if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo fi echo "BackUp job finished in $(date +%T) $(date +%T)" #запись в лог-файл времени окончания резервного копирования echo -en "BackUp job finished in $(date +%T) $(date +%T)\n" >> $log echo -en "_____________________________________________\n" >> $log 

Contents of the Restore.sh file:
 #!/bin/bash #Где лежат бэкапы Path="/mnt/ZM/BackUps" #Название домена Zimbra ZDomain="zimbramail.home.local" #Список ящиков MBoxes="/mnt/ZM/MBoxesList" #Куда писать логи log="/mnt/ZM/RestoreLog.txt" read -p "Дата резервной копии, которую необходимо восстановить в формате 02-09-2001: " Date if ! [ -d $Path/$Date ]; then echo "Нет резервных копий на указанную дату." echo -en "No BackUp file at $Date $(date +%T)\n" > $log exit fi read -p "Введите имя почтового ящика (без указания домена), или ALL для восстановления всех почтовых ящиков на указанную дату: " A if [[ "$A" = "ALL" || "$A" = "all" ]]; then echo -en "Restore started in $(date +%T)\n" >> $log #Запись списка ящиков в файл ls "$Path/$Date" > $MBoxes for MailBox in $( cat $MBoxes); do #Проверка существования ящика echo "Проверка существования ящика $MailBox" Result=$(/opt/zimbra/bin/zmprov getMailboxInfo $MailBox) if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6) [OK]" echo echo -en "Start restore job for $MailBox $(date +%T)\n" >> $log echo "Ящик $MailBox существует. Восстановление..." else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Mail Box $MailBox does not exist, creating Mail Box $MailBox $(date +%T)\n" >> $log echo "Ящик $MailBox не существует. Создание ящика $MailBox..." #Проверка создания ящика Result=$(/opt/zimbra/bin/zmprov ca $MailBox 12345678 displayName "$MailBox") if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6) [OK]" echo echo -en "Mail Box $MailBox is created, starting restore $(date +%T)\n" >> $log echo "Ящик $MailBox создан успешно. Восстановление..." else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Can NOT create Mail Box $MailBox ! $(date +%T)\n" >> $log echo "Ящик $MailBox создать не удалось." fi fi #Восстановление ящика Result=$(/opt/zimbra/bin/zmmailbox -z -m $MailBox postRestURL "//?fmt=tgz&resolve=replace" $Path/$Date/$MailBox) if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Ящик $MailBox восстановлен в $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Ящик $MailBox НЕ восстановлен! $(date +%T)\n" >> $log fi done else #Проверка существования запрошенной резервной копии MailBox="$A@$ZDomain" if [ -a $Path/$Date/$MailBox ]; then #Проверка существования ящика echo "Проверка существования ящика $MailBox..." Result=$(/opt/zimbra/bin/zmprov getMailboxInfo $MailBox) if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6) [OK]" echo echo -en "Start restore job for $MailBox $(date +%T)\n" >> $log echo "Ящик $MailBox существует. Восстановление..." else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Mail Box $MailBox does not exist $(date +%T)\n" >> $log echo "Ящик $MailBox не существует." read -p "Хотите создать почтовый ящик с указанным именем и восстановить в него резервную копию? [N]: " B if [[ "$B" = "Y" || "$B" = "y" ]]; then echo "Создание почтового ящика $MailBox..." Result=$(/opt/zimbra/bin/zmprov ca $MailBox 12345678 displayName "$MailBox") #Проверка создания ящика if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6) [OK]" echo echo -en "Mail Box $MailBox is created, starting restore $(date +%T)\n" >> $log echo "Ящик $MailBox создан успешно. Восстановление..." else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo echo -en "Can NOT create Mail Box $MailBox ! $(date +%T)\n" >> $log echo "Ящик $MailBox создать не удалось. Завершение работы скрипта..." exit fi else #Ящик не будет создан, нечего восстанавливать. Выход echo "Ящик не будет создан. Завершение работы скрипта" exit fi fi #Восстановление ящика Result=$(/opt/zimbra/bin/zmmailbox -z -m $MailBox postRestURL "//?fmt=tgz&resolve=replace" $Path/$Date/$MailBox) if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo echo -en "Ящик $MailBox восстановлен в $(date +%T)\n" >> $log else echo -n "$(tput hpa $(tput cols))$(tput cub 6)[FAIL]" echo echo -en "Ящик $MailBox НЕ восстановлен! $(date +%T)\n" >> $log fi else #Запрошенной резервной копии не существует echo "Запрошенной резервной копии не существует. Завершение работы скрипта" echo -en "Required BackUp file is not exist\n" >> $log exit fi fi #Очищаем файл со списком ящиков echo "Очистка файла со споском ящиков..." Result(echo -n > $MBoxes) if [ $? -eq 0 ]; then echo -n "$(tput hpa $(tput cols))$(tput cub 6)[OK]" echo else echo -n "$(tput hpa $(tput cols))$(tput cub 6) [FAIL]" echo fi echo "BackUp job finished in $(date +%T) $(date +%T)" #запись в лог-файл времени окончания резервного копирования echo -en "Restore job complete in $(date +%T)\n" >> $log echo -en "____________________________________\n" >> $log 

There is one subtlety. If you create and edit script files for bash under Windows, then when you try to run the script, you will get the error: / bin / sh ^ M: bad interpreter: No such file or directory , which is that Windows editors add to the end of the line is the carriage return character "CR \ LF", which the editors in Linux do not display. But this symbol is there, and has not gone anywhere. To get rid of the error and remove the extra characters do the following:

 $ cat your-script.sh | tr -d '\r' > corrected-your-script.sh 

Well, or you can use the dos2unix utility, but it still needs to be installed:

 $ yum install dos2unix $ dos2unix your-script.sh 

5) Making the scripts executable:

 $ chmod 0740 /opt/zimbra/BkUpRestScripts/FullBackUp.sh $ chmod 0740 /opt/zimbra/BkUpRestScripts/HandBackUp.sh $ chmod 0740 /opt/zimbra/BkUpRestScripts/Restore.sh 

6) I recommend using my hands to run scripts and check if they work and how they work.

7) Create a task in CRON for daily mailbox backup:

 10 0 * * * root /opt/zimbra/ BkUpRestScripts/FullBackUp.sh 

8) Enjoy

4. Script work


In case the comments in the scripts themselves did not help.

1) FullBaclUp.sh script:
At the beginning, variables are determined, which one is responsible for what - everything is commented.

This is followed by checking the absence of a directory for backup; if it does not exist, it is created. An output is made to the log file and to the screen (if launched by the hands) about the success or failure of the creation of the catalog.

Check the absence of a catalog with today's date. If it does not exist, it is created, the same output to the screen and to the log of the result of creating the directory.

Write to the file all existing Zimbra mailboxes. It is needed for the consistent backup of each box. Displays the result of the command.

Creating a backup for each box from the list. With the output of the execution result on the screen and in the log file.

Clearing the mailbox list file. Since all 3 scripts work with this file, it is advisable to clean it after each pass so that there are no surprises. You can clean it before starting the script, but somehow I’m used to not keep extra data after doing the work, but doing the same thing before and after the script’s work is a slight degree of schizophrenia.

Next, the block to create a compressed archive.

Checking the absence of the archive storage directory, its creation in the absence and output to the screen and to the log of the creation result

Archiving.

Check "Is it the first number today?". I prefer to keep backups for the 1st of all months for a long time, I have enough of this to work with. You can store for weeks, but often it is redundant. But on top of it is set the condition for storing backups for the entire period of the mail server operation, so that they will forever lie there forever. If the number is the first, the resulting archive is copied to a separate “Mounthly” directory. And in order to copy it there - you need to check if there is such a directory, and if not - create it, what to report on the screen and in the log file.

Next, the storage is cleaned - all backups older than 2 weeks and archives older than 61 days are deleted. The result of the deletion is displayed on the screen and in the log file.

After that, the script writes the completion time in the log file and on the screen, and completes its work.

2) HandBackUp.sh script:
In the beginning, variables are also defined, also commented on why they are needed.

Next, the user is prompted to enter the name of the mailbox, which must be backed up, and without specifying the Zimbra domain. (it is written there in the invitation to enter), or select the backup of all mailboxes by writing ALL or all.

If the backup of all mailboxes is selected, then the script works in almost the same way as the first one listed above, except that after creating the backups of each mailbox, it will ask if you need to archive them?

If a specific mailbox was written, then its existence is checked in Zimbra. If a box with the specified name exists, then the process of backing up this mailbox is started, with the relevant information displayed on the screen and in the log file. If the specified mailbox does not exist, this will be reported to the user, and the script will complete its work.

After completing the backup box will be prompted to archive a copy. If the user agrees - there is a check of the absence of the catalog for storing the archive and further along the list, as in the first script.

After archiving is completed, the mailbox file is cleared. Just in case.

At the end, information about the time the script ends on the screen and to the log file is displayed.

3) Restore.sh script:
As in the previous files, first - the definition of variables with comments.

An invitation to the user to select the backup date to be restored. If there is no specified date in the backups, the script ends by informing about it.

If backups for the specified date exist - an invitation to the user to enter the name of the mailbox to be restored. To restore all the boxes you need to write ALL or all.

If the ALL parameter is selected, the script creates a list of files in the backup folder to a file, notifies of the result of the creation.

Next is the step-by-step recovery of each mailbox. First, check the existence of the box in the system, in case of absence, it is created, and then restored. About each operation information is displayed in the log file and on the screen.

If a particular box is selected, it is almost the same. Check for the existence of a file with the requested name. Output to the screen and to the log file of the result.

Check the existence of the box in the system. Output to the screen and to the log file of the result.
Inviting the user to agree to the creation of the box in the absence of such in the system. Output to the screen and to the log file of the result.

Create a box in case of consent. Output to the screen and to the log file of the result.
Recovery box. Output to the screen and to the log file of the result.

Cleanup file with a list of recovery boxes.

At the end, information about the time the script ends on the screen and to the log file is displayed.

5. Regarding recovery


If we slightly modify these scripts, they are also quite suitable for moving mail from the server to the server, simply by creating in the new place boxes with all the content. It is also possible to restore the work of the system from scratch, when for some reason it is easier to re-raise the Zimbra server than to try to revive the old one. Как было написано выше, можно сохранить образ настроенной виртуальной машины, развернув его при необходимости, и залив в него все данные. Тот же алгоритм действий при переезде с одного сервера Zimbra на другой. И не нужно никаких платных утилит типа Zextras.

6. Заключение


В целом, ничего сложного в написанных мной скриптах нет. Да, в них много условий, потому что я старался предвидеть как можно больше проблем и ошибок в их работе, но я так же постарался комментировать работу скрипта как можно более понятно. Скорее всего, «из коробки» они заработают не для всех. Может быть, кому-то вообще не подойдет такой способ резервного копирования. Но, надеюсь, многим окажутся полезными.

7. PS:


Это вторая статья из серии «как я «Zimbra» внедрял». Первая, про внедрение, LDAP-авторизацию и автоматическое создание ящиков для пользователей AD, вот тут .

И еще хочу отметить, что это мой первый опыт скриптования на баше, из-за этого скрипты получились такими громоздкими и «для особо умных», что называется.

Source: https://habr.com/ru/post/439728/