]> scripts.mit.edu Git - wizard.git/blob - wizard/command/restore.py
Fix bug where php.ini not being rewritten for MediaWiki.
[wizard.git] / wizard / command / restore.py
1 import logging
2 import os
3 import sys
4
5 from wizard import command, deploy, shell, util
6
7 def main(argv, baton):
8     options, args = parse_args(argv, baton)
9     command.chdir_to_production()
10     d = deploy.ProductionCopy(".")
11     d.verify()
12     backups = d.backup_dir
13     if not args:
14         if not os.path.exists(backups):
15             print "No restore points available"
16             return
17         sys.stderr.write("Available backups:\n")
18         count = 0
19         for d in reversed(sorted(os.listdir(backups))):
20             if ".bak" in d:
21                 continue
22             if d.startswith("."):
23                 continue
24             if os.listdir(os.path.join(backups, d)):
25                 print d
26             else:
27                 count += 1
28         if count:
29             logging.warning("Pruned %d empty backups" % count)
30         return
31     backup = args[0]
32     if backup == "top":
33         try:
34             backup = sorted(os.listdir(backups))[-1]
35             logging.warning("Using backup %s" % backup)
36         except IndexError:
37             raise Exception("No restore points available")
38     bits = backup.split('-')
39     date = '-'.join(bits[-3:])
40     version = '-'.join(bits[0:-3])
41     shell.drop_priviledges(".", options.log_file)
42     d = deploy.ProductionCopy(".")
43     d.verify()
44     d.verifyConfigured()
45     tag = "%s-%s" % (d.application.name, version)
46     try:
47         shell.call("git", "rev-parse", tag)
48     except shell.CallError:
49         raise Exception("Tag %s doesn't exist in repository" % tag)
50     shell.call("git", "reset", "-q", "--hard", tag)
51     d.restore(backup, options)
52
53 def parse_args(argv, baton):
54     usage = """usage: %prog restore [ARGS] ID
55
56 Takes a backup from the backups/ directory and does
57 a full restore back to it.  CURRENT DATA IS DESTROYED,
58 so you may want to do a backup before you do a restore.
59
60 You can specify 'top' as the ID in order to restore from
61 the latest backup."""
62     parser = command.WizardOptionParser(usage)
63     options, args = parser.parse_all(argv)
64     if len(args) > 1:
65         parser.error("too many arguments")
66     return options, args
67