2007-03-25 07:55:37 +05:30
|
|
|
#
|
|
|
|
# Gramps - a GTK+/GNOME based genealogy program
|
|
|
|
#
|
2007-06-12 09:59:15 +05:30
|
|
|
# Copyright (C) 2007 Donald N. Allingham
|
2007-03-25 07:55:37 +05:30
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
#
|
|
|
|
|
|
|
|
"""
|
2007-06-12 09:59:15 +05:30
|
|
|
Provides backup and restore functions for a database
|
2007-03-25 07:55:37 +05:30
|
|
|
"""
|
|
|
|
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# load standard python libraries
|
|
|
|
#
|
|
|
|
#-------------------------------------------------------------------------
|
|
|
|
from gettext import gettext as _
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Gramps libs
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
from QuestionDialog import ErrorDialog
|
|
|
|
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
#
|
|
|
|
# Set up logging
|
|
|
|
#
|
|
|
|
#------------------------------------------------------------------------
|
|
|
|
import logging
|
|
|
|
import os
|
2007-06-12 09:59:15 +05:30
|
|
|
from GrampsDb import _GrampsDBDir as GrampsDBDir
|
2007-03-25 07:55:37 +05:30
|
|
|
import cPickle as pickle
|
|
|
|
|
2007-06-12 09:59:15 +05:30
|
|
|
LOG = logging.getLogger(".Backup")
|
2007-03-25 07:55:37 +05:30
|
|
|
|
|
|
|
def export(database):
|
2007-06-12 09:59:15 +05:30
|
|
|
"""
|
|
|
|
Exports the database to a set of backup files. These files consist
|
|
|
|
of the pickled database tables, one file for each table.
|
|
|
|
|
|
|
|
The heavy lifting is done by the private __do__export function. The
|
|
|
|
purpose of this function is to catch any exceptions that occur.
|
|
|
|
|
|
|
|
@param database: database instance to backup
|
|
|
|
@type database: GrampsDbDir
|
|
|
|
"""
|
2007-03-25 07:55:37 +05:30
|
|
|
try:
|
2007-06-12 09:59:15 +05:30
|
|
|
__do_export(database)
|
2007-03-25 07:55:37 +05:30
|
|
|
except (OSError, IOError), msg:
|
2007-06-12 09:59:15 +05:30
|
|
|
ErrorDialog(_("Error saving backup data"), str(msg))
|
|
|
|
|
|
|
|
def __do_export(database):
|
|
|
|
"""
|
|
|
|
Loop through each table of the database, saving the pickled data
|
|
|
|
a file.
|
|
|
|
|
|
|
|
@param database: database instance to backup
|
|
|
|
@type database: GrampsDbDir
|
|
|
|
"""
|
2007-06-14 04:18:28 +05:30
|
|
|
try:
|
|
|
|
for (base, tbl) in __build_tbl_map(database):
|
|
|
|
backup_name = os.path.join(database.get_save_path(), base + ".gbkp.new")
|
|
|
|
backup_table = open(backup_name, 'wb')
|
2007-03-25 07:55:37 +05:30
|
|
|
|
2007-06-14 04:18:28 +05:30
|
|
|
cursor = tbl.cursor()
|
|
|
|
data = cursor.first()
|
|
|
|
while data:
|
|
|
|
pickle.dump(data, backup_table, 2)
|
|
|
|
data = cursor.next()
|
|
|
|
cursor.close()
|
|
|
|
backup_table.close()
|
|
|
|
except (IOError,OSError):
|
|
|
|
return
|
|
|
|
|
|
|
|
for (base, tbl) in __build_tbl_map(database):
|
|
|
|
new_name = os.path.join(database.get_save_path(), base + ".gbkp")
|
|
|
|
old_name = new_name + ".new"
|
|
|
|
os.unlink(new_name)
|
|
|
|
os.rename(old_name, new_name)
|
2007-03-25 07:55:37 +05:30
|
|
|
|
|
|
|
def restore(database):
|
2007-06-12 09:59:15 +05:30
|
|
|
"""
|
|
|
|
Restores the database to a set of backup files. These files consist
|
|
|
|
of the pickled database tables, one file for each table.
|
|
|
|
|
|
|
|
The heavy lifting is done by the private __do__restore function. The
|
|
|
|
purpose of this function is to catch any exceptions that occur.
|
|
|
|
|
|
|
|
@param database: database instance to restore
|
|
|
|
@type database: GrampsDbDir
|
|
|
|
"""
|
2007-03-25 07:55:37 +05:30
|
|
|
try:
|
2007-06-12 09:59:15 +05:30
|
|
|
__do_restore(database)
|
2007-03-25 07:55:37 +05:30
|
|
|
except (OSError, IOError), msg:
|
2007-06-12 09:59:15 +05:30
|
|
|
ErrorDialog(_("Error restoring backup data"), str(msg))
|
|
|
|
|
|
|
|
def __do_restore(database):
|
|
|
|
"""
|
|
|
|
Loop through each table of the database, restoring the pickled data
|
|
|
|
to the appropriate database file.
|
|
|
|
|
|
|
|
@param database: database instance to backup
|
|
|
|
@type database: GrampsDbDir
|
|
|
|
"""
|
|
|
|
for (base, tbl) in __build_tbl_map(database):
|
2007-03-25 08:47:25 +05:30
|
|
|
backup_name = os.path.join(database.get_save_path(), base + ".gbkp")
|
2007-06-12 09:59:15 +05:30
|
|
|
backup_table = open(backup_name, 'rb')
|
2007-03-25 07:55:37 +05:30
|
|
|
|
2007-03-25 08:47:25 +05:30
|
|
|
try:
|
|
|
|
while True:
|
2007-06-12 09:59:15 +05:30
|
|
|
data = pickle.load(backup_table)
|
|
|
|
tbl[data[0]] = data[1]
|
2007-03-25 08:47:25 +05:30
|
|
|
except EOFError:
|
|
|
|
backup_table.close()
|
2007-06-12 09:59:15 +05:30
|
|
|
database.rebuild_secondary()
|
|
|
|
|
|
|
|
def __build_tbl_map(database):
|
|
|
|
"""
|
|
|
|
Builds a table map of names to database tables.
|
2007-03-25 07:55:37 +05:30
|
|
|
|
2007-06-12 09:59:15 +05:30
|
|
|
@param database: database instance to backup
|
|
|
|
@type database: GrampsDbDir
|
|
|
|
"""
|
|
|
|
return [
|
|
|
|
( GrampsDBDir.PERSON_TBL, database.person_map.db),
|
|
|
|
( GrampsDBDir.FAMILY_TBL, database.family_map.db),
|
|
|
|
( GrampsDBDir.PLACES_TBL, database.place_map.db),
|
|
|
|
( GrampsDBDir.SOURCES_TBL, database.source_map.db),
|
|
|
|
( GrampsDBDir.REPO_TBL, database.repository_map.db),
|
|
|
|
( GrampsDBDir.NOTE_TBL, database.note_map.db),
|
|
|
|
( GrampsDBDir.MEDIA_TBL, database.media_map.db),
|
|
|
|
( GrampsDBDir.EVENTS_TBL, database.event_map.db),
|
|
|
|
( GrampsDBDir.META, database.metadata.db),
|
|
|
|
]
|