简体   繁体   English

如何从SqlAlchemy创建和恢复备份?

[英]How to create and restore a backup from SqlAlchemy?

I'm writing a Pylons app, and am trying to create a simple backup system where every table is serialized and tarred up into a single file for an administrator to download, and use to restore the app should something bad happen. 我正在编写一个Pylons应用程序,并且正在尝试创建一个简单的备份系统,其中每个表都被序列化并且被压缩成单个文件供管理员下载,并且如果发生了不好的事情,则用于恢复应用程序。

I can serialize my table data just fine using the SqlAlchemy serializer , and I can deserialize it fine as well, but I can't figure out how to commit those changes back to the database. 我可以使用SqlAlchemy序列化程序很好地序列化我的表数据,我也可以很好地反序列化它,但我无法弄清楚如何将这些更改提交回数据库。

In order to serialize my data I am doing this: 为了序列化我的数据,我这样做:

from myproject.model.meta import Session
from sqlalchemy.ext.serializer import loads, dumps
q = Session.query(MyTable)
serialized_data = dumps(q.all())

In order to test things out, I go ahead and truncation MyTable , and then attempt to restore using serialized_data : 为了测试一下,我继续截断MyTable ,然后尝试使用serialized_data进行恢复:

from myproject.model import meta
restore_q = loads(serialized_data, meta.metadata, Session)

This doesn't seem to do anything... I've tried calling a Session.commit after the fact, individually walking through all the objects in restore_q and adding them, but nothing seems to work. 这似乎没有做任何事情......事实上,我已经尝试调用Session.commit ,单独遍历restore_q所有对象并添加它们,但似乎没有任何效果。

What am I missing? 我错过了什么? Or is there a better way to do what I'm aiming for? 或者有更好的方法来实现我的目标吗? I don't want to shell out and directly touch the database, since SqlAlchemy supports different database engines. 我不想出来并直接触摸数据库,因为SqlAlchemy支持不同的数据库引擎。

您必须使用Session.merge()方法而不是Session.add()将反序列化的对象放回会话中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM