add backup and restore function

This commit is contained in:
Robin van Genderen 2016-08-11 09:46:18 +02:00
parent 18e6267226
commit 5f356b66d7

View File

@ -10,6 +10,9 @@ def opt_args():
parser = kopano.parser('skpcfm')
parser.add_option("--user", dest="user", action="store", help="Run script for user")
parser.add_option("--list", dest="list", action="store_true", help="List recipients history")
parser.add_option("--backup", dest="backup", action="store_true", help="Backup recipients history")
parser.add_option("--restore", dest="restore", action="store_true", help="Restore recipients history")
parser.add_option("--restore-file", dest="restorefile", action="store", help="Restore from an other file then username.json")
parser.add_option("--remove", dest="remove", action="store", help="Remove recipients ")
parser.add_option("--remove-all", dest="removeall", action="store_true", help="Remove complete recipients history")
parser.add_option("--dry-run", dest="dryrun", action="store_true", help="Test script")
@ -21,19 +24,38 @@ def main():
options, args = opt_args()
if not options.user:
sys.exit('Please use:\n %s --user <username> ' % (sys.argv[0]))
user = kopano.Server(options).user(options.user)
print 'Please use:\n %s --user <username>' % (sys.argv[0])
sys.exit(0)
user = kopano.Server(options).user(options.user)
webapp = user.store.prop(0X6773001F).value
webapp = json.loads(webapp)
if options.backup:
f = open('%s.json' % user.name, 'w')
f.write(json.dumps(webapp, sort_keys=True,
indent=4, separators=(',', ': ')))
f.close()
sys.exit(0)
if options.restore:
if options.restorefile:
filename = options.restorefile
else:
filename = '%s.json' % user.name
with open(filename) as data_file:
data = json.load(data_file)
user.store.mapiobj.SetProps([SPropValue(0X6773001F, u'%s' % json.dumps(data))])
user.store.mapiobj.SaveChanges(KEEP_OPEN_READWRITE)
sys.exit(0)
if options.list:
print json.dumps(webapp, sort_keys=True,
indent=4, separators=(',', ': '))
sys.exit(0)
if options.remove:
newlist = json.loads('{"recipients":[]}')
for rec in webapp['recipients']:
if options.remove in rec['display_name'] or options.remove in rec['smtp_address'] \
or options.remove in rec['email_address']:
@ -45,12 +67,15 @@ def main():
user.store.mapiobj.SetProps([SPropValue(0X6773001F, u'%s' % json.dumps(newlist))])
user.store.mapiobj.SaveChanges(KEEP_OPEN_READWRITE)
sys.exit(0)
if options.removeall:
newlist = json.loads('{"recipients":[]}')
if not options.dryrun:
user.store.mapiobj.SetProps([SPropValue(0X6773001F, u'%s' % json.dumps(newlist))])
user.store.mapiobj.SaveChanges(KEEP_OPEN_READWRITE)
sys.exit(0)
if __name__ == "__main__":
main()