Pesquisar neste blog

quinta-feira, 22 de agosto de 2013

Backup/Restore Zimbra 8

Excelente dica de backup das contas do Zimbra.

zimbra_backup



#!/usr/bin/perl -w
### $Id: export-acc-zcs.pl,v 0.5 2012/11/19 12:35:56 knitterb Exp $
### Autor Jose Nilton
### http://www.zimbra.com/forums/developers/10287-ldap-attributes-different-output-perl-php.html
### http://wiki.zimbra.com/wiki/Mbox_to_SMTP_with_Perl
use strict;
use warnings;
use Getopt::Long qw(GetOptions);
use lib "/opt/zimbra/zimbramon/lib";
use Net::LDAP;
use XML::Simple;
$ENV{'PATH'} = "/sbin:/bin:/usr/sbin:/usr/bin:/opt/zimbra/openldap/bin:/opt/zimbra/openldap/sbin:/opt/zimbra/bin";
#Variaveis Ambientes
my $WORKDIR    '/opt/zimbra/backup';
my $localxml   = XMLin("/opt/zimbra/conf/localconfig.xml");
my $LDAPMASTERSERVER $localxml->{key}->{ldap_url}->{value};
my $LDAPZIMBRADN     $localxml->{key}->{zimbra_ldap_userdn}->{value};
my $LDAPZIMBRAPASS   $localxml->{key}->{zimbra_ldap_password}->{value};
my $FROMADDRESS      $localxml->{key}->{smtp_source}->{value};
my $TOADDRESS        $localxml->{key}->{smtp_destination}->{value};
my $SMTPHOST         $localxml->{key}->{zimbra_server_hostname}->{value};
my %Opt$MAILHOST );
(&help()) if (!$ARGV[0]);
GetOptions( \%Opt"backup""listUsers""restore""dominio=s") or ( &help() );
if($Opt{backup}){ &backupAll(); }
if($Opt{listUsers}){ &getUsers(); }
if($Opt{dominio} && $Opt{restore}){ &restoreAll(); }
###FUNCTIONS###
sub backupAll() {
my $GETUSER = &getUsers();
my $MAIL $Opt{backupUser};
    ## Backup para todas as contas
    while ( ($MAIL$MAILHOST) = each(%$GETUSER) ) {
      my @attr split (/\@/,$MAIL);
      my $TEMPDIR $WORKDIR .'/'$attr[1];
      next if ($attr[0] =~ m/(admin|virus-quarantine|spam.|ham.)/);
      unless(-d  $TEMPDIR){system("mkdir -p $TEMPDIR");}
      unless(-e "$TEMPDIR/ldap.bak"){system(qq{slapcat -F /opt/zimbra/data/ldap/config -b "" -l $TEMPDIR/ldap.bak});};
      print "BACKUP EMAIL:$MAIL PASTA:$TEMPDIR \n";
      system(qq{ldapsearch -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -w $LDAPZIMBRAPASS
                    -b '' -LLL "(zimbraMailDeliveryAddress=$MAIL)" $TEMPDIR/$MAIL.ldiff});
    }
}
sub restoreAll() {
my $GETUSER = &getUsers();
my $DOMINIO $Opt{dominio};
opendir(DIR, "$WORKDIR/$DOMINIO/") || die "Error in opening dir $DOMINIO\n";
my @MAILS grep(/\.ldiff$/,readdir(DIR));
closedir(DIR);
foreach my $MAIL (@MAILS) {
   $MAIL =~ s/\.ldiff//g;
   $MAILHOST $GETUSER->{$MAIL};
if(-e "$WORKDIR/$DOMINIO/$MAIL.ldiff"){
        if($MAILHOST){
        #print "MAILHOST:$MAILHOST FILE:$WORKDIR/$DOMINIO/$MAIL.tgz\n";
        print "A conta $MAIL ja existe no Servidor...\n";
        }else{
                $MAILHOST = qq{grep ^zimbraMailHost $WORKDIR/$DOMINIO/$MAIL.ldiff | awk '{print \$2}'};
                $MAILHOST = `$MAILHOST`; chomp $MAILHOST;
                #print "MAILHOST:$MAILHOST FILE:$WORKDIR/$DOMINIO/$MAIL.tgz\n";
                print "Restore LDAP ACCOUNT $MAIL...\n";
                system(qq{ldapadd -x -H $LDAPMASTERSERVER -D $LDAPZIMBRADN -c -w $LDAPZIMBRAPASS -f$WORKDIR/$DOMINIO/$MAIL.ldiff});
        }
 }
}
}
sub getUsers() {
my $hash_ref;
my $ldap = Net::LDAP->new($LDAPMASTERSERVER) or die "$@";
$ldap->bind ($LDAPZIMBRADN, password => $LDAPZIMBRAPASS) or die "$@";
my $result $ldap->search(base => '',scope => 'sub',filter => '(objectClass=zimbraAccount)') or die "$@";
if $result->code ){ print $result->error."\n"}
elsif ($result->count > 0)
{
  # now work through all of our defined dirs
  for (my $i = 0 ; $i $result->count ; $i++)
  {
    my $entry $result->entry($i);
    next if !$entry->get_value('zimbraMailDeliveryAddress');
    $hash_ref->{ $entry->get_value('zimbraMailDeliveryAddress') } = $entry->get_value('zimbraMailHost');
    if($Opt{listUsers}){ print $entry->get_value('zimbraMailDeliveryAddress') . "\n"; }
  }
}
$ldap->unbind;
return $hash_ref;
}
sub help() {
print "Usage:
     $0 --backup
          Executa o backup full de todas as contas.
     $0 --listUsers
          Lista todas as contas
     $0 --restore --dominio=dominio.com.br
          Para restaurar todas as contas de email
"

O Backup vai ficar armazenado na pasta /opt/zimbra/backup/nome_do_dominio
Fonte: http://www.linuxpro.com.br/blog/2012/12/backups-das-contas-no-zimbra/

Um comentário:

Edson F. Cunha disse...

Ele funciona no Zimbra 8.0.6 ?