View Single Post
  #184 (permalink)  
Old 05-06-2009, 06:26 PM
tiger2000 tiger2000 is offline
Elite Member
 
Posts: 296
Default

Here's my steps and scripts for per-user backup/restore :

(please correct me if there's anything not correct)

Backup:
Run this script by : backup.sh filename
and you can put the user account into such filename.

backup.sh:

Code:
#!/bin/bash

if [ $# -ne 1 ]; then
   echo "Usage: $0 filename"
   exit 1
fi

domain="@my.domain"
backupdir="/root/backup/users"

echo "Start to backup => `date`"

if [ ! -d $backupdir ]; then
   echo "$backupdir not existing, create it now automatically"
   mkdir -p $backupdir
fi

cat $1 |
while read line; do
  echo -n "backup user : $line..."
  sudo -u zimbra /opt/zimbra/bin/zmmailbox -z -m $line$domain getRestURL "//?fmt=tgz" > $backupdir/$line.tgz
  if [ $? -eq 0 ]; then
     echo "OK [`du -h $backupdir/$line.tgz | awk '{print $1}'`]"
  else
     echo "Fail"
  fi

done

echo "Backup finish => `date`"

Restore:
Run this script by : restore.sh username {reset,skip}
e.g. restore.sh superman skip


restore.sh

Code:
#!/bin/bash

domain="@my.domain"
backupdir="/root/backup/users"

if [ $# -ne 2 ]; then
   echo "Usage: $0 account [reset, skip]"
   exit 1
fi

case "$2" in
        reset)
                OPTION="reset"
                ;;
        skip)
                OPTION="skip"
                ;;
        *)
                echo "Usage: $0 account [reset, skip]"
                exit 3
esac

echo "Start to restore => `date`"

echo -n "Check account [$1] ==> "
sudo -u zimbra /opt/zimbra/bin/zmprov ga $1$domain > /dev/null 2>&1
if [ $? -ne 0 ]; then
   echo "NO, [$1] not exist"
   exit 2
else
   echo "Yes, we have [$1]"
fi

echo -n "Check backup data for [$1] ==> "
if [ ! -e $backupdir/$1.tgz ]; then
   echo "No, no backup data for [$1]"
   exit 3
else
   echo "Yes, backup data for [$1] exists"
fi

echo -n "Start to restore user [$1] ==> "
sudo -u zimbra /opt/zimbra/bin/zmmailbox -z -m $1$domain postRestURL "//?fmt=tgz&resolve=$OPTION" $backupdir/$1.tgz

if [ $? -eq 0 ]; then
   echo "OK"
else
   echo "Fail"
fi

echo "Restore finish => `date`"




If you're going to backup ALL users in your system, then you can use this backupall.sh ;

Code:
#!/bin/bash

domain="@my.domain"
backupdir="/root/backup/users"

echo "Start to backup => `date`"

if [ ! -d $backupdir ]; then
   echo "$backupdir not existing, create it now automatically"
   mkdir -p $backupdir
fi

index=1
sudo -u zimbra /opt/zimbra/bin/zmprov gaa |
while read line; do
  echo -n "[$index]backup user : $line..."
  username=`echo $line | cut -d@ -f1`
  sudo -u zimbra /opt/zimbra/bin/zmmailbox -z -m $line getRestURL "//?fmt=tgz" > $backupdir/$username.tgz
  if [ $? -eq 0 ]; then
     echo "OK [`du -h $backupdir/$username.tgz | awk '{print $1}'`]"
  else
     echo "Fail"
  fi

  index=`expr $index + 1`
done

echo "Backup finish => `date`"

Last edited by tiger2000; 05-06-2009 at 08:51 PM..
Reply With Quote