annotate MoinMoin/action/backup.py @ 4424:5ad5753ae311

pre-1.9: request.form has qs args and post data, 1.9: .form only post data, .args only qs args, .values both
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 16 Nov 2008 22:20:21 +0100
parents e88baf535b48
children 41f1c2922ae7
rev   line source
4054
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
1 # -*- coding: iso-8859-1 -*-
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
2 """
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
3 MoinMoin - download a backup via http.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
4
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 Triggering backup action will check if you are authorized to do a backup
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 and if yes, just send a <siteid>-<date>--<time>.tar.<format> to you.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
7 What exactly is contained in your backup depends on your wiki's
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8 configuration - please make sure you have everything you need BEFORE you
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
9 really need it.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 Note: there is no restore support, you need somebody having access to your
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12 wiki installation via the server's file system, knowing about tar
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 and restoring your data CAREFULLY (AKA "the server admin").
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
14
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15 @copyright: 2005-2008 by MoinMoin:ThomasWaldmann
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 @license: GNU GPL, see COPYING for details.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
17 """
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
19 import os, re, time
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
20
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
21 from MoinMoin import wikiutil
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
22 from MoinMoin.support import tarfile
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
23
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
24
4109
e88baf535b48 fix backup action configuration (broke on windows due to backslashes in e.g. cache_dir), try 2.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4108
diff changeset
25 def addFiles(path, tar, exclude_func):
4054
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
26 """ Add files in path to tar """
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
27 for root, dirs, files in os.walk(path):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
28 files.sort() # sorted page revs may compress better
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
29 for name in files:
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
30 path = os.path.join(root, name)
4109
e88baf535b48 fix backup action configuration (broke on windows due to backslashes in e.g. cache_dir), try 2.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4108
diff changeset
31 if exclude_func(path):
4054
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
32 continue
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
33 tar.add(path)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
34
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
35
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
36 def sendBackup(request):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
37 """ Send compressed tar file """
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
38 dateStamp = time.strftime("%Y-%m-%d--%H-%M-%S-UTC", time.gmtime())
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
39 filename = "%s-%s.tar.%s" % (request.cfg.siteid, dateStamp, request.cfg.backup_compression)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
40 request.emit_http_headers([
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
41 'Content-Type: application/octet-stream',
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
42 'Content-Disposition: inline; filename="%s"' % filename, ])
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
43
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
44 tar = tarfile.open(fileobj=request, mode="w|%s" % request.cfg.backup_compression)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
45 # allow GNU tar's longer file/pathnames
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
46 tar.posix = False
4108
1cd493d5e113 fix backup action configuration (broke on windows due to backslashes in e.g. cache_dir)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4054
diff changeset
47 for path in request.cfg.backup_include:
4109
e88baf535b48 fix backup action configuration (broke on windows due to backslashes in e.g. cache_dir), try 2.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4108
diff changeset
48 addFiles(path, tar, request.cfg.backup_exclude)
4054
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
49 tar.close()
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
50
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
51
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
52 def sendBackupForm(request, pagename):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
53 _ = request.getText
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
54 request.emit_http_headers()
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
55 request.setContentLanguage(request.lang)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
56 title = _('Wiki Backup')
4424
5ad5753ae311 pre-1.9: request.form has qs args and post data, 1.9: .form only post data, .args only qs args, .values both
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4109
diff changeset
57 request.theme.send_title(title, pagename=pagename)
4054
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
58 request.write(request.formatter.startContent("content"))
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
59
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
60 request.write(_("""= Downloading a backup =
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
61
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
62 Please note:
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
63 * Store backups in a safe and secure place - they contain sensitive information.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
64 * Make sure your wiki configuration backup_* values are correct and complete.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
65 * Make sure the backup file you get contains everything you need in case of problems.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
66 * Make sure it is downloaded without problems.
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
67
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
68 To get a backup, just click here:""", wiki=True))
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
69
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
70 request.write("""
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
71 <form action="%(baseurl)s/%(pagename)s" method="POST" enctype="multipart/form-data">
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
72 <input type="hidden" name="action" value="backup">
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
73 <input type="hidden" name="do" value="backup">
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
74 <input type="submit" value="%(backup_button)s">
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
75 </form>
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
76 """ % {
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
77 'baseurl': request.getScriptname(),
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
78 'pagename': wikiutil.quoteWikinameURL(pagename),
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
79 'backup_button': _('Backup'),
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
80 })
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
81
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
82 request.write(request.formatter.endContent())
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
83 request.theme.send_footer(pagename)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
84 request.theme.send_closing_html()
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
85
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
86
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
87 def sendMsg(request, pagename, msg, msgtype):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
88 from MoinMoin import Page
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
89 request.theme.add_msg(msg, msgtype)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
90 return Page.Page(request, pagename).send_page()
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
91
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
92
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
93 def backupAllowed(request):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
94 """ Return True if backup is allowed """
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
95 action = __name__.split('.')[-1]
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
96 user = request.user
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
97 return user.valid and user.name in request.cfg.backup_users
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
98
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
99
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
100 def execute(pagename, request):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
101 _ = request.getText
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
102 if not backupAllowed(request):
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
103 return sendMsg(request, pagename,
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
104 msg=_('You are not allowed to do remote backup.'), msgtype="error")
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
105
4424
5ad5753ae311 pre-1.9: request.form has qs args and post data, 1.9: .form only post data, .args only qs args, .values both
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 4109
diff changeset
106 dowhat = request.form.get('do')
4054
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
107 if dowhat == 'backup':
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
108 sendBackup(request)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
109 elif dowhat is None:
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
110 sendBackupForm(request, pagename)
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
111 else:
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
112 return sendMsg(request, pagename,
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
113 msg=_('Unknown backup subaction: %s.') % dowhat, msgtype="error")
691e12f53408 backup action: add missing file
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
114