| OLD | NEW |
| (Empty) |
| 1 #!/usr/bin/env python | |
| 2 # Copyright (c) 2009 Chris Moyer http://kopertop.blogspot.com/ | |
| 3 # | |
| 4 # Permission is hereby granted, free of charge, to any person obtaining a | |
| 5 # copy of this software and associated documentation files (the | |
| 6 # "Software"), to deal in the Software without restriction, including | |
| 7 # without limitation the rights to use, copy, modify, merge, publish, dis- | |
| 8 # tribute, sublicense, and/or sell copies of the Software, and to permit | |
| 9 # persons to whom the Software is furnished to do so, subject to the fol- | |
| 10 # lowing conditions: | |
| 11 # | |
| 12 # The above copyright notice and this permission notice shall be included | |
| 13 # in all copies or substantial portions of the Software. | |
| 14 # | |
| 15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS | |
| 16 # OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL- | |
| 17 # ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT | |
| 18 # SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, | |
| 19 # WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| 20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS | |
| 21 | |
| 22 # | |
| 23 # Tools to dump and recover an SDB domain | |
| 24 # | |
| 25 VERSION = "%prog version 1.0" | |
| 26 import boto | |
| 27 import time | |
| 28 from boto import sdb | |
| 29 | |
| 30 def choice_input(options, default=None, title=None): | |
| 31 """ | |
| 32 Choice input | |
| 33 """ | |
| 34 if title == None: | |
| 35 title = "Please choose" | |
| 36 print title | |
| 37 objects = [] | |
| 38 for n, obj in enumerate(options): | |
| 39 print "%s: %s" % (n, obj) | |
| 40 objects.append(obj) | |
| 41 choice = int(raw_input(">>> ")) | |
| 42 try: | |
| 43 choice = objects[choice] | |
| 44 except: | |
| 45 choice = default | |
| 46 return choice | |
| 47 | |
| 48 def confirm(message="Are you sure?"): | |
| 49 choice = raw_input("%s [yN] " % message) | |
| 50 return choice and len(choice) > 0 and choice[0].lower() == "y" | |
| 51 | |
| 52 | |
| 53 def dump_db(domain, file_name): | |
| 54 """ | |
| 55 Dump SDB domain to file | |
| 56 """ | |
| 57 doc = domain.to_xml(open(file_name, "w")) | |
| 58 | |
| 59 def empty_db(domain): | |
| 60 """ | |
| 61 Remove all entries from domain | |
| 62 """ | |
| 63 for item in domain: | |
| 64 item.delete() | |
| 65 | |
| 66 def load_db(domain, file): | |
| 67 """ | |
| 68 Load a domain from a file, this doesn't overwrite any existing | |
| 69 data in the file so if you want to do a full recovery and restore | |
| 70 you need to call empty_db before calling this | |
| 71 | |
| 72 :param domain: The SDB Domain object to load to | |
| 73 :param file: The File to load the DB from | |
| 74 """ | |
| 75 domain.from_xml(file) | |
| 76 | |
| 77 def create_db(domain_name, region_name): | |
| 78 """Create a new DB | |
| 79 | |
| 80 :param domain: Name of the domain to create | |
| 81 :type domain: str | |
| 82 """ | |
| 83 sdb = boto.sdb.connect_to_region(region_name) | |
| 84 return sdb.create_domain(domain_name) | |
| 85 | |
| 86 if __name__ == "__main__": | |
| 87 from optparse import OptionParser | |
| 88 parser = OptionParser(version=VERSION, usage="Usage: %prog [--dump|--load|--
empty|--list|-l] [options]") | |
| 89 | |
| 90 # Commands | |
| 91 parser.add_option("--dump", help="Dump domain to file", dest="dump", default
=False, action="store_true") | |
| 92 parser.add_option("--load", help="Load domain contents from file", dest="loa
d", default=False, action="store_true") | |
| 93 parser.add_option("--empty", help="Empty all contents of domain", dest="empt
y", default=False, action="store_true") | |
| 94 parser.add_option("-l", "--list", help="List All domains", dest="list", defa
ult=False, action="store_true") | |
| 95 parser.add_option("-c", "--create", help="Create domain", dest="create", def
ault=False, action="store_true") | |
| 96 | |
| 97 parser.add_option("-a", "--all-domains", help="Operate on all domains", acti
on="store_true", default=False, dest="all_domains") | |
| 98 parser.add_option("-d", "--domain", help="Do functions on domain (may be mor
e then one)", action="append", dest="domains") | |
| 99 parser.add_option("-f", "--file", help="Input/Output file we're operating on
", dest="file_name") | |
| 100 parser.add_option("-r", "--region", help="Region (e.g. us-east-1[default] or
eu-west-1)", default="us-east-1", dest="region_name") | |
| 101 (options, args) = parser.parse_args() | |
| 102 | |
| 103 if options.create: | |
| 104 for domain_name in options.domains: | |
| 105 create_db(domain_name, options.region_name) | |
| 106 exit() | |
| 107 | |
| 108 sdb = boto.sdb.connect_to_region(options.region_name) | |
| 109 if options.list: | |
| 110 for db in sdb.get_all_domains(): | |
| 111 print db | |
| 112 exit() | |
| 113 | |
| 114 if not options.dump and not options.load and not options.empty: | |
| 115 parser.print_help() | |
| 116 exit() | |
| 117 | |
| 118 | |
| 119 | |
| 120 | |
| 121 # | |
| 122 # Setup | |
| 123 # | |
| 124 if options.domains: | |
| 125 domains = [] | |
| 126 for domain_name in options.domains: | |
| 127 domains.append(sdb.get_domain(domain_name)) | |
| 128 elif options.all_domains: | |
| 129 domains = sdb.get_all_domains() | |
| 130 else: | |
| 131 domains = [choice_input(options=sdb.get_all_domains(), title="No domain
specified, please choose one")] | |
| 132 | |
| 133 | |
| 134 # | |
| 135 # Execute the commands | |
| 136 # | |
| 137 stime = time.time() | |
| 138 if options.empty: | |
| 139 if confirm("WARNING!!! Are you sure you want to empty the following doma
ins?: %s" % domains): | |
| 140 stime = time.time() | |
| 141 for domain in domains: | |
| 142 print "--------> Emptying %s <--------" % domain.name | |
| 143 empty_db(domain) | |
| 144 else: | |
| 145 print "Canceling operations" | |
| 146 exit() | |
| 147 | |
| 148 if options.dump: | |
| 149 for domain in domains: | |
| 150 print "--------> Dumping %s <---------" % domain.name | |
| 151 if options.file_name: | |
| 152 file_name = options.file_name | |
| 153 else: | |
| 154 file_name = "%s.db" % domain.name | |
| 155 dump_db(domain, file_name) | |
| 156 | |
| 157 if options.load: | |
| 158 for domain in domains: | |
| 159 print "---------> Loading %s <----------" % domain.name | |
| 160 if options.file_name: | |
| 161 file_name = options.file_name | |
| 162 else: | |
| 163 file_name = "%s.db" % domain.name | |
| 164 load_db(domain, open(file_name, "rb")) | |
| 165 | |
| 166 | |
| 167 total_time = round(time.time() - stime, 2) | |
| 168 print "--------> Finished in %s <--------" % total_time | |
| OLD | NEW |