2
# Copyright (c) 2009 Chris Moyer http://kopertop.blogspot.com/
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-
12
# The above copyright notice and this permission notice shall be included
13
# in all copies or substantial portions of the Software.
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
23
# Tools to dump and recover an SDB domain
25
VERSION = "%prog version 1.0"
30
def choice_input(options, default=None, title=None):
35
title = "Please choose"
38
for n, obj in enumerate(options):
39
print "%s: %s" % (n, obj)
41
choice = int(raw_input(">>> "))
43
choice = objects[choice]
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"
53
def dump_db(domain, file_name):
55
Dump SDB domain to file
57
doc = domain.to_xml(open(file_name, "w"))
61
Remove all entries from domain
66
def load_db(domain, file):
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
72
:param domain: The SDB Domain object to load to
73
:param file: The File to load the DB from
77
def create_db(domain_name, region_name):
80
:param domain: Name of the domain to create
83
sdb = boto.sdb.connect_to_region(region_name)
84
return sdb.create_domain(domain_name)
86
if __name__ == "__main__":
87
from optparse import OptionParser
88
parser = OptionParser(version=VERSION, usage="Usage: %prog [--dump|--load|--empty|--list|-l] [options]")
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="load", default=False, action="store_true")
93
parser.add_option("--empty", help="Empty all contents of domain", dest="empty", default=False, action="store_true")
94
parser.add_option("-l", "--list", help="List All domains", dest="list", default=False, action="store_true")
95
parser.add_option("-c", "--create", help="Create domain", dest="create", default=False, action="store_true")
97
parser.add_option("-a", "--all-domains", help="Operate on all domains", action="store_true", default=False, dest="all_domains")
98
parser.add_option("-d", "--domain", help="Do functions on domain (may be more 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()
104
for domain_name in options.domains:
105
create_db(domain_name, options.region_name)
108
sdb = boto.sdb.connect_to_region(options.region_name)
110
for db in sdb.get_all_domains():
114
if not options.dump and not options.load and not options.empty:
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()
131
domains = [choice_input(options=sdb.get_all_domains(), title="No domain specified, please choose one")]
135
# Execute the commands
139
if confirm("WARNING!!! Are you sure you want to empty the following domains?: %s" % domains):
141
for domain in domains:
142
print "--------> Emptying %s <--------" % domain.name
145
print "Canceling operations"
149
for domain in domains:
150
print "--------> Dumping %s <---------" % domain.name
151
if options.file_name:
152
file_name = options.file_name
154
file_name = "%s.db" % domain.name
155
dump_db(domain, file_name)
158
for domain in domains:
159
print "---------> Loading %s <----------" % domain.name
160
if options.file_name:
161
file_name = options.file_name
163
file_name = "%s.db" % domain.name
164
load_db(domain, open(file_name, "rb"))
167
total_time = round(time.time() - stime, 2)
168
print "--------> Finished in %s <--------" % total_time