Back up whole Zope through-the-web

Submitted by: chrism
Last Edited: 2001-07-09

Category: Python(External Method)

Average rating is: 4.75 out of 5 (8 ratings)

Zope's 'export' functionality allows you to create backups of
particular folders or other non-containerish objects in your Zope
site. However, Zope by default does not allow you to export its
"root" object. There are reasons for this, not very many of them

Although the same goal may be accomplished by simply backing up the
Data.fs file in a stock Zope install, many folks desire the ability
to export the Zope "root" object in order to make backups of Zope
"through the web". The following code snippet can be turned into
two external methods: "backup" and "restore". The backup function
allows you to export your entire ZODB into a file and subsequently
download it via the web. The restore function allows you to restore
a file backed up in this way.

Source (Text):
import Globals
import os
from Acquisition import aq_base
from cStringIO import StringIO
from Globals import MessageDialog
from ZODB.PersistentMapping import PersistentMapping

def backup(self, REQUEST=None, download=1, filename='allzope.zexp'):
    """ saves backup file to var or allows download of backup file"""
    if download:  f = StringIO()
    else:  f = os.path.join(Globals.data_dir, filename)
    self.c = PersistentMapping()
    for k, v in self.objectItems():  self.c[k] = aq_base(v)
    self.c._p_jar.exportFile(self.c._p_oid, f)
    del self.c
    if download:
        REQUEST.RESPONSE.setHeader('Content-type', 'application/data')
                                   'inline;filename=%s' % filename)
        return f.getvalue()
        return MessageDialog(
            title="Zope backed up successfully",
            message="<EM>All items in root</EM> sucessfully\
            exported to <pre>%s</pre>." % f,

def restore(self, REQUEST=None, filename='allzope.zexp'):
    """ restores backup file from 'import' """
    dirname, file=os.path.split(filename)
    if dirname:
        raise 'Bad Request', 'Invalid file name %s' % filename

    instance_home = INSTANCE_HOME
    software_home = os.path.join(SOFTWARE_HOME, '..%s..' % os.sep)
    software_home = os.path.normpath(software_home)

    for impath in (instance_home, software_home):
        filepath = os.path.join(impath, 'import', filename)
        if os.path.exists(filepath):
        raise 'Bad Request', 'File does not exist: %s' % filename

    conn = self._p_jar
    ob = conn.importFile(filepath)
    for k, v in ob.items():
        try: self._delObject(k)
        except AttributeError: pass
        self._setObject(k, v)
    return MessageDialog(
            title="Zope restored successfully",
            message="All items from <EM>%s</EM> sucessfully\
            imported into <pre>root</pre>." % filepath,

Create a module,, with the contents of the source
given here within the Extensions directory of your Zope instance.

Create two external methods, "restore" and "backup" in your Zope root
folder by pointing them at Extensions/ -> restore
and Extensions/ -> backup.

After creating the two external methods, protect
each of them with a management-level permission! If you don't, if
anonymous users can run either, you'll be in a world of hurt, as
people can nab your passwords and arbitrarily overwrite portions of
your site.

To back up your site, visit the "test" tab of the backup external
method. A file named "allzope.zexp" will be downloaded to
your local machine via your browser. This file contains all
the objects within your Zope site.

To restore your site, place the "allzope.zexp" file in the "import"
directory of your Zope instance, and press the "test" tab of the
restore external method. Be careful, this overwrites anything
that exists with the same name in your Zope install.

Note that the "zexp" file created by 'backup' may not successfully
be imported using the standard Zope import feature, it needs to be
imported via the "restore" external method.


neat by kbolton - 2001-06-11
After trying to get this to work on Zope 2.3.2 linux x86 and getting a SyntaxError, I tried it on a win98 box running the same version of Zope and it works beautifully. One thing (feature? bug?) is that restores happen around newer objects. Barring identical IDs, anything you create between your last backup and your restore doesn't seem to be affected!