from time import strftime
# Open the list of Zimbra users and read it into a list
userfile = open("/opt/zimbra/accountlists/zimbrausers.txt",'r')
userlist = userfile.readlines()
# Set the current date - used to create and set the backup destination directory
cur_date = strftime("%Y-%m-%d")
backupdir = "/zimbra-backups/calendar-backups/" + cur_date
os.system("mkdir " + backupdir)
# Go through each user in the userlist
for user in userlist:
# The account name is obtained by dropping the \n at the end of each line
cur_account = user[:-1]
# The username is obtained by dropping the @ sign and anything after from each line and is used for the backup file name.
cur_user = user.split('@')
# Assemble the command to get the list of all users calendar folders
# Exclude external folders on another account (thus excluding lines with @)
# Exclude externally subscribed .ics files
command = "zmmailbox -z -m " + cur_account + " gaf | grep -i appo | grep -v '\@' | grep -v .ics"
# Execute the command, read the output into a list
output = os.popen(command).readlines()
# For each entry in the list, split out the calendars, if they exist
for line in output:
calendar_name = line.split("/")[:-1]
command = "zmmailbox -z -m " + cur_account + " getRestURL /\"" + calendar_name + "\" > \"" + backupdir + "/" + cur_user + "-" + calendar_name + ".ics\""
command2 = "bzip2 \"" + backupdir + "/" + cur_user + "-" + calendar_name + ".ics\""