当前位置: 首页>>代码示例>>Python>>正文


Python DatabaseManager.backup方法代码示例

本文整理汇总了Python中trac.db.DatabaseManager.backup方法的典型用法代码示例。如果您正苦于以下问题:Python DatabaseManager.backup方法的具体用法?Python DatabaseManager.backup怎么用?Python DatabaseManager.backup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在trac.db.DatabaseManager的用法示例。


在下文中一共展示了DatabaseManager.backup方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: ProjectBackup

# 需要导入模块: from trac.db import DatabaseManager [as 别名]
# 或者: from trac.db.DatabaseManager import backup [as 别名]
class ProjectBackup(object):
    """
    Class models the project backups (snapshots), doable by the project admin.
    Example usage:

    >>> prj = Project()
    >>> pb = ProjectBackup(prj)
    >>> pb.backup()

    Properties (set automatically on __init__):
        - mysql_path: path to mysql client (default: expect it to found from ``$PATH``)
        - project: reference to project to backup/restore
        - env: environment object based on given project info
        - dm: database manager setup against the environment

    Configuration keys:
        Class uses following configuration keys (placed either in ``project.ini`` or
        to project specific ``conf/trac.ini`` -file) to adjust the functionality:

    """
    def __init__(self, project):
        """
        Initiates the class.

        :param project: Instance of the :class:`Project` to backup/restore
        """
        self.project = project
        self.env = open_environment(self.project.trac_fs_path, use_cache=True)
        backup_dir = self.env.config.get('trac', 'backup_dir', '/tmp')
        self.backup_path_tmpl =  backup_dir + '/project-%s.snapshot-%d.sql'
        self.mysql_path = self.env.config.get('trac', 'mysql_path', 'mysql')

        self.dm = DatabaseManager(self.env)

    def backup(self, user_id, description=None):
        """
        Creates a database backup of the trac instance.

        .. IMPORTANT:: Only the **database** is backed up, while attachments are left as is.

        :param user_id: Id the user who did the restore
        :param description: Optional description about the backup, why it was done or current state...

        Returns:
            True if all went well, otherwise TracError is raised.

        """
        assert isinstance(user_id, long), 'User id needs to be long int'
        description = description if description else ''
        dump_path = None

        # Create database entry about the back
        with admin_transaction() as cursor:
            cursor.execute(("INSERT INTO project_backup (project_key, created_by, description)"
                            "VALUES (%s, %s, %s)"), (self.project.id, user_id, description))

            # Now, take the last inserted id and use it to generate unique dump path
            dump_path = self.backup_path_tmpl % (self.project.env_name, cursor.lastrowid)

            # Use Trac's database manager to dump the database into filesystem
            try:
                self.dm.backup(dump_path)

            except OSError, err:
                self.env.log.exception(err)
                raise TracError('Failed to dump database: %s' % err)

        return True
开发者ID:alvabai,项目名称:trac-multiproject,代码行数:70,代码来源:backup.py


注:本文中的trac.db.DatabaseManager.backup方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。