-
Notifications
You must be signed in to change notification settings - Fork 0
/
zmdomainexport.py
executable file
·71 lines (60 loc) · 2.8 KB
/
zmdomainexport.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env python
import argparse
from subprocess import call
import zimbrasoap;
def main():
args = parse()
if not args.server:
print("Missing server name!")
exit(1)
if not args.domain:
print("Missing domain name!")
exit(1)
if not args.admin_user:
print("Missing admin user name!")
exit(1)
if not args.password:
print("Missing admin password!")
exit(1)
if not args.backup_dir:
print("Missing backup directory!")
exit(1)
# Setup zimbra soap session
print("Performing admin authentication...")
zmsoap = zimbrasoap.admin(server = args.server, trace=args.verbose)
zmsoap.Auth(name = args.admin_user, password = args.password)
accounts = []
if not args.users:
print("Getting all accounts...")
source_accounts = zmsoap.GetAllAccounts(domain = {'by':'name', 'value':args.domain})
for account in source_accounts.GetAllAccountsResponse.children():
if account.get_name() == 'account':
accounts.append(account['name'])
else:
accounts = args.users.split(',')
for account in accounts:
print("Exporting {0}...".format(account))
auth = zmsoap.DelegateAuth(attributes = {'duration':'86400'}, account = {'by':'name', 'value':account})
url = "https://{0}/home/{1}/?fmt={2}&auth=qp&zauthtoken={3}&meta=1".format(args.server,account,args.format,auth.authToken)
if args.wget:
rval = call(['wget', url, '-O', "{0}/{1}.{2}".format(args.backup_dir, account, args.format)])
else:
rval = call(['curl', url, '-o', "{0}/{1}.{2}".format(args.backup_dir, account, args.format)])
if rval != 0:
print("Error backing up: {0}, aborting!".format(account))
exit(1)
print("Finished successfully!")
def parse():
parser = argparse.ArgumentParser(description='Zimbra Domain Account Auto-Exporter')
parser.add_argument('-u', '--users', help="Users to migrate (optional, defaults to all)")
parser.add_argument('-d', '--domain', help='Domain to export')
parser.add_argument('-f', '--format', help='Format for export (tgz, tar, zip), default: tgz', default='tgz')
parser.add_argument('-s', '--server', help="Zimbra server hostname, default: zimbra.xmission.com", default='zimbra.xmission.com')
parser.add_argument('-a', '--admin_user', help="Zimbra Admin Username")
parser.add_argument('-p', '--password', help="Zimbra Admin Password")
parser.add_argument('-b', '--backup_dir', help="Directory for backups")
parser.add_argument('-w', '--wget', help="Use wget instead of curl", action='store_true')
parser.add_argument('-v', '--verbose', help="Verbose mode (SOAP Tracing)", action='store_true')
return parser.parse_args()
if __name__ == '__main__':
main()