Skip to content

Commit 8e8528b

Browse files
committed
Add restoreDirMailbox.pl
1 parent 51dca9b commit 8e8528b

File tree

2 files changed

+170
-1
lines changed

2 files changed

+170
-1
lines changed

README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,27 @@
11
zimbraTools
22
===========
33

4-
Scripts Zimbra
4+
Scripts Zimbra
5+
6+
#sizeAccount.pl
7+
8+
Find instantly account size with mysql size_checkpoint.
9+
You have to fix the following variables if necessary.
10+
11+
##Example of use:
12+
13+
```bash
14+
15+
su - zimbra
16+
./sizeAccount.pl
17+
```
18+
19+
#restoreDirMailbox.pl
20+
21+
Can restore a directory from a backup into an account.
22+
Restore an account in account_bak, copy a directory into account and delete account_bak.
23+
24+
##Example of use:
25+
```bash
26+
restoreDirMailbox.pl <[email protected]> <LabelBackup> <dir/subdir...>
27+
```

restoreDirMailbox.pl

+146
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
#!/usr/bin/perl -w
2+
use strict;
3+
4+
5+
####### MAIN ##########
6+
7+
my $account = shift;
8+
my $backupId = shift;
9+
my $backupIdFull ='';
10+
my (@listedir) = @ARGV;
11+
12+
13+
checkParam();
14+
restoreAccountBak();
15+
16+
foreach (@listedir){
17+
saveDirAccountBak($_);
18+
restoreDirAccount($_);
19+
}
20+
21+
delAccountBak();
22+
23+
24+
#Full backup for an account
25+
#zmbackup -f - demo1.domain.com -a [email protected]
26+
27+
#Restore
28+
#zmrestore -a $account -lb $backupIdFULL -ca -pre bak_
29+
#zmrestore -a $account -restoreToIncrLabel $backupId -lb $backupIdFull -br -ca -pre bak_
30+
31+
#Create directory into inbox
32+
#zmmailbox -z -m [email protected] cf /inbox/Rep
33+
34+
#Rename directory
35+
#zmmailbox -z -m [email protected] rf /inbox/toto/foo /inbox/toto/foo_bak
36+
37+
##############################
38+
39+
40+
41+
#Check script parameters
42+
sub checkParam{
43+
if(($account && $account =~ /^[^ ]+@[^ ]+$/) && (!$backupId) ){
44+
print `zmbackupquery -a $account`;
45+
exit 0;
46+
}
47+
48+
if(($account && $account =~ /^[^ ]+@[^ ]+$/) && ($backupId && $backupId =~ /^(full|incr)[^ ]+$/) && (@listedir && $#listedir >= 0)){
49+
if ($backupId =~ /^incr[^ ]+$/){
50+
my @query = `zmbackupquery -a $account`;
51+
my $flag = 0;
52+
foreach (@query){
53+
$flag = 1 if $_ =~ /^[ ]+Label:[ ]+$backupId/;
54+
if($_ =~ /^[ ]+Label:[ ]+(full.*)/ && $flag eq 1){
55+
$backupIdFull = $1;
56+
chomp($backupIdFull);
57+
last;
58+
}
59+
}
60+
if(!$backupIdFull || $backupIdFull eq ''){
61+
echo_red("backup full non trouvé");
62+
exit 0;
63+
}
64+
}
65+
return 1;
66+
}
67+
68+
echo_white("$0 <user\@domain.com> <LabelBackup> <dir/subdir...>");
69+
echo_blue("\t<dir/subdir> : For a directory into \"Boite de Reception\" you have to prefix with ",1); echo_red("inbox/",1); echo_blue(" example inbox/dir/sudir");
70+
exit 0;
71+
}
72+
73+
74+
### restore account to bak_compte
75+
sub restoreAccountBak{
76+
echo_green("restoreAccountBak $account --> bak_$account");
77+
# print "zmrestore -a $account -ca -pre bak_\n";
78+
79+
if($backupIdFull eq ''){
80+
print `zmrestore -a $account -lb $backupId -ca -pre bak_`;
81+
}else{
82+
print `zmrestore -a $account -restoreToIncrLabel $backupId -lb $backupIdFull -br -ca -pre bak_`;
83+
}
84+
}
85+
86+
87+
### Backup account directory in /tmp
88+
sub saveDirAccountBak{
89+
my $dir = shift;
90+
echo_green("saveDirAccountBak - $dir");
91+
92+
93+
# print `zmmailbox -z -m bak_$account rf /inbox/$dir /inbox/$dir\_bak`;
94+
print `zmmailbox -z -m bak_$account rf "/$dir" "/$dir\_bak"`;
95+
# print `zmmailbox -z -m bak_$account getRestURL "/inbox/$dir\_bak?fmt=zip" > /tmp/$account.zip`;
96+
print `zmmailbox -z -m bak_$account getRestURL "/$dir\_bak?fmt=zip" > /tmp/$account.zip`;
97+
}
98+
99+
### Restore directory in rep_bak
100+
sub restoreDirAccount{
101+
my $dir = shift;
102+
echo_green("restoreDirAccount - $dir");
103+
# print "zmmailbox -z -m $account postRestURL \"/?fmt=zip\" /tmp/$account.zip\n";
104+
print `zmmailbox -z -m $account postRestURL "/?fmt=zip" /tmp/$account.zip`;
105+
}
106+
107+
### Delete account_bak
108+
sub delAccountBak{
109+
echo_green("delAccountBak - bak_$account");
110+
# print "zmprov da bak_$account\n";
111+
print `zmprov da bak_$account`;
112+
}
113+
114+
115+
116+
sub echo_red {
117+
print defined($_[1]) ? "\033[0;31m$_[0]\033[0;37m" : "\033[0;31m$_[0]\033[0;37m\n";
118+
}
119+
120+
sub echo_green {
121+
print defined($_[1]) ? "\033[0;32m$_[0]\033[0;37m" : "\033[0;32m$_[0]\033[0;37m\n";
122+
}
123+
124+
sub echo_yellow {
125+
print defined($_[1]) ? "\033[0;33m$_[0]\033[0;37m" : "\033[0;33m$_[0]\033[0;37m\n";
126+
}
127+
128+
sub echo_blue {
129+
print defined($_[1]) ? "\033[0;34m$_[0]\033[0;37m" : "\033[0;34m$_[0]\033[0;37m\n";
130+
}
131+
132+
sub echo_pink {
133+
print defined($_[1]) ? "\033[0;35m$_[0]\033[0;37m" : "\033[0;35m$_[0]\033[0;37m\n";
134+
}
135+
136+
sub echo_cyan {
137+
print defined($_[1]) ? "\033[0;36m$_[0]\033[0;37m" : "\033[0;36m$_[0]\033[0;37m\n";
138+
}
139+
140+
sub echo_white {
141+
print defined($_[1]) ? "\033[0;37m$_[0]\033[0;37m" : "\033[0;37m$_[0]\033[0;37m\n";
142+
}
143+
144+
sub echo_grey {
145+
print defined($_[1]) ? "\033[1;30m$_[0]\033[0;37m" : "\033[1;30m$_[0]\033[0;37m\n";
146+
}

0 commit comments

Comments
 (0)