~ubuntu-branches/ubuntu/lucid/python-apsw/lucid

« back to all changes in this revision

Viewing changes to doc/_sources/backup.txt

  • Committer: Bazaar Package Importer
  • Author(s): Joel Rosdahl
  • Date: 2010-01-11 20:44:37 UTC
  • mfrom: (1.1.6 upstream) (3.1.4 sid)
  • Revision ID: james.westby@ubuntu.com-20100111204437-iao0rcj1p2m29juy
Tags: 3.6.22-r1-1
* New upstream version.
* Added ${misc:Depends} for binary packages' requirements.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
.. Automatically generated by code2rst.py
 
2
   code2rst.py src/backup.c doc/backup.rst
 
3
   Edit src/backup.c not this file!
 
4
 
 
5
.. currentmodule:: apsw
 
6
 
 
7
.. _backup:
 
8
 
 
9
Backup
 
10
******
 
11
 
 
12
A backup object encapsulates copying one database to another.  You
 
13
call :meth:`Connection.backup` on the destination database to get the
 
14
backup object.  Call :meth:`~backup.step` to copy some pages
 
15
repeatedly dealing with errors as appropriate.  Finally
 
16
:meth:`~backup.finish` cleans up committing or rolling back and
 
17
releasing locks.
 
18
 
 
19
Here is an example usage using the **with** statement to ensure
 
20
:meth:`~backup.finish` is called::
 
21
 
 
22
  # copies source.main into db
 
23
  with db.backup("main", source, "main") as b:
 
24
      while not b.done:
 
25
          b.step(100)
 
26
          print b.remaining, b.pagecount, "\r",
 
27
 
 
28
If you are not using **with** then you'll need to ensure
 
29
:meth:`~backup.finish` is called::
 
30
 
 
31
  # copies source.main into db
 
32
  b=db.backup("main", source, "main")
 
33
  try:
 
34
      while not b.done:
 
35
          b.step(100)
 
36
          print b.remaining, b.pagecount, "\r",
 
37
  finally:
 
38
      b.finish()
 
39
 
 
40
Important details
 
41
=================
 
42
 
 
43
The database is copied page by page.  This means that there is not a
 
44
round trip via SQL.  All pages are copied including free ones.
 
45
 
 
46
The destination database is locked during the copy.  You will get a
 
47
:exc:`ThreadingViolationError` if you attempt to use it.
 
48
 
 
49
backup class
 
50
============
 
51
 
 
52
.. class:: backup
 
53
 
 
54
  You create a backup instance by calling :meth:`Connection.backup`.
 
55
 
 
56
.. method:: backup.__enter__() -> self
 
57
 
 
58
  You can use the backup object as a `context manager
 
59
  <http://docs.python.org/reference/datamodel.html#with-statement-context-managers>`_
 
60
  as defined in :pep:`0343`.  The :meth:`~backup.__exit__` method ensures that backup
 
61
  is :meth:`finished <backup.finish>`.
 
62
 
 
63
.. method:: backup.__exit__() -> False
 
64
 
 
65
  Implements context manager in conjunction with :meth:`~backup.__enter__` ensuring
 
66
  that the copy is :meth:`finished <backup.finish>`.
 
67
 
 
68
.. method:: backup.close([force=False])
 
69
 
 
70
  Does the same thing as :meth:`~backup.finish`.  This extra api is
 
71
  provided to give the same api as other APSW objects such as
 
72
  :meth:`Connection.close`, :meth:`blob.close` and
 
73
  :meth:`Cursor.close`.  It is safe to call this method multiple
 
74
  times.
 
75
 
 
76
  :param force: If true then any exceptions are ignored.
 
77
 
 
78
.. attribute:: backup.done
 
79
 
 
80
  A boolean that is True if the copy completed in the last call to :meth:`~backup.step`.
 
81
 
 
82
.. index:: sqlite3_backup_finish
 
83
 
 
84
.. method:: backup.finish()
 
85
 
 
86
  Completes the copy process.  If all pages have been copied then the
 
87
  transaction is committed on the destination database, otherwise it
 
88
  is rolled back.  This method must be called for your backup to take
 
89
  effect.  The backup object will always be finished even if there is
 
90
  an exception.  It is safe to call this method multiple times.
 
91
 
 
92
  Calls: :sqliteapi:`sqlite3_backup_finish <backup_finish>`
 
93
 
 
94
.. index:: sqlite3_backup_pagecount
 
95
 
 
96
.. attribute:: backup.pagecount
 
97
 
 
98
  Read only. How many pages were in the source database after the last
 
99
  step.  If you haven't called :meth:`~backup.step` or the backup
 
100
  object has been :meth:`finished <backup.finish>` then zero is
 
101
  returned.
 
102
 
 
103
  Calls: :sqliteapi:`sqlite3_backup_pagecount <backup_finish>`
 
104
 
 
105
.. index:: sqlite3_backup_remaining
 
106
 
 
107
.. attribute:: backup.remaining
 
108
 
 
109
  Read only. How many pages were remaining to be copied after the last
 
110
  step.  If you haven't called :meth:`~backup.step` or the backup
 
111
  object has been :meth:`finished <backup.finish>` then zero is
 
112
  returned.
 
113
 
 
114
  Calls: :sqliteapi:`sqlite3_backup_remaining <backup_finish>`
 
115
 
 
116
.. index:: sqlite3_backup_step
 
117
 
 
118
.. method:: backup.step([npages=All]) -> bool
 
119
 
 
120
  Copies *npages* pages from the source to destination database.  The source database is locked during the copy so
 
121
  using smaller values allows other access to the source database.  The destination database is always locked until the
 
122
  backup object is :meth:`finished <backup.finish>`.
 
123
 
 
124
  :param npages: How many pages to copy. If the parameter is omitted
 
125
     or negative then all remaining pages are copied. The default page
 
126
     size is 1024 bytes (1kb) which can be changed before database
 
127
     creation using a `pragma
 
128
     <http://www.sqlite.org/pragma.html#modify>`_.
 
129
 
 
130
  This method may throw a :exc:`BusyError` or :exc:`LockedError` if
 
131
  unable to lock the source database.  You can catch those and try
 
132
  again.
 
133
 
 
134
  :returns: True if this copied the last remaining outstanding pages, else false.  This is the same value as :attr:`~backup.done`
 
135
 
 
136
  Calls: :sqliteapi:`sqlite3_backup_step <backup_finish>`
 
137