使用Python进行Postgresql数据库备份

时间:2014-05-19 08:20:15

标签: python postgresql

我想使用Python代码备份数据库。我想备份一些相关数据表。如何备份以及如何使用" SELECT"选择所需的表格声明?

e.g。

我想从2014-05-01到2014-05-10获取某些表的数据并将此结果输出为.sql扩展名文件

如何使用python代码获取此格式? 如果你不介意,请解释。 感谢。

3 个答案:

答案 0 :(得分:3)

使用psycopg2建立数据连接。文档中有很多例子:

http://initd.org/psycopg/

配置完数据源后,请仔细检查" SELECT"的结果。声明建立" INSERT INTO"语句通过将结果集打印到文件。基本上是一些反向逻辑。

这样,如果时机到了,您需要使用备份文件,那么您可以简单地运行将数据插入的SQL文件...

示例:

        import psycopg2
        import sys


        con = None

        try:

            con = psycopg2.connect(database='local', user='local', password='local',port='1970')
            cur = con.cursor()
            cur.execute('SELECT x FROM t')
            f = open('test.sql', 'w')
            for row in cur:
              f.write("insert into t values (" + str(row) + ");")
        except psycopg2.DatabaseError, e:
            print 'Error %s' % e
            sys.exit(1)
        finally:
            if con:
                con.close()

然后恢复:

psql <dbname> <username> < test.sql

干杯,

答案 1 :(得分:1)

我想到的第一个想法是转储你的表调用pg_dump命令,类似于提出的方法here(但谷歌有很多选择)。

但是,由于您的备份策略要求您选择精确的日期而不仅仅是表格,因此您可能不得不依赖一系列查询,然后我的建议是使用像Psycopg这样的库。

编辑

我不能提供一个完整的例子,因为我不知道:

  • 您要转储哪些表
  • 每个表的精确备份策略是什么(即SELECT语句)
  • 你想如何恢复它们。通过删除表然后重新创建它,通过基于ID属性覆盖数据库行,...

以下示例生成一个存储单个查询结果的文件。

import psycopg

conn = psycopg2.connect("dbname=test user=postgres")  # change this according to your RDBMS configuration
cursor = conn.cursor()

table_name='YOUR_TABLE_HERE'  # place your table name here
with open("table_dump.sql") as f:
    cursor.execute("SELECT * FROM %s" % (table_name))  # change the query according to your needs
    column_names = []
    columns_descr = cursor.description
    for c in columns_descr:
        column_names.append(c[0])
    insert_prefix = 'INSERT INTO %s (%s) VALUES ' % (table_name, ', '.join(column_names))
    rows = cursor.fetchall()
    for row in rows:
    row_data = []
        for rd in row:
            if rd is None:
                row_data.append('NULL')
            elif isinstance(rd, datetime.datetime):
                row_data.append("'%s'" % (rd.strftime('%Y-%m-%d %H:%M:%S') ))
            else:
                row_data.append(repr(rd))
    f.write('%s (%s);\n' % (insert_prefix, ', '.join(row_data)))  # this is the text that will be put in the SQL file. You can change it if you wish.

答案 2 :(得分:1)

如果您的操作系统是Linux,则可以使用以下代码。 首先,您应该运行apt-get install postgresql

def create_essentials():
yaml_file = open("settings.yaml", 'r')
settings = yaml.load(yaml_file)
db_name = settings["db_name"]
db_user = settings["db_user"]
db_password = settings["db_password"]
db_host = settings["db_host"]
db_port = settings["db_port"]
backup_path = settings["backup_path"]
filename = settings["filename"]
filename = filename + "-" + time.strftime("%Y%m%d") + ".backup"
command_str = str(db_host)+" -p "+str(db_port)+" -d "+db_name+" -U "+db_user
return command_str, backup_path, filename


def backup_database(table_names=None):
command_str,backup_path,filename = create_essentials()
command_str = "pg_dump -h "+command_str

if table_names is not None:
    for x in table_names:
        command_str = command_str +" -t "+x

command_str = command_str + " -F c -b -v -f '"+backup_path+"/"+filename+"'"
try:
    os.system(command_str)
    print "Backup completed"
except Exception as e:
    print "!!Problem occured!!"
    print e

def restore_database(table_names=None):
command_str,backup_path,filename = create_essentials()
command_str = "pg_restore -h "+command_str

if table_names is not None:
    for x in table_names:
        command_str = command_str +" -t "+x

command_str = command_str + " -v '"+backup_path+"/"+filename+"'"
try:
    os.system(command_str)
    print "Restore completed"
except Exception as e:
    print "!!Problem occured!!"
    print e