def restore(config): # pragma: no cover
db_connect = config.conf['db_connect']
if not db_connect:
sys.exit("Error: database connection string not "
"found in any of the configuration files")
try:
engine = create_engine(db_connect)
except exc.NoSuchModuleError as e:
sys.exit("Error: %s" % str(e))
if database_exists(engine.url):
sys.exit("Error: database already exists, will not overwrite!")
try:
create_database(engine.url)
except exc.OperationalError as e:
sys.exit("Error: %s" % str(e))
models.Base.metadata.create_all(engine)
conn = engine.connect()
print("Restoring database from backup file: opp.db.tz")
code, out, err = utils.execute("tar zxf opp.db.tz", propagate=False)
if code != 0:
sys.exit("Error extracting database archive file: %s" % err)
for table in models.Base.metadata.sorted_tables:
with open("%s.pickle" % table, "rb") as f:
while True:
try:
ins = table.insert().values(load(f))
conn.execute(ins)
except EOFError:
break
files = ["%s.pickle" % x.name for x in models.Base.metadata.sorted_tables]
code, out, err = utils.execute("rm %s" % " ".join(files))
if code != 0:
print("Unable to remove .pickle files: %s" % err)
评论列表
文章目录