]> git.rkrishnan.org Git - tahoe-lafs/tahoe-lafs.git/blob - docs/backupdb.rst
mutable/layout.py: make unpack_sdmf_checkstring and unpack_mdmf_checkstring more...
[tahoe-lafs/tahoe-lafs.git] / docs / backupdb.rst
1 ==================
2 The Tahoe BackupDB
3 ==================
4
5 1.  `Overview`_
6 2.  `Schema`_
7 3.  `Upload Operation`_
8 4.  `Directory Operations`_
9
10 Overview
11 ========
12 To speed up backup operations, Tahoe maintains a small database known as the
13 "backupdb". This is used to avoid re-uploading files which have already been
14 uploaded recently.
15
16 This database lives in ``~/.tahoe/private/backupdb.sqlite``, and is a SQLite
17 single-file database. It is used by the "``tahoe backup``" command. In the
18 future, it may optionally be used by other commands such as "``tahoe cp``".
19
20 The purpose of this database is twofold: to manage the file-to-cap
21 translation (the "upload" step) and the directory-to-cap translation (the
22 "mkdir-immutable" step).
23
24 The overall goal of optimizing backup is to reduce the work required when the
25 source disk has not changed (much) since the last backup. In the ideal case,
26 running "``tahoe backup``" twice in a row, with no intervening changes to the
27 disk, will not require any network traffic. Minimal changes to the source
28 disk should result in minimal traffic.
29
30 This database is optional. If it is deleted, the worst effect is that a
31 subsequent backup operation may use more effort (network bandwidth, CPU
32 cycles, and disk IO) than it would have without the backupdb.
33
34 The database uses sqlite3, which is included as part of the standard Python
35 library with Python 2.5 and later. For Python 2.4, Tahoe will try to install the
36 "pysqlite" package at build-time, but this will succeed only if sqlite3 with
37 development headers is already installed.  On Debian and Debian derivatives
38 you can install the "python-pysqlite2" package (which, despite the name,
39 actually provides sqlite3 rather than sqlite2). On old distributions such
40 as Debian etch (4.0 "oldstable") or Ubuntu Edgy (6.10) the "python-pysqlite2"
41 package won't work, but the "sqlite3-dev" package will.
42
43 Schema
44 ======
45
46 The database contains the following tables::
47
48   CREATE TABLE version
49   (
50    version integer  # contains one row, set to 1
51   );
52   
53   CREATE TABLE local_files
54   (
55    path  varchar(1024),  PRIMARY KEY -- index, this is an absolute UTF-8-encoded local filename
56    size  integer,         -- os.stat(fn)[stat.ST_SIZE]
57    mtime number,          -- os.stat(fn)[stat.ST_MTIME]
58    ctime number,          -- os.stat(fn)[stat.ST_CTIME]
59    fileid integer
60   );
61   
62   CREATE TABLE caps
63   (
64    fileid integer PRIMARY KEY AUTOINCREMENT,
65    filecap varchar(256) UNIQUE    -- URI:CHK:...
66   );
67   
68   CREATE TABLE last_upload
69   (
70    fileid INTEGER PRIMARY KEY,
71    last_uploaded TIMESTAMP,
72    last_checked TIMESTAMP
73   );
74   
75   CREATE TABLE directories
76   (
77    dirhash varchar(256) PRIMARY KEY,
78    dircap varchar(256),
79    last_uploaded TIMESTAMP,
80    last_checked TIMESTAMP
81   );
82
83 Upload Operation
84 ================
85
86 The upload process starts with a pathname (like ``~/.emacs``) and wants to end up
87 with a file-cap (like ``URI:CHK:...``).
88
89 The first step is to convert the path to an absolute form
90 (``/home/warner/.emacs``) and do a lookup in the local_files table. If the path
91 is not present in this table, the file must be uploaded. The upload process
92 is:
93
94 1. record the file's size, ctime (which is the directory-entry change time or
95    file creation time depending on OS) and modification time
96
97 2. upload the file into the grid, obtaining an immutable file read-cap
98
99 3. add an entry to the 'caps' table, with the read-cap, to get a fileid
100
101 4. add an entry to the 'last_upload' table, with the current time
102
103 5. add an entry to the 'local_files' table, with the fileid, the path,
104    and the local file's size/ctime/mtime
105
106 If the path *is* present in 'local_files', the easy-to-compute identifying
107 information is compared: file size and ctime/mtime. If these differ, the file
108 must be uploaded. The row is removed from the local_files table, and the
109 upload process above is followed.
110
111 If the path is present but ctime or mtime differs, the file may have changed.
112 If the size differs, then the file has certainly changed. At this point, a
113 future version of the "backup" command might hash the file and look for a
114 match in an as-yet-defined table, in the hopes that the file has simply been
115 moved from somewhere else on the disk. This enhancement requires changes to
116 the Tahoe upload API before it can be significantly more efficient than
117 simply handing the file to Tahoe and relying upon the normal convergence to
118 notice the similarity.
119
120 If ctime, mtime, or size is different, the client will upload the file, as
121 above.
122
123 If these identifiers are the same, the client will assume that the file is
124 unchanged (unless the ``--ignore-timestamps`` option is provided, in which
125 case the client always re-uploads the file), and it may be allowed to skip
126 the upload. For safety, however, we require the client periodically perform a
127 filecheck on these probably-already-uploaded files, and re-upload anything
128 that doesn't look healthy. The client looks the fileid up in the
129 'last_checked' table, to see how long it has been since the file was last
130 checked.
131
132 A "random early check" algorithm should be used, in which a check is
133 performed with a probability that increases with the age of the previous
134 results. E.g. files that were last checked within a month are not checked,
135 files that were checked 5 weeks ago are re-checked with 25% probability, 6
136 weeks with 50%, more than 8 weeks are always checked. This reduces the
137 "thundering herd" of filechecks-on-everything that would otherwise result
138 when a backup operation is run one month after the original backup. If a
139 filecheck reveals the file is not healthy, it is re-uploaded.
140
141 If the filecheck shows the file is healthy, or if the filecheck was skipped,
142 the client gets to skip the upload, and uses the previous filecap (from the
143 'caps' table) to add to the parent directory.
144
145 If a new file is uploaded, a new entry is put in the 'caps' and 'last_upload'
146 table, and an entry is made in the 'local_files' table to reflect the mapping
147 from local disk pathname to uploaded filecap. If an old file is re-uploaded,
148 the 'last_upload' entry is updated with the new timestamps. If an old file is
149 checked and found healthy, the 'last_upload' entry is updated.
150
151 Relying upon timestamps is a compromise between efficiency and safety: a file
152 which is modified without changing the timestamp or size will be treated as
153 unmodified, and the "``tahoe backup``" command will not copy the new contents
154 into the grid. The ``--no-timestamps`` option can be used to disable this
155 optimization, forcing every byte of the file to be hashed and encoded.
156
157 Directory Operations
158 ====================
159
160 Once the contents of a directory are known (a filecap for each file, and a
161 dircap for each directory), the backup process must find or create a tahoe
162 directory node with the same contents. The contents are hashed, and the hash
163 is queried in the 'directories' table. If found, the last-checked timestamp
164 is used to perform the same random-early-check algorithm described for files
165 above, but no new upload is performed. Since "``tahoe backup``" creates immutable
166 directories, it is perfectly safe to re-use a directory from a previous
167 backup.
168
169 If not found, the web-API "mkdir-immutable" operation is used to create a new
170 directory, and an entry is stored in the table.
171
172 The comparison operation ignores timestamps and metadata, and pays attention
173 solely to the file names and contents.
174
175 By using a directory-contents hash, the "``tahoe backup``" command is able to
176 re-use directories from other places in the backed up data, or from old
177 backups. This means that renaming a directory and moving a subdirectory to a
178 new parent both count as "minor changes" and will result in minimal Tahoe
179 operations and subsequent network traffic (new directories will be created
180 for the modified directory and all of its ancestors). It also means that you
181 can perform a backup ("#1"), delete a file or directory, perform a backup
182 ("#2"), restore it, and then the next backup ("#3") will re-use the
183 directories from backup #1.
184
185 The best case is a null backup, in which nothing has changed. This will
186 result in minimal network bandwidth: one directory read and two modifies. The
187 ``Archives/`` directory must be read to locate the latest backup, and must be
188 modified to add a new snapshot, and the ``Latest/`` directory will be updated to
189 point to that same snapshot.
190