Zimbra offers Open Source email server software and shared calendar for Linux and the Mac
Go Back   Zimbra :: Forums > Zimbra Collaboration Suite > Migration

Welcome to the Zimbra :: Forums!
Welcome, if you would like to post a comment please register. We also encourage you to explore all things Zimbra with our team and members of the community.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #181 (permalink)  
Old 04-28-2009, 03:06 PM
Member
 
Posts: 10
Default

Does anyone know the answer to the question here, which is: How does one backup/restore a single user?
Reply With Quote
  #182 (permalink)  
Old 04-29-2009, 02:22 AM
Zimbra Consultant & Moderator
 
Posts: 19,655
Default

Quote:
Originally Posted by zakai View Post
Does anyone know the answer to the question here, which is: How does one backup/restore a single user?
One method: restore your backup, find the user account then inject them into the server with zmlmtpinject?
__________________
Regards


Bill
Reply With Quote
  #183 (permalink)  
Old 05-06-2009, 12:08 AM
Starter Member
 
Posts: 1
Default

Quote:
Originally Posted by mmarodin View Post
This is a per-user full backup script (based on mubley's code, 2005), for Zimbra 5.x. Zimbra DB and LDAP are also included.
I've tested it on 5.0.12 version.

I don't want to use ...
Code:
/opt/zimbra/bin/zmmailbox -z -m user@domain.com getRestURL “//?fmt=tgz” > /tmp/account.tgz
... because with this backup job I could not restore single messages, but only full mailbox.

I hope it could be usefull for you!

Code:
#!/bin/sh
#--------

# Backup Zimbra Open Source 5.x
# by mm@rodin - 20090206

# Modify the following variables to suit your installation
export time=`date +%y%m%d`
export backup_dir=/opt/backup
export zimbra_dir=/opt/zimbra
export index_dir=$zimbra_dir/index
export store_dir=$zimbra_dir/store

# We need mysqldump.  If it doesn't exist,
# copy mysql and modify it to call mysqldump.
if [ ! -e $zimbra_dir/bin/mysqldump ]; then
   cp $zimbra_dir/bin/mysql $zimbra_dir/bin/mysqldump
   sed -i 's|/bin/mysql |/bin/mysqldump |g' $zimbra_dir/bin/mysqldump
fi

# Create the backup directory
mkdir -p $backup_dir

# Get a list of all accounts and execute
# the contents of the for-loop for each account.
for i in `$zimbra_dir/bin/zmprov gaa` ; do

# Beginning of for-loop

#  Use email address to get zimbra_id
   zimbraId=`$zimbra_dir/bin/zmprov ga $i |grep zimbraId |sed 's|zimbraId: ||g'`
   set $zimbraId

#  Use mysql to get number id
   zimbra_nr=`$zimbra_dir/bin/mysql -e "use zimbra; select id from mailbox where account_id = '$1'"`
   zimbra_nr=`echo $zimbra_nr | sed 's|id ||g'`

#  Check to make sure we got a valid number id
   if [ "$zimbra_nr" = "" ]; then
      echo $i has no mailbox.
   else

#     Set zimbraAccountStatus to "maintenance"
      $zimbra_dir/bin/zmprov ma $i zimbraAccountStatus maintenance

#     Marking user backup
      if [ ! -e $backup_dir/info-$zimbra_nr.txt ]; then
         echo "Zimbra ID:	$1" > $backup_dir/info-$zimbra_nr.txt
         echo "Mail address:	$i" >> $backup_dir/info-$zimbra_nr.txt
      fi

#     Backup index and store files
      tar -czf $backup_dir/$time-$zimbra_nr-data.tgz $zimbra_dir/index/0/$zimbra_nr $zimbra_dir/store/0/$zimbra_nr

#     Backup Mysql db
      $zimbra_dir/bin/mysqldump mboxgroup$zimbra_nr > $backup_dir/$time-$zimbra_nr.dump
      tar -czf $backup_dir/$time-$zimbra_nr.dump.tgz $backup_dir/$time-$zimbra_nr.dump
      rm -f $backup_dir/$time-$zimbra_nr.dump

#     Set zimbraAccountStatus to "active"
      $zimbra_dir/bin/zmprov ma $i zimbraAccountStatus active

#  End of if statement
   fi

# End of for-loop
done

# Backup Zimbra master database and LDAP accounts database
# First, set all accounts to maintenance.
for i in `$zimbra_dir/bin/zmprov gaa` ; do
   $zimbra_dir/bin/zmprov ma $i zimbraAccountStatus maintenance
done

$zimbra_dir/bin/mysqldump zimbra  > $backup_dir/$time-zimbra.dump
tar -czf $backup_dir/$time-zimbra.dump.tgz $backup_dir/$time-zimbra.dump
rm -f $backup_dir/$time-zimbra.dump

$zimbra_dir/bin/zmslapcat > $backup_dir/$time-zimbra.ldap
tar -czf $backup_dir/$time-zimbra.ldap.tgz $backup_dir/$time-zimbra.ldap
rm -f $backup_dir/$time-zimbra.ldap

# Setting all accounts to active mode.
for i in `$zimbra_dir/bin/zmprov gaa` ; do
   $zimbra_dir/bin/zmprov ma $i zimbraAccountStatus active
done

# Backup complete
This looks promising for my needs, but can you describe with some detail the restore procedure? It's fairly clear to me on the conceptual level what this script is doing, but I'd appreciate an example of the steps needed to to a full restore from the backup files this script produces.
Reply With Quote
  #184 (permalink)  
Old 05-06-2009, 07:26 PM
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 09:51 PM..
Reply With Quote
  #185 (permalink)  
Old 05-27-2009, 02:33 AM
Junior Member
 
Posts: 9
Default

Thank you very much!
Reply With Quote
  #186 (permalink)  
Old 05-27-2009, 08:42 AM
Intermediate Member
 
Posts: 16
Default

Does this script still requires to turn off Zimbra in order to run the backup/restore job?
Has somebody else done testing?

Thanks,

Ed
__________________
Release 5.0.16_GA_2921.UBUNTU8 UBUNTU8 FOSS edition
Reply With Quote
  #187 (permalink)  
Old 06-02-2009, 12:47 PM
Member
 
Posts: 10
Thumbs down Erro restore with restore.sh

I ran backup.sh ok
but I ran restore.sh and not ok

Start to restore => Seg Jun 1 04:23:42 BRT 2009
Check account [marcos] ==> Yes, we have [marcos]
Check backup data for [marcos] ==> Yes, backup data for [marcos] exists
Start to restore user [marcos] ==> ERROR: zclient.CLIENT_ERROR (file not found: /root/backup/users/marcos.tgz) (cause:java.io.FileNotFoundException
/root/backup/users/marcos.tgz (Permission denied)) Fail
Reply With Quote
  #188 (permalink)  
Old 06-02-2009, 01:19 PM
Member
 
Posts: 10
Default Resolved

I changed my permissions and worked.

I have one more question.

Can I change backup.sh ( full to incremental ) ?
Reply With Quote
  #189 (permalink)  
Old 06-02-2009, 06:51 PM
Elite Member
 
Posts: 296
Default

>Does this script still requires to turn off Zimbra in order to run the >backup/restore job?

No, you don't need to turn off Zimbra during backup, because it's a online dump for user's folders.
Reply With Quote
  #190 (permalink)  
Old 06-02-2009, 07:00 PM
Elite Member
 
Posts: 296
Default

> Can I change backup.sh ( full to incremental ) ?

i'm not sure, because I don't whether there's any parameter for 'getResturl' access. did you see that 'postResturl' (the restore script) did have several parameters which can be used for reset or skip.. *maybe* 'getResturl' also does, but i'm not sure.

perhaps someone who's more familiar with "zmmailbox with getRestUrl" can help here ?
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads

Why Join?

Registering let's you ask questions, makes it easier to search, displays any files attached to posts, and notifies you about replies.

blog.zimbra.com




 

SEO by vBSEO ©2011, Crawlability, Inc.