]> git.proxmox.com Git - ceph.git/blob - ceph/doc/rbd/rbd-snapshot.rst
5aaa23e5d19ddd4390e37ce4b26d9897521b99d6
[ceph.git] / ceph / doc / rbd / rbd-snapshot.rst
1 ===========
2 Snapshots
3 ===========
4
5 .. index:: Ceph Block Device; snapshots
6
7 A snapshot is a read-only copy of the state of an image at a particular point in
8 time. One of the advanced features of Ceph block devices is that you can create
9 snapshots of the images to retain a history of an image's state. Ceph also
10 supports snapshot layering, which allows you to clone images (e.g., a VM image)
11 quickly and easily. Ceph supports block device snapshots using the ``rbd``
12 command and many higher level interfaces, including `QEMU`_, `libvirt`_,
13 `OpenStack`_ and `CloudStack`_.
14
15 .. important:: To use use RBD snapshots, you must have a running Ceph cluster.
16
17 .. note:: Because RBD does not know about the file system, snapshots are
18 `crash-consistent` if they are not coordinated with the mounting
19 computer. So, we recommend you stop `I/O` before taking a snapshot of
20 an image. If the image contains a file system, the file system must be
21 in a consistent state before taking a snapshot or you may have to run
22 `fsck`. To stop `I/O` you can use `fsfreeze` command. See
23 `fsfreeze(8)` man page for more details.
24 For virtual machines, `qemu-guest-agent` can be used to automatically
25 freeze file systems when creating a snapshot.
26
27 .. ditaa:: +------------+ +-------------+
28 | {s} | | {s} c999 |
29 | Active |<-------*| Snapshot |
30 | Image | | of Image |
31 | (stop i/o) | | (read only) |
32 +------------+ +-------------+
33
34
35 Cephx Notes
36 ===========
37
38 When `cephx`_ is enabled (it is by default), you must specify a user name or ID
39 and a path to the keyring containing the corresponding key for the user. See
40 :ref:`User Management <user-management>` for details. You may also add the ``CEPH_ARGS`` environment
41 variable to avoid re-entry of the following parameters. ::
42
43 rbd --id {user-ID} --keyring=/path/to/secret [commands]
44 rbd --name {username} --keyring=/path/to/secret [commands]
45
46 For example::
47
48 rbd --id admin --keyring=/etc/ceph/ceph.keyring [commands]
49 rbd --name client.admin --keyring=/etc/ceph/ceph.keyring [commands]
50
51 .. tip:: Add the user and secret to the ``CEPH_ARGS`` environment
52 variable so that you don't need to enter them each time.
53
54
55 Snapshot Basics
56 ===============
57
58 The following procedures demonstrate how to create, list, and remove
59 snapshots using the ``rbd`` command on the command line.
60
61 Create Snapshot
62 ---------------
63
64 To create a snapshot with ``rbd``, specify the ``snap create`` option, the pool
65 name and the image name. ::
66
67 rbd snap create {pool-name}/{image-name}@{snap-name}
68
69 For example::
70
71 rbd snap create rbd/foo@snapname
72
73
74 List Snapshots
75 --------------
76
77 To list snapshots of an image, specify the pool name and the image name. ::
78
79 rbd snap ls {pool-name}/{image-name}
80
81 For example::
82
83 rbd snap ls rbd/foo
84
85
86 Rollback Snapshot
87 -----------------
88
89 To rollback to a snapshot with ``rbd``, specify the ``snap rollback`` option, the
90 pool name, the image name and the snap name. ::
91
92 rbd snap rollback {pool-name}/{image-name}@{snap-name}
93
94 For example::
95
96 rbd snap rollback rbd/foo@snapname
97
98
99 .. note:: Rolling back an image to a snapshot means overwriting
100 the current version of the image with data from a snapshot. The
101 time it takes to execute a rollback increases with the size of the
102 image. It is **faster to clone** from a snapshot **than to rollback**
103 an image to a snapshot, and it is the preferred method of returning
104 to a pre-existing state.
105
106
107 Delete a Snapshot
108 -----------------
109
110 To delete a snapshot with ``rbd``, specify the ``snap rm`` option, the pool
111 name, the image name and the snap name. ::
112
113 rbd snap rm {pool-name}/{image-name}@{snap-name}
114
115 For example::
116
117 rbd snap rm rbd/foo@snapname
118
119
120 .. note:: Ceph OSDs delete data asynchronously, so deleting a snapshot
121 doesn't free up the disk space immediately.
122
123 Purge Snapshots
124 ---------------
125
126 To delete all snapshots for an image with ``rbd``, specify the ``snap purge``
127 option and the image name. ::
128
129 rbd snap purge {pool-name}/{image-name}
130
131 For example::
132
133 rbd snap purge rbd/foo
134
135
136 .. index:: Ceph Block Device; snapshot layering
137
138 Layering
139 ========
140
141 Ceph supports the ability to create many copy-on-write (COW) clones of a block
142 device snapshot. Snapshot layering enables Ceph block device clients to create
143 images very quickly. For example, you might create a block device image with a
144 Linux VM written to it; then, snapshot the image, protect the snapshot, and
145 create as many copy-on-write clones as you like. A snapshot is read-only,
146 so cloning a snapshot simplifies semantics--making it possible to create
147 clones rapidly.
148
149
150 .. ditaa:: +-------------+ +-------------+
151 | {s} c999 | | {s} |
152 | Snapshot | Child refers | COW Clone |
153 | of Image |<------------*| of Snapshot |
154 | | to Parent | |
155 | (read only) | | (writable) |
156 +-------------+ +-------------+
157
158 Parent Child
159
160 .. note:: The terms "parent" and "child" mean a Ceph block device snapshot (parent),
161 and the corresponding image cloned from the snapshot (child). These terms are
162 important for the command line usage below.
163
164 Each cloned image (child) stores a reference to its parent image, which enables
165 the cloned image to open the parent snapshot and read it.
166
167 A COW clone of a snapshot behaves exactly like any other Ceph block device
168 image. You can read to, write from, clone, and resize cloned images. There are
169 no special restrictions with cloned images. However, the copy-on-write clone of
170 a snapshot refers to the snapshot, so you **MUST** protect the snapshot before
171 you clone it. The following diagram depicts the process.
172
173 .. note:: Ceph only supports cloning for format 2 images (i.e., created with
174 ``rbd create --image-format 2``). The kernel client supports cloned images
175 since kernel 3.10.
176
177 Getting Started with Layering
178 -----------------------------
179
180 Ceph block device layering is a simple process. You must have an image. You must
181 create a snapshot of the image. You must protect the snapshot. Once you have
182 performed these steps, you can begin cloning the snapshot.
183
184 .. ditaa:: +----------------------------+ +-----------------------------+
185 | | | |
186 | Create Block Device Image |------->| Create a Snapshot |
187 | | | |
188 +----------------------------+ +-----------------------------+
189 |
190 +--------------------------------------+
191 |
192 v
193 +----------------------------+ +-----------------------------+
194 | | | |
195 | Protect the Snapshot |------->| Clone the Snapshot |
196 | | | |
197 +----------------------------+ +-----------------------------+
198
199
200 The cloned image has a reference to the parent snapshot, and includes the pool
201 ID, image ID and snapshot ID. The inclusion of the pool ID means that you may
202 clone snapshots from one pool to images in another pool.
203
204
205 #. **Image Template:** A common use case for block device layering is to create a
206 master image and a snapshot that serves as a template for clones. For example,
207 a user may create an image for a Linux distribution (e.g., Ubuntu 12.04), and
208 create a snapshot for it. Periodically, the user may update the image and create
209 a new snapshot (e.g., ``sudo apt-get update``, ``sudo apt-get upgrade``,
210 ``sudo apt-get dist-upgrade`` followed by ``rbd snap create``). As the image
211 matures, the user can clone any one of the snapshots.
212
213 #. **Extended Template:** A more advanced use case includes extending a template
214 image that provides more information than a base image. For example, a user may
215 clone an image (e.g., a VM template) and install other software (e.g., a database,
216 a content management system, an analytics system, etc.) and then snapshot the
217 extended image, which itself may be updated just like the base image.
218
219 #. **Template Pool:** One way to use block device layering is to create a
220 pool that contains master images that act as templates, and snapshots of those
221 templates. You may then extend read-only privileges to users so that they
222 may clone the snapshots without the ability to write or execute within the pool.
223
224 #. **Image Migration/Recovery:** One way to use block device layering is to migrate
225 or recover data from one pool into another pool.
226
227 Protecting a Snapshot
228 ---------------------
229
230 Clones access the parent snapshots. All clones would break if a user inadvertently
231 deleted the parent snapshot. To prevent data loss, you **MUST** protect the
232 snapshot before you can clone it. ::
233
234 rbd snap protect {pool-name}/{image-name}@{snapshot-name}
235
236 For example::
237
238 rbd snap protect rbd/my-image@my-snapshot
239
240 .. note:: You cannot delete a protected snapshot.
241
242 Cloning a Snapshot
243 ------------------
244
245 To clone a snapshot, specify you need to specify the parent pool, image and
246 snapshot; and, the child pool and image name. You must protect the snapshot
247 before you can clone it. ::
248
249 rbd clone {pool-name}/{parent-image}@{snap-name} {pool-name}/{child-image-name}
250
251 For example::
252
253 rbd clone rbd/my-image@my-snapshot rbd/new-image
254
255 .. note:: You may clone a snapshot from one pool to an image in another pool. For example,
256 you may maintain read-only images and snapshots as templates in one pool, and writeable
257 clones in another pool.
258
259 Unprotecting a Snapshot
260 -----------------------
261
262 Before you can delete a snapshot, you must unprotect it first. Additionally,
263 you may *NOT* delete snapshots that have references from clones. You must
264 flatten each clone of a snapshot, before you can delete the snapshot. ::
265
266 rbd snap unprotect {pool-name}/{image-name}@{snapshot-name}
267
268 For example::
269
270 rbd snap unprotect rbd/my-image@my-snapshot
271
272
273 Listing Children of a Snapshot
274 ------------------------------
275
276 To list the children of a snapshot, execute the following::
277
278 rbd children {pool-name}/{image-name}@{snapshot-name}
279
280 For example::
281
282 rbd children rbd/my-image@my-snapshot
283
284
285 Flattening a Cloned Image
286 -------------------------
287
288 Cloned images retain a reference to the parent snapshot. When you remove the
289 reference from the child clone to the parent snapshot, you effectively "flatten"
290 the image by copying the information from the snapshot to the clone. The time
291 it takes to flatten a clone increases with the size of the snapshot. To delete
292 a snapshot, you must flatten the child images first. ::
293
294 rbd flatten {pool-name}/{image-name}
295
296 For example::
297
298 rbd flatten rbd/new-image
299
300 .. note:: Since a flattened image contains all the information from the snapshot,
301 a flattened image will take up more storage space than a layered clone.
302
303
304 .. _cephx: ../../rados/configuration/auth-config-ref/
305 .. _QEMU: ../qemu-rbd/
306 .. _OpenStack: ../rbd-openstack/
307 .. _CloudStack: ../rbd-cloudstack/
308 .. _libvirt: ../libvirt/