Back to index

moin  1.9.0~rc2
backup.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - download a backup via http.
00004 
00005     Triggering backup action will check if you are authorized to do a backup
00006     and if yes, just send a <siteid>-<date>--<time>.tar.<format> to you.
00007     What exactly is contained in your backup depends on your wiki's
00008     configuration - please make sure you have everything you need BEFORE you
00009     really need it.
00010 
00011     Note: there is no restore support, you need somebody having access to your
00012           wiki installation via the server's file system, knowing about tar
00013           and restoring your data CAREFULLY (AKA "the server admin").
00014 
00015     @copyright: 2005-2008 by MoinMoin:ThomasWaldmann
00016     @license: GNU GPL, see COPYING for details.
00017 """
00018 
00019 import os, re, time
00020 
00021 from MoinMoin import wikiutil
00022 from MoinMoin.support import tarfile
00023 
00024 
00025 def addFiles(path, tar, exclude_func):
00026     """ Add files in path to tar """
00027     for root, dirs, files in os.walk(path):
00028         files.sort() # sorted page revs may compress better
00029         for name in files:
00030             path = os.path.join(root, name)
00031             if exclude_func(path):
00032                 continue
00033             tar.add(path)
00034 
00035 
00036 def sendBackup(request):
00037     """ Send compressed tar file """
00038     dateStamp = time.strftime("%Y-%m-%d--%H-%M-%S-UTC", time.gmtime())
00039     filename = "%s-%s.tar.%s" % (request.cfg.siteid, dateStamp, request.cfg.backup_compression)
00040     request.content_type = 'application/octet-stream'
00041     request.headers.add('Content-Disposition', 'inline; filename="%s"' % filename)
00042     tar = tarfile.open(fileobj=request, mode="w|%s" % request.cfg.backup_compression)
00043     # allow GNU tar's longer file/pathnames
00044     tar.posix = False
00045     for path in request.cfg.backup_include:
00046         addFiles(path, tar, request.cfg.backup_exclude)
00047     tar.close()
00048 
00049 
00050 def sendBackupForm(request, pagename):
00051     _ = request.getText
00052     request.setContentLanguage(request.lang)
00053     title = _('Wiki Backup')
00054     request.theme.send_title(title, pagename=pagename)
00055     request.write(request.formatter.startContent("content"))
00056 
00057     request.write(_("""= Downloading a backup =
00058 
00059 Please note:
00060  * Store backups in a safe and secure place - they contain sensitive information.
00061  * Make sure your wiki configuration backup_* values are correct and complete.
00062  * Make sure the backup file you get contains everything you need in case of problems.
00063  * Make sure it is downloaded without problems.
00064 
00065 To get a backup, just click here:""", wiki=True))
00066 
00067     request.write("""
00068 <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
00069 <input type="hidden" name="action" value="backup">
00070 <input type="hidden" name="do" value="backup">
00071 <input type="submit" value="%(backup_button)s">
00072 </form>
00073 """ % {
00074     'baseurl': request.script_root,
00075     'pagename': wikiutil.quoteWikinameURL(pagename),
00076     'backup_button': _('Backup'),
00077 })
00078 
00079     request.write(request.formatter.endContent())
00080     request.theme.send_footer(pagename)
00081     request.theme.send_closing_html()
00082 
00083 
00084 def sendMsg(request, pagename, msg, msgtype):
00085     from MoinMoin import Page
00086     request.theme.add_msg(msg, msgtype)
00087     return Page.Page(request, pagename).send_page()
00088 
00089 
00090 def backupAllowed(request):
00091     """ Return True if backup is allowed """
00092     action = __name__.split('.')[-1]
00093     user = request.user
00094     return user.valid and user.name in request.cfg.backup_users
00095 
00096 
00097 def execute(pagename, request):
00098     _ = request.getText
00099     if not backupAllowed(request):
00100         return sendMsg(request, pagename,
00101                        msg=_('You are not allowed to do remote backup.'), msgtype="error")
00102 
00103     dowhat = request.form.get('do')
00104     if dowhat == 'backup':
00105         sendBackup(request)
00106     elif dowhat is None:
00107         sendBackupForm(request, pagename)
00108     else:
00109         return sendMsg(request, pagename,
00110                        msg=_('Unknown backup subaction: %s.') % dowhat, msgtype="error")
00111