#!/usr/bin/python

#  Script: backup_sites.py
#  Version: 1.0
#  Author: Marcel Mare (WebToTheMax)
#
#  Purpose: Backs up series of production plone sites to the local machine. Puts 
#  a timestamp in the file name for easy identification so the file name becomes:
#  shortname_20041231_17:45:23.zexp
#
#  Tested on OS X only, but will probably work on any platform that has Curl
#  Freeware. Use at your own risk. 
# 
#  Based on some shell scripts posted on the Plone or Zope User list. I'd really like to give credit
#  to the author but I am unable to find the message (I hate GMANE). 


import os, time

# == Start of config =====================================

# You need to modify the list of sites to backup:
# 
# Available keys: 
#   shortname: a convenient name for the site, also used as a base for the backup file
#   location : the URL minus http://
#   username : a username of a Manager
#   password : his pw
#   bakdir   : where to backup to (include trailing slash)

SITES = ( {'shortname':'mysite', 'location':'mysite.com'},
          {'shortname':'yoursite', 'location':'yoursite.org'},
        )

# you might want to change these defaults; might save some time typing the sites list
DEFAULT_USERNAME    = "admin"
DEFAULT_PASSWORD    = "wouldn't you like to know"
DEFAULT_BACKUP_DIR  = "/Volumes/LaCie1/backups/Plone_sites/"

TIMESTAMP        = time.strftime("%Y%m%d_%H:%M:%S")
DEFAULT_PARAMS   = {'username'   : DEFAULT_USERNAME, 
                    'pw'         : DEFAULT_PASSWORD, 
                    'timestamp'  : TIMESTAMP,
                    'bakdir'     : DEFAULT_BACKUP_DIR,
                   }

# == End of config ========================================   

CMD = "curl -u %(username)s:%(pw)s -o %(bakdir)s%(shortname)s_%(timestamp)s.zexp http://%(location)s/manage_exportObject?download:int=1"


for site in SITES: 
  params = DEFAULT_PARAMS.copy()
  params.update(site) 
  command = CMD % params
  # print "Running ", command
  result = os.system(command)
  print "Backup of %s returned %d" % (params['shortname'], result)
  
