Excelente dica de backup das contas do Zimbra.
#!/usr/bin/perl -w
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"
;
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(); }
sub
backupAll() {
my
$GETUSER
= &getUsers();
my
$MAIL
=
$Opt
{backupUser};
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
"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
"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)
{
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:
Ele funciona no Zimbra 8.0.6 ?
Postar um comentário